Custom Resize Handle in Border-less Form C#

前端 未结 4 559
北海茫月
北海茫月 2020-12-03 10:55

I\'m attempting to make border-less forms that pop out of a tool bar. I want the user to be able to grab at the bottom-right corner (a \"resize handle\") and be able to resi

4条回答
  •  旧巷少年郎
    2020-12-03 11:28

    just little modification to your code. I've added WM_MOUSEMOVE message handling:

        protected override void WndProc(ref Message m)
        {
            const UInt32 WM_NCHITTEST = 0x0084;
            const UInt32 WM_MOUSEMOVE = 0x0200;
            const UInt32 HTBOTTOMRIGHT = 17;
            const int RESIZE_HANDLE_SIZE = 10;
            bool handled = false;
            if (m.Msg == WM_NCHITTEST || m.Msg == WM_MOUSEMOVE )
            {
                Size formSize = this.Size;
                Point screenPoint = new Point(m.LParam.ToInt32());
                Point clientPoint = this.PointToClient(screenPoint);
                Rectangle hitBox = new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, formSize.Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE);
                if (hitBox.Contains(clientPoint))
                {
                    m.Result = (IntPtr)HTBOTTOMRIGHT;
                    handled = true;
                }
            }
    
            if (!handled)
                base.WndProc(ref m);
        }
    

    by the way, you can draw system specific window size grip with ControlPaint.DrawSizeGrip Method http://msdn.microsoft.com/en-us/library/2e1yx2sa.aspx

提交回复
热议问题