無邊框窗體拖拽拉大,通過API

private const int WM_NCHITTEST = 0x84; //移動鼠標,按住或釋放鼠標時發生的系統消息
        private const int HTCLIENT = 0x1;//工作區
        private const int HTSYSMENU = 3;//系統菜單
        private const int HTCAPTION = 0x2; //標題欄

        private const int HTLEFT = 10;//向左
        private const int HTRIGHT = 11;//向右
        private const int HTTOP = 12;//向上
        private const int HTTOPLEFT = 13;//向左上
        private const int HTTOPRIGHT = 14;//向右上
        private const int HTBOTTOM = 15;//向下
        private const int HTBOTTOMLEFT = 16;//向左下
        private const int HTBOTTOMRIGHT = 17;//向右下

        private const int BorderWidth = 5;//自己定義的窗體邊的寬度

        //可以調整窗體的大小和移動窗體的位置
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_NCHITTEST:
                    base.WndProc(ref m);
                    if (DesignMode)
                    {
                        return;
                    }

                    if ((int)m.Result == HTCLIENT)//在客戶區
                        if ((Cursor.Position.X <= this.Left + BorderWidth) && (Cursor.Position.Y <= this.Top + BorderWidth))
                            m.Result = (IntPtr)HTTOPLEFT;//左上
                        else if ((Cursor.Position.X >= this.Left + this.Width - BorderWidth) && (Cursor.Position.Y <= this.Top + BorderWidth))
                            m.Result = (IntPtr)HTTOPRIGHT;//右上
                        else if ((Cursor.Position.X <= this.Left + BorderWidth) && (Cursor.Position.Y >= this.Top + this.Height - BorderWidth))
                            m.Result = (IntPtr)HTBOTTOMLEFT;//左下
                        else if ((Cursor.Position.X >= this.Left + this.Width - BorderWidth) && (Cursor.Position.Y >= this.Top + this.Height - BorderWidth))
                            m.Result = (IntPtr)HTBOTTOMRIGHT;//右下
                        else if (Cursor.Position.X <= this.Left + BorderWidth)
                            m.Result = (IntPtr)HTLEFT;//左
                        else if (Cursor.Position.X >= this.Left + this.Width - BorderWidth)
                            m.Result = (IntPtr)HTRIGHT;//右
                        else if (Cursor.Position.Y <= this.Top + BorderWidth)
                            m.Result = (IntPtr)HTTOP;//上
                        else if (Cursor.Position.Y >= this.Top + this.Height - BorderWidth)
                            m.Result = (IntPtr)HTBOTTOM;//下
                        else if (Cursor.Position.Y <= this.Top + BorderWidth + 10)
                        {
                            if (Cursor.Position.X <= this.Left + this.Width - BorderWidth + 10)
                            {
                                m.Result = (IntPtr)HTSYSMENU;//系統菜單,可以在這裏雙擊鼠標關閉窗體。
                            }
                        }
                        else
                            m.Result = (IntPtr)HTCAPTION;//移動窗體,雙擊可以最大化窗體或還原。
                    return;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章