Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Creating Nonrectangular Windows


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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.