<p>I am creating a small modal form that is used in Winforms application. It is basically a progress bar of sorts. But I would like the user to be able to click anywhere in the form and drag it to move it around on the desktop while it is still being displayed.</p>

<p>How can I implement this behavior?</p>

[{'answer_id': 30241, 'author': 'Timothy Fries', 'author_id': 3163, 'author_profile': 'https://Stackoverflow.com/users/3163', 'pm_score': 6, 'selected': True, 'text': '<p><a href="http://support.microsoft.com/kb/320687" rel="nofollow noreferrer" title="KB Article 320687">Microsoft KB Article 320687</a> has a detailed answer to this question.</p>\n\n<p>Basically, you override the WndProc method to return HTCAPTION to the WM_NCHITTEST message when the point being tested is in the client area of the form -- which is, in effect, telling Windows to treat the click exactly the same as if it had occured on the caption of the form.</p>\n\n<pre><code>private const int WM_NCHITTEST = 0x84;\nprivate const int HTCLIENT     = 0x1;\nprivate const int HTCAPTION    = 0x2;\n\nprotected override void WndProc(ref Message m)\n{\n    switch(m.Msg)\n    {\n        case WM_NCHITTEST:\n        base.WndProc(ref m);\n\n        if ((int)m.Result == HTCLIENT)\n            m.Result = (IntPtr)HTCAPTION;\n        return;\n    }\n\n    base.WndProc(ref m);\n}\n</code></pre>\n'}, {'answer_id': 30245, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 3, 'selected': False, 'text': '<p>The following code assumes that the ProgressBarForm form has a ProgressBar control with <strong>Dock</strong> property set to <strong>Fill</strong></p>\n\n<pre><code>public partial class ProgressBarForm : Form\n{\n    private bool mouseDown;\n    private Point lastPos;\n\n    public ProgressBarForm()\n    {\n        InitializeComponent();\n    }\n\n    private void progressBar1_MouseMove(object sender, MouseEventArgs e)\n    {\n        if (mouseDown)\n        {\n            int xoffset = MousePosition.X - lastPos.X;\n            int yoffset = MousePosition.Y - lastPos.Y;\n            Left += xoffset;\n            Top += yoffset;\n            lastPos = MousePosition;\n        }\n    }\n\n    private void progressBar1_MouseDown(object sender, MouseEventArgs e)\n    {\n        mouseDown = true;\n        lastPos = MousePosition;\n    }\n\n    private void progressBar1_MouseUp(object sender, MouseEventArgs e)\n    {\n        mouseDown = false;\n    }\n}\n</code></pre>\n'}, {'answer_id': 30278, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 4, 'selected': False, 'text': '<p>Here is a way to do it using a P/Invoke.</p>\n\n<pre><code>public const int WM_NCLBUTTONDOWN = 0xA1;\npublic const int HTCAPTION = 0x2;\n[DllImport("User32.dll")]\npublic static extern bool ReleaseCapture();\n[DllImport("User32.dll")]\npublic static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);\n\nvoid Form_Load(object sender, EventArgs e)\n{\n   this.MouseDown += new MouseEventHandler(Form_MouseDown);  \n}\n\nvoid Form_MouseDown(object sender, MouseEventArgs e)\n{                        \n    if (e.Button == MouseButtons.Left)\n    {\n        ReleaseCapture();\n        SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);\n    }\n}\n</code></pre>\n'}, {'answer_id': 21486168, 'author': 'Simon Mourier', 'author_id': 403671, 'author_profile': 'https://Stackoverflow.com/users/403671', 'pm_score': 2, 'selected': False, 'text': "<p>The accepted answer is a cool trick, but it doesn't always work if the Form is covered by a Fill-docked child control like a Panel (or derivates) for example, because this control will eat all most Windows messages.</p>\n\n<p>Here is a simple approach that works also in this case: derive the control in question (use this class instead of the standard one) an handle mouse messages like this:</p>\n\n<pre><code>    private class MyTableLayoutPanel : Panel // or TableLayoutPanel, etc.\n    {\n        private Point _mouseDown;\n        private Point _formLocation;\n        private bool _capture;\n\n        // NOTE: we cannot use the WM_NCHITTEST / HTCAPTION trick because the table is in control, not the owning form...\n        protected override void OnMouseDown(MouseEventArgs e)\n        {\n            _capture = true;\n            _mouseDown = e.Location;\n            _formLocation = ((Form)TopLevelControl).Location;\n        }\n\n        protected override void OnMouseUp(MouseEventArgs e)\n        {\n            _capture = false;\n        }\n\n        protected override void OnMouseMove(MouseEventArgs e)\n        {\n            if (_capture)\n            {\n                int dx = e.Location.X - _mouseDown.X;\n                int dy = e.Location.Y - _mouseDown.Y;\n                Point newLocation = new Point(_formLocation.X + dx, _formLocation.Y + dy);\n                ((Form)TopLevelControl).Location = newLocation;\n                _formLocation = newLocation;\n            }\n        }\n    }\n</code></pre>\n"}, {'answer_id': 34101694, 'author': 'Wyllow Wulf', 'author_id': 5310022, 'author_profile': 'https://Stackoverflow.com/users/5310022', 'pm_score': 0, 'selected': False, 'text': '<p>VC++ 2010 Version (of FlySwat\'s):</p>\n\n<pre><code>#include &lt;Windows.h&gt;\n\nnamespace DragWithoutTitleBar {\n\n    using namespace System;\n    using namespace System::Windows::Forms;\n    using namespace System::ComponentModel;\n    using namespace System::Collections;\n    using namespace System::Data;\n    using namespace System::Drawing;\n\n    public ref class Form1 : public System::Windows::Forms::Form\n    {\n    public:\n        Form1(void) { InitializeComponent(); }\n\n    protected:\n        ~Form1() { if (components) { delete components; } }\n\n    private:\n        System::ComponentModel::Container ^components;\n        HWND hWnd;\n\n#pragma region Windows Form Designer generated code\n        void InitializeComponent(void)\n        {\n            this-&gt;SuspendLayout();\n            this-&gt;AutoScaleDimensions = System::Drawing::SizeF(6, 13);\n            this-&gt;AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;\n            this-&gt;ClientSize = System::Drawing::Size(640, 480);\n            this-&gt;FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;\n            this-&gt;Name = L"Form1";\n            this-&gt;Text = L"Form1";\n            this-&gt;Load += gcnew EventHandler(this, &amp;Form1::Form1_Load);\n            this-&gt;MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &amp;Form1::Form1_MouseDown);\n            this-&gt;ResumeLayout(false);\n\n        }\n#pragma endregion\n    private: System::Void Form1_Load(Object^ sender, EventArgs^  e) {\n                    hWnd = static_cast&lt;HWND&gt;(Handle.ToPointer());\n                }\n\n    private: System::Void Form1_MouseDown(Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {\n                    if (e-&gt;Button == System::Windows::Forms::MouseButtons::Left) {\n                        ::ReleaseCapture();\n                        ::SendMessage(hWnd, /*WM_NCLBUTTONDOWN*/ 0xA1, /*HT_CAPTION*/ 0x2, 0);\n                    }\n                }\n\n    };\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/30184', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/507/']