|
Introduction
One of the niftiest aspects of Windows Forms is that you can craft them into non-rectangular shapes. Microsoft Windows Media™ Player 7 exhibits this feature, and, without doubt, many developers who see this want to incorporate it into their own applications. After much searching, I finally found the answer in MSDN.
Using the code
Following are the steps to create a simple non rectangular form:
- Create a bitmap with a non-rectangular shape. This will serve as your form later on, as shown in Fig. 1.

Fig. 1
Note: Choose an easy-to-remember background color, such as black, as this will be important later on.
- In Microsoft Visual Studio .NET, create a Windows Application Project.
- Set the
FormBorderStyle property to None.
- Set the
BackgroundImage property of the form to the .bmp you created above.
- Set the
TransparencyKey property of the form to the background color of the .bmp file. In the case of the example above, you would set it to black. This will make the black portion of the form disappear.
Note: Setting the FormBorderStyle to None disables the standard functionality provided by the title bar. Thus, you must add custom code to the project to allow the form to be moved, closed, minimized, and maximized.
Writing code to close the form
- From the Toolbox, drag a
Button control to the form as shown in Fig 2.
- In the Properties window, set the
Text property to "X".
- Double-click the
Button to add a Click event handler.
- Enter code similar to the following to Close the form when the button is clicked.
private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
- Similarly, drop another button and enter the following Code for minimizing, and enter code similar to as follows:
private void button2_Click(object sender, System.EventArgs e)
{
this.WindowState=FormWindowState.Minimized;
}

Fig. 2
Writing code to move the form
- Create a new variable:
public Point mouse_offset;
This will store the mouse position when the form is clicked.
- Create an event handler for the form's
MouseDown event.

Fig. 3
- Enter code similar to the following to assign coordinates to the
mouse_offset variable based on the current position of the mouse. private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
mouse_offset = new Point(-e.X, -e.Y);
}
- Similarly, create an event handler for the form's
MouseMove event.
- Enter code similar to the following. When the left mouse button is clicked and the mouse is dragged, the form's
Location property is set to the new position. private void Form1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouse_offset.X, mouse_offset.Y);
Location = mousePos;
}
}
Save the application. Press F5 to run it. The form will be shaped like the image you drew at the beginning. Click anywhere on the form and drag it to see the move functionality. Click the Close Form button to close the form.

NOTE: Make Sure your VGA Card is set at 16 bit resolution.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh) | FirstPrevNext |
|
|
 |
|
|
I downloaded the demo file and ran it on windows xp. I found that the black background shows. However if i make the same background design using photoshop but keep the black portion transparent and then using it as a form background image and setting the background color of the form sams as the transparency key color (black in this case) it works fine.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
It works fine in 32 bit. Make your background pic with a transparent background. I use Photoshop CS with a .png or .gif, both worked after you follow the above directions!
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
I've had it work on my home computer with 32 bit display
However at school it won't work on 32 bit, 16 bit is the only way to get it to work.
Is it possible that the type of graphics card would affect it?
My home pc has an ATi X1600 256MB, whereas the school one has integrated (Intel accelerated)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
setting FormBorderStyle to none cause the system context menu (when you right click the form tab in the task bar) not to appear. Does any one know a way of having such a borderless window still have this system menu? Thank you
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi everybody... I have a problem when the form is minimized and then restored.I'm using a button to minimize the form. I added this OnClick method:
void BtnMinimizeClick(object sender, System.EventArgs e) { this.WindowState = FormWindowState.Minimized; }
Later when i restore the form the title bar appears (and it's kinda ugly :S), i can't make it go away..
Anyone Help ¿?¿?
Thanks..
custom windows forms
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, thanks for the articule. I want that the client can resize the form, how can i do that?
Thanks, eL RaNu
ranu.com.ar
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Mental note...this doesnt work with Visual Studio.Net 2003 or windows XP....gotta be one of them cause all tutorials or executables i have tried diplay the black background color even though it is not supposed to....anyone know how to make this work with .NET 2003???
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
It seems to me that it would be more robust using the SetWindowRegion API (Okay, it would make you use a bit of Interop...)
something like :
[DllImport("Gdi32.dll", EntryPoint= "CombineRgn", SetLastError=true, CharSet=CharSet.Auto)] public static extern int CombineRegion(IntPtr dst, IntPtr src1, IntPtr src2, CombineRegionOptions options); [DllImport("User32.dll", EntryPoint= "SetWindowRgn", SetLastError=true,CharSet=CharSet.Auto)] public static extern int SetWindowRegion(IntPtr hWnd, IntPtr hRgn, int visible); [DllImport("Gdi32.dll", EntryPoint= "CreateRectRgn",SetLastError=true,CharSet=CharSet.Auto)] public static extern IntPtr CreateRectangleRegion(int left, int top, int right, int bottom); [DllImport("Gdi32.dll", EntryPoint= "DeleteObject", SetLastError=true,CharSet=CharSet.Auto)] public static extern int DeleteObject(IntPtr obj);
public enum CombineRegionOptions : int { RGN_AND = 1, RGN_OR = 2, RGN_XOR = 3, RGN_DIFF = 4, RGN_COPY = 5, RGN_MIN = RGN_AND, RGN_MAX = RGN_COPY }
IntPtr region = Win32.CreateRectangleRegion(0,0,image.Width,image.Height); for(int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { Color pixel = image.GetPixel(x,y); if((pixel.R == color.R) && (pixel.G == color.G) && (pixel.B == color.B)) { IntPtr pixelRegion = CreateRectangleRegion(x,y,x + 1, y + 1); CombineRegion(region,region, pixelRegion,Win32.CombineRegionOptions.RGN_XOR); DeleteObject(pixelRegion); } } } SetWindowRegion(form.Handle,region,0); where color is the desired transparent color and image the bitmap to parse.
I don't really know where I've seen this code (probably on http://www.learn247.net) but it works well for creating custom shaped forms.
Michael CARBENAY
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Why use SetWindowRegion API when winforms provides the properties already; eg public TestForm() { InitializeComponent(); this.Region = new Region(new Rectangle(5,5,250,150)); }
Works well.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Use this code to set the ouside region of the window...
private void FormMain_Load(object sender, EventArgs e) { GraphicsPath oPath = new GraphicsPath(); oPath.AddEllipse(this.ClientRectangle); this.Region = new Region(oPath); }
C#, ASPX, SQL, novice to NHibernate
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hi! There are problems with using the TransparencyKey when the desktop is set to 32bit color depth. It simply doesn't work then. The workaround is something like this:System.Drawing.Bitmap Img = new System.Drawing.Bitmap("MyBackground.bmp"); Img.MakeTransparent(Img.GetPixel(0,0)); this.BackgroundImage = Img; this.TransparencyKey = Img.GetPixel(0,0);in the Load event of your Form.
Regards, mav
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I tried doing this, but it also makes all of my black text transparent too. Is there anyway to get around that?
Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
What about setting your text color to RGB(1,0,0) ? I didn't try that but maybe it works. And I think, you wouldn't see any difference to black.
if it doesn't fit... use a bigger hammer!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I had the same problem...googled around and found the solution...
System.Drawing.Bitmap Img = new System.Drawing.Bitmap("LoginBackground.png"); Color color = Img.GetPixel(0, 0); Img.MakeTransparent(color); this.BackgroundImage = Img; this.TransparencyKey = color; this.BackColor = color;
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Your showing download files of: Download source files - 13.9 Kb Download demo project - 7.62 Kb
but the links are dead. Anthony
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|