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: 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 nonclient area altogether, as shown in 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 nonclient adornments yields a nonrectangular 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 that 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 (which will be covered in a subsequent newsletter), 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; <br> // 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, for example. 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, and so on, 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.
Chris Sells in an independent consultant specializing in distributed applications in .NET and COM, as well as an instructor for DevelopMentor. He's written several books including ATL Internals, which is currently being updated for ATL7. He's also working on Essential Windows Forms for Addison-Wesley and Mastering Visual Studio .NET for O'Reilly. In his free time, Chris hosts the Web Services DevCon (November 2002) and directs the Genghis source-available project. If you enjoy this newsletter, you may be interested in SellsBrothers News, which notifies subscribers of the various projects that Chris is working on. Subscribe or browse the archives at http://www.sellsbrothers.com/newsletter.