![]() |
Languages »
C# »
Windows Forms
Intermediate
Creating Custom Shaped Windows Forms in .NETBy Haroon RehmanCreating custom shaped Windows Forms in .NET. |
C#, Windows, .NET 1.0, .NET 1.1, GDI+, VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
Following are the steps to create a simple non rectangular form:

Fig. 1
Note: Choose an easy-to-remember background color, such as black, as this will be important later on.
FormBorderStyle property to None.
BackgroundImage property of the form to the .bmp you created above.
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.
Button control to the form as shown in Fig 2.
Text property to "X".
Button to add a Click event handler.
private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, System.EventArgs e)
{
this.WindowState=FormWindowState.Minimized;
}
Fig. 2
public Point mouse_offset;
This will store the mouse position when the form is clicked.
MouseDown event.
Fig. 3
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);
}
MouseMove event.
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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Aug 2004 Editor: Smitha Vijayan |
Copyright 2004 by Haroon Rehman Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |