Hi,

I have a small issue, wondering if anyone could help.

I have a form, FormBorderStyle is set to None. I have a groupBox on the form called gbLogin. I want to be able to move the form via anywhere on the groupBox. I have searched the net and have found some solutions, none that have worked. Such as:

public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HTCAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        private void gbLogin_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
        }

Does anyone have a solution?

Thanks

Jay

hi friend,
sorry! i have not exactly solution for this problem. But these code may be helpful for you.

Point lastPoint;

        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            lastPoint = new Point(e.X, e.Y);
        }

        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Left += e.X - lastPoint.X;
                this.Top += e.Y - lastPoint.Y;
            }
        }

Hi,

Thanks for the reply,

I can't even get that working :P

Does no-one have a solution for me :-(

Try this,

public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();
        }

        private const int WM_NCHITTEST = 0x84;
        private const int HTCLIENT = 0x1;
        private const int HTCAPTION = 0x2;

        protected override void WndProc(ref Message message)
        {
            base.WndProc(ref message);

            if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
                message.Result = (IntPtr)HTCAPTION;
        }
}

Hi,

Thanks for the reply,

This moves the form by clicking on the form itself but doesnt work when trying to move from the groupbox. :-(

Jay

Found the answer.

The code in my first post with

gbLogin.MouseDown += gbLogin_MouseDown;

into the Form_Load eventhandler.

Thanks for everyones help.

No problem, glad I could help. Please mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.