Tuesday, July 13, 2010

How to move a Borderless Form in C#

In C# if you have a WinForm program written without borders or a title bar, moving the form itself can be tricky. Below is a little code I had used in my recent program R2E.


        private void frmMain_MouseDown(object sender, MouseEventArgs e)
        {
            flag = true;
        }


        private void frmMain_MouseMove(object sender, MouseEventArgs e)
        {
            if (flag == true)
            {
                this.Location = Cursor.Position;
                this.Cursor = Cursors.NoMove2D;
            }
        }


        private void frmMain_MouseUp(object sender, MouseEventArgs e)
        {
            flag = false;
            this.Cursor = Cursors.Default;
        }

No comments:

Post a Comment