Creating custom shaped Windows Forms in. net

Source: Internet
Author: User

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:

  1. 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.

  2. In Microsoft Visual Studio. NET, create a Windows application project.
  3. SetFormborderstylePropertyNonE.
  4. SetBackgroundimageProperty of the form to. BMPYou created above.
  5. SetTransparencykeyProperty 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:SettingFormborderstyleToNonEDisables 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
  1. From the toolbox, dragButton ControlTo the form as shown in Fig 2.
  2. In the Properties window, setTextProperty"X".
  3. Double-clickButtonTo addClickEvent Handler.
  4. Enter code similar to the followingCloseThe form when the button is clicked.Collapse|Copy code
    Private VoidButton#click (ObjectSender, system. eventargs e ){This. Close ();}
  5. 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
  1. Create a new variable:

    Collapse|Copy code
    PublicPoint mouse_offset;

    This will store the mouse position when the form is clicked.

  2. Create an event handler for the form'sMousedownEvent.

  3. Fig. 3

  4. Enter code similar to the following to assign coordinates toMouse_offsetVariable Based on the current position of the mouse.Collapse|Copy code
    Private VoidForm1_mousedown (ObjectSender, system. Windows. Forms. mouseeventargs e) {mouse_offset =NewPoint (-E. X,-E. Y );}
  5. Similarly, create an event handler for the form'sMousemoveEvent.
  6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.