Click here to Skip to main content
Click here to Skip to main content

Dragging a Borderless Form

By , 13 Jul 2010
 
A slightly different solution.
 
using System.Runtime.InteropServices;
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTCAPTION = 0x2;
 
[DllImport("User32.dll")]
private static extern bool ReleaseCapture();
[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
 
public Form()
{
    InitializeComponent();
    AssignHandler(this);
}
 
private void AssignHandler(Control control)
{
    if (control is Form
        || control is Label
        || control is Panel
        || control is PictureBox
        || control is TableLayoutPanel)
    {
        control.MouseDown += Form_MouseDown;
    }
    foreach (Control subControl in control.Controls)
    {
        AssignHandler(subControl);
    }
}
 
private void Form_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dima Popov
Software Developer Energoservice
Russian Federation Russian Federation
Member
Dmitry lives in Arkhangelsk. He has developed C# applications for testing and configuring so-called IEDs since 2007.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 13 Jul 2010
Article Copyright 2010 by Dima Popov
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid