This article from http://www.codeproject.com/KB/cs/customforms.aspx
Creating custom shaped Windows Forms in. net.
- Download source code-12.2 KB
- Download demo-7.62 KB
Introduction
One of the niftiest aspects of Windows Forms is that you can craft themNon-RectangularShapes. microsoft Windows Media Player 7 exhibits this feature, and, without doubt, please developers who see this want to induplicate ate 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 simpleNon RectangularForm:
-
- Create a bitmap withNon-RectangularShape. 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
Formborderstyle
PropertyNonE
.
- Set
Backgroundimage
Property of the form to. BMPYou created above.
-
- Set
Transparencykey
Property of the form to the background color of. BMPFile. In the case of the example above, you wocould set it to black. This will make the black portion of the form disappear.Note:SettingFormborderstyle
ToNonE
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
Button
ControlTo the form as shown in Fig 2.
- In the Properties window, set
Text
Property"X
".
-
- Double-click
Button
To addClick
Event Handler.
-
- Enter code similar to the followingCloseThe form when the button is clicked.Collapse|Copy code
Private VoidButton#click (ObjectSender, system. eventargs e ){This. Close ();}
- Similarly, drop another button and enter the following codeMinimizing, And enter code similar to as follows:Collapse|Copy code
Private VoidButton2_click (ObjectSender, system. eventargs e ){This. Windowstate = formwindowstate. minimized ;}
Fig. 2
Writing code to move the form
- Create a new variable:Collapse|Copy code
PublicPoint 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
Mouse_offset
Variable Based on the current position of the mouse.Collapse|Copy codePrivate VoidForm1_mousedown (ObjectSender, system. Windows. Forms. mouseeventargs e) {mouse_offset =NewPoint (-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.Collapse | Copy code Private VoidForm1_mousemove (ObjectSender, 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. PressF5To 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.
License
This article has no explicit license attached to it but may contain in usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here