C# Form 視窗大小

摘要:C# Form 視窗大小

當 FormBorderStyle 設為 None 時,會無法移動以及改變大小

以下是改變大小的程式碼

分別針對四個邊及四個角做判斷

        const int WM_NCHITTEST = 0x0084; //滑鼠事件
        const int HTLEFT = 10;
        const int HTRIGHT = 11;
        const int HTTOP = 12;
        const int HTTOPLEFT = 13;
        const int HTTOPRIGHT = 14;
        const int HTBOTTOM = 15;
        const int HTBOTTOMLEFT = 0x10;
        const int HTBOTTOMRIGHT = 17;
        int padding = 5;
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case WM_NCHITTEST:
                    Point vPoint = new Point((int)m.LParam & 0xFFFF,(int)m.LParam >> 16 & 0xFFFF);
                    vPoint = PointToClient(vPoint);
                    if (vPoint.X <= padding)//left
                    {
                        if (vPoint.Y <= padding)//top left
                            m.Result = (IntPtr)HTTOPLEFT;
                        else if (vPoint.Y >= ClientSize.Height - padding)//bottom left
                            m.Result = (IntPtr)HTBOTTOMLEFT;
                        else m.Result = (IntPtr)HTLEFT;//left
                    }
                    else if (vPoint.X >= ClientSize.Width - padding)//right
                    {
                        if (vPoint.Y <= padding)
                            m.Result = (IntPtr)HTTOPRIGHT;
                        else if (vPoint.Y >= ClientSize.Height - padding)
                            m.Result = (IntPtr)HTBOTTOMRIGHT;
                        else m.Result = (IntPtr)HTRIGHT;
                    }
                    else if (vPoint.Y <= padding)//top
                        m.Result = (IntPtr)HTTOP;
                    else if (vPoint.Y >= ClientSize.Height - padding)//bottom
                        m.Result = (IntPtr)HTBOTTOM;
                    break;
            }
        }

希望能有更好的寫法