c#表單去掉borderstyle進行拖動

來源:互聯網
上載者:User

標籤:style   io   ar   os   sp   for   on   bs   ad   

 private void BaseCForm_Load(object sender, EventArgs e)
        {
            DrawStyle();

            this.panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
            this.panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
            this.panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y);
                leftFlag = true;
            }

        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                leftFlag = false;
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);
                if (sender is Panel)
                {
                    ((System.Windows.Forms.Panel)sender).Parent.Location = mouseSet;
                }
            }

        }

 

 

 

[DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        private const int WM_SYSCOMMAND = 0x0112;//點擊視窗左上方那個表徵圖時的系統資訊
        private const int WM_MOVING = 0x216;//滑鼠移動訊息
        private const int SC_MOVE = 0xF010;//移動資訊
        private const int HTCAPTION = 0x0002;//表示滑鼠在視窗標題列時的系統資訊
        private const int WM_NCHITTEST = 0x84;//滑鼠在表單客戶區(除了標題列和邊框以外的部分)時發送的訊息
        private const int HTCLIENT = 0x1;//表示滑鼠在視窗客戶區的系統訊息
        private const int SC_MAXIMIZE = 0xF030;//最大化資訊
        private const int SC_MINIMIZE = 0xF020;//最小化資訊

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_MOVING: //如果滑鼠移                 
                    base.WndProc(ref m);//調用基類的視窗過程——WndProc方法處理這個訊息
                    if (m.Result == (IntPtr)HTCLIENT)//如果返回的是HTCLIENT
                    {
                        m.Result = (IntPtr)HTCAPTION;//把它改為HTCAPTION
                        return;//直接返回退出方法
                    }
                    break;
            }
            base.WndProc(ref m);//如果不是滑鼠移動或單擊訊息就調用基類的視窗過程進行處理

        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
            base.OnMouseMove(e);
        }

 

c#表單去掉borderstyle進行拖動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.