Make a transparent form and let the user move it in C#
To make a transparent form, set the form's TransparencyKey property to the same color as the form's BackColor.
If you like, you can give the form a BackgroundImage containing the color. However if you do, be sure to use a png, bmp, or some other format that produces exact colors. If you use a jpg, for example, the colors won't be exactly the same as the TransparencyKey color so they won't be transparent even if they look like the same color to the naked eye.
To avoid weird border and title bar effects, set the form's FormBorderStyle property to None.
That makes the form transparent but because the title bar is gone, the user cannot move it.
To let the user move the form, pick the event that starts moving. The following code uses the form's MouseDown event.
When the event occurs, use the WndProc method to process the WM_NCLBUTTONDOWN message with the HT_CAPTION parameter. That tells the form that the user has pressed the mouse down in the form's non-client area (that's what WM_NCLBUTTONDOWN means) over the caption (that's what HT_CAPTION means).
// On mouse down, start moving the form.You can make special areas that the user has to click to move the form if you like. You can also add special areas to minimize, maximize, and close the form (the standard areas are removed with the FormBorderStyle = None). Note that the user can still use Alt+F4 to close the form even though the close button is gone.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
const int WM_NCLBUTTONDOWN = 0xA1;
const int HT_CAPTION = 0x2;
this.Capture = false;
Message msg = Message.Create(this.Handle,
WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
WndProc(ref msg);
}



Comments