Main Menu
Knowledge Base
Product Registration
Log an Incident
Request a Feature
Search Incidents/Bug Reports


Search KB

Please note: In an effort to better serve you, we are in the process of restructuring DevCenter. In the process, we have moved many items that you may be used to finding in DevCenter over to the Main Site. If you are having trouble locating something, please try looking at the following places:

Creating Non-Rectangular Windows
View Printer Friendly Version

Chris Sells
Windows Developer Network
Page Options
Average Rating:
6 out of 10

Rate this page
Print this page
E-mail this page
Add to Favorites

Posted:

Monday, September 01, 2003

Applies To:


  • C#
  • .NET Windows Forms

Summary:

Break out of the box by combining TransparencyKey with FormBorderStyle.None to create forms of any shape.

It’s possible to change the shape of the form by making parts completely transparent. One way to do this is with the TransparencyKey property, which designates a color to use to mark transparent pixels. When a pixel on the form is supposed to be drawn with the transparent key color, that pixel will instead be removed from the form, both in the sense that the pixel will not be drawn over whatever is behind it and that clicking on that spot on the form will actually cause the click to happen on what’s underneath.
As an example, setting the TransparencyKey property to the same as the BackColor property of the form will cause a form to lose its background (as well as anything else drawn with that color), as shown in Figure 1.

Figure 1
Figure 1: Form shown in front of Notepad with TransparencyKey set to SystemColors.Control


Of course, the novelty of a form as shown in Figure 1 seems pretty limited until you combine it with FormBorderStyle.None, removing the non-client area altogether, as shown in Figure 2.
 
Figure 2
Figure 2: TransparencyKey combined with FormBorderStyle.None

The combination of a transparent color to erase the form’s background and setting the form border style to FormBorderStyle.None to erase the non-client adornments yields a non-rectangular window, which is all the rage with the kids these days. The transparency key color is used to create a region that describes the visible area of the form to the underlying OS, as set via the Form.Region property, and which can be any combination of shapes and holes.

Moving and Sizing


Unfortunately, with the caption and the edges on the form missing, there’s no way for the user to resize or move. You can certainly implement moving and resizing with the WinForms mouse events (covered later in this chapter), but that’s more trouble than you need to go to in this case. Instead, let’s leverage the fact that WinForms is built on top of Win32 and handle the WM_NCHITTEST message directly by overloading the form’s WndProc method:

protected override void WndProc(ref Message m) {
  // Let the base class have first crack
  base.WndProc(ref m);
  int WM_NCHITTEST = 0x84; // winuser.h
  if( m.Msg != WM_NCHITTEST ) return;</P><P>  // If the user clicked on the client area,
  // ask the OS to treat it as a click on the caption
  int HTCLIENT = 1;
  int HTCAPTION = 2;
  if( m.Result.ToInt32() == HTCLIENT )
    m.Result = (IntPtr)HTCAPTION;
}

WM_NCHITTEST is used by the system every time the mouse moves over a window to determine what part of the window the mouse is currently over, e.g. the caption, an edge or the client area. This information is used to change the cursor and to handle clicks and drags. In the case of our elliptical form example, when the user clicks anywhere in the client area, we’d like to pretend that they’ve clicked on the caption, which will tell the OS to handle clicking and dragging to move the form around for us. We could get really fancy and detect when the mouse is over the edge of the ellipse, setting the Message.Result field to HTLEFT, HTRIGHT, etc. depending on what part of the edge the mouse was over, instructing the OS to handle the sizing for us as well as the moving, but I’ll leave that as an exercise for the reader.

Article provided by:

How would you rate the quality of this content?
Poor -----------------------------------------> Outstanding

Tell us why you rated the content this way. (optional)