Creating Custom Shaped Windows Forms in .NET

來源:互聯網
上載者:User

本文轉自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 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:

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

  2. In Microsoft Visual Studio .NET, create a Windows Application Project.
  3. Set the FormBorderStyle property to None.
  4. Set the BackgroundImage property of the form to the .bmp you created above.
  5. 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
  1. From the Toolbox, drag a Button control to the form as shown in Fig 2.
  2. In the Properties window, set the Text property to "X".
  3. Double-click the Button to add a Click event handler.
  4. Enter code similar to the following to Close the form when the button is clicked. Collapse | Copy Code
    private void button1_Click(object sender, System.EventArgs e){  this.Close();}
  5. Similarly, drop another button and enter the following Code for minimizing, and enter code similar to as follows: Collapse | Copy Code
    private void button2_Click(object sender, System.EventArgs e){  this.WindowState=FormWindowState.Minimized;}

    Fig. 2

Writing code to move the form
  1. Create a new variable:

    Collapse | Copy Code
    public Point mouse_offset;

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

  2. Create an event handler for the form's MouseDown event.

  3. Fig. 3

  4. Enter code similar to the following to assign coordinates to the mouse_offset variable based on the current position of the mouse. Collapse | Copy Code
    private void Form1_MouseDown(object sender,         System.Windows.Forms.MouseEventArgs e){    mouse_offset = new Point(-e.X, -e.Y);}
  5. Similarly, create an event handler for the form's MouseMove event.
  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 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.


License

This article has no explicit license attached to it but may contain 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.