C#拖动窗体/双击最大化/改变大小(无边框窗体)

        [DllImport("user32.dll")]//命名空间System.Runtime.InteropServices;
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        public const int SC_MINIMIZE = 0xF020;
        public const int SC_MAXIMIZE = 0xF030;
        public const int SC_RESTORE = 0xF120;
        public const int SC_SIZE = 0xF000;
        //改变窗体大小,SC_SIZE+下面的值
        public const int LEFT = 0x0001;//光标在窗体左边缘
        public const int RIGHT = 0x0002;//右边缘
        public const int UP = 0x0003;//上边缘
        public const int LEFTUP = 0x0004;//左上角
        public const int RIGHTUP = 0x0005;//右上角
        public const int BOTTOM = 0x0006;//下边缘
        public const int LEFTBOTTOM = 0x0007;//左下角
        public const int RIGHTBOTTOM = 0x0008;//右下角
        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Clicks <= 1)
            {
                //拖动窗体
                ReleaseCapture();//释放label1对鼠标的捕捉
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
            else if (e.Clicks == 2)
            {
                if (this.WindowState == FormWindowState.Maximized)
                {
                    //还原窗体
                    ReleaseCapture();
                    SendMessage(this.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
                }
                else if (this.WindowState == FormWindowState.Normal)
                {
                    //最大化窗体
                    ReleaseCapture();
                    SendMessage(this.Handle, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
                }
            }
        }

以上为拖动窗体、双击最大化的处理。

以下为拖动右下角改变窗体大小的处理。

        private void pictureBox4_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Clicks <= 1)
            {
                this.Cursor = Cursors.SizeNWSE;
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_SIZE + RIGHTBOTTOM, 0);//拖动右下角改变窗体大小
            }
        }

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(C#)