Freigeben über


Move Window border less form c#

We can move windows border less form in 2 ways.

 bool flag = false;

        private void Form1_MouseDown(object sender, MouseEventArgs e)

        { 

            flag = true;

        } 

        private void Form1_MouseMove(object sender, MouseEventArgs e)

        {

            //Check if Flag is True ??? if so then make form position same

            //as Cursor position

            if (flag == true)

            {

                this.Location = Cursor.Position; 

            }

        } 

        private void Form1_MouseUp(object sender, MouseEventArgs e)

        {

            flag = false;

        }

Another way is to TRICK WINDOWS MANAGER into thinking that it is grabbing title bar of the Windows form.

 

publicconstint WM_NCLBUTTONDOWN =0xA1;

publicconstint HT_CAPTION =0x2;

[DllImportAttribute("user32.dll")]publicstaticexternintSendMessage(IntPtr hWnd,intMsg,int wParam,int lParam);

[DllImportAttribute("user32.dll")

]publicstaticextern bool ReleaseCapture();

privatevoidForm1_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e){

if(e.Button==MouseButtons.Left){ReleaseCapture();SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION,0);}

}