Question 1:
To allow a form to be moved when dragging its client area, you need to tell the window manager to treat the client area as if it were the title bar (caption area). You suggest something similar in your question.
This can be done in .NET by overriding the WndProc
method of your form, responding to the WM_NCHITTEST
message, and returning HTCAPTION
to indicate that everything should be treated as part of the caption (title) bar, instead of the default HTCLIENT
, which indicates that it should be treated as the form's client area. Add the following code to your form class:
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
{
// Convert HTCLIENT to HTCAPTION
if (m.Result.ToInt32() == HTCLIENT)
{
m.Result = (IntPtr)HTCAPTION;
}
}
}
Question 2:
You can create a form of an arbitrary, non-rectangular shape by setting the Region
property of your form to the custom Region
of your choice. If you have experience with graphics programs like Photoshop, you can think of this as setting a "clipping region" for your form: the window manager will not draw anything outside of the bounds you specify. The pixels in the shape described this Region
can even be non-contiguous.
The simplest way to create your region is probably to use the GraphicsPath
class, and then use the constructor for the Region
class that accepts a single GraphicsPath
object as a parameter.
And as I assume you already know, given the first question, you'll have to set the FormBorderStyle
property to None
to make sure that the default borders drawn by the window manager disappear.
Unfortunately, these regions cannot be anti-aliased. See Hans's answer to this question for more details on these limitations.
Finally, it is worth noting that this latter approach to creating non-rectangular forms can produce some downright ugly user interfaces that don't at all improve the usability of your product, like so:

Please use this technique sparingly and exercise good judgment. When in doubt, rectangles are actually a really good shape for windows.