I;m not a great fan of title bars on windows forms, so I've removed it. However I'm looking for a way for the end user to drag it to wherever they choose.

Is there a way in the .Net framework? All my searches resulted in importing user32.dll, but the majority of answers were from a few years ago.

Was looking for an Allow Drag attribute for the form itself or even a control without luck.

As alway, I appreciate any advice warmly.

5 Years
Discussion Span
Last Post by Suzie999

Check here: http://www.codeproject.com/KB/cs/csharpmovewindow.aspx

Two ways. First:

//API functions to move the form
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
public static extern bool ReleaseCapture();
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

//And in your mouse down event of the form:

public void Form1_MouseDown(object sender, MouseEventArgs e)
//If the left mouse is pressed, release form for movement
if (e.Button == MouseButtons.Left)



protected override void WndProc(ref Message m)
            base.WndProc(ref m);

            int WM_NCHITTEST = 0x84;
            if (m.Msg == WM_NCHITTEST)
                int HTCLIENT = 1;
                int HTCAPTION = 2;
                if (m.Result.ToInt32() == HTCLIENT)
                    m.Result = (IntPtr)HTCAPTION;

I use the second. Its much simplier and doesn't use any API's. Though the first method is the one that I was using during my VB6 days...

Edited by Mitja Bonca: n/a

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
    public partial class Form1 : Form
        private Panel panel1 = new Panel();
        private PictureBox box = new PictureBox();
        private Panel closeButton = new Panel();
        private bool moveable;
        private Point currentPosition;
        private Image img;
        public Form1()
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            closeButton.Dock = DockStyle.Right;
            closeButton.Width = 35;
            closeButton.Click += new EventHandler(closeButton_Click);
            img = box.ErrorImage;
            closeButton.Paint += new PaintEventHandler(closeButton_Paint);
            panel1.BackColor = Color.Blue;
            panel1.Dock = DockStyle.Top;
            panel1.Height = 35;
            panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
            panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
            panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);

        void closeButton_Paint(object sender, PaintEventArgs e)
            e.Graphics.DrawImage(img, 0, 0, closeButton.Width, closeButton.Height);

        void closeButton_Click(object sender, EventArgs e)


        void panel1_MouseMove(object sender, MouseEventArgs e)
            if (moveable)
                Point newPosition = Control.MousePosition;
                newPosition.X = newPosition.X - currentPosition.X; // .Offset(mouseOffset.X, mouseOffset.Y);
                newPosition.Y = newPosition.Y - currentPosition.Y;
                this.Location = newPosition;

        void panel1_MouseUp(object sender, MouseEventArgs e)
            moveable = false;

        void panel1_MouseDown(object sender, MouseEventArgs e)
            moveable = true;
            currentPosition.X = e.X;
            currentPosition.Y = e.Y;

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.