调整右下角的无边框窗口大小

我希望用户在右下角调整无边框窗口的大小,就像我可以调整combobox控件的自动完成窗口一样。

我无法找到以这种方式配置表单的属性。

也许有人可以帮我解决这个问题。

可以在这里找到一张图片:

在此处输入图像描述

这是与Franci的解释相对应的代码,我正在写它,但他同时回答所以如果这个代码适合你的需要,那么他的解释是好的。

 protected override void WndProc(ref Message m) { const int wmNcHitTest = 0x84; const int htBottomLeft = 16; const int htBottomRight = 17; if (m.Msg == wmNcHitTest) { int x = (int) (m.LParam.ToInt64() & 0xFFFF); int y = (int) ((m.LParam.ToInt64() & 0xFFFF0000) >> 16); Point pt = PointToClient(new Point(x, y)); Size clientSize = ClientSize; if (pt.X >= clientSize.Width - 16 && pt.Y >= clientSize.Height - 16 && clientSize.Height >= 16) { m.Result = (IntPtr) (IsMirrored ? htBottomLeft : htBottomRight); return; } } base.WndProc(ref m); } 

编辑:要编写抓取器,您可以初始化一个new VisualStyleRenderer(VisualStyleElement.Status.Gripper.Normal)并使用其PaintBackground()方法。

非常感谢您发布这个精彩的样本和解释。 我添加了一些其他人可能感兴趣的附加内容。这里的一些代码来自其他stackoverflow发布,但是能够在一个代码块中看到它可能对其他人有帮助。 我希望能够在所有边框上调整表单大小,而不仅仅是右下角。 我也希望能够拖动表单。 最后,我想要一个阴影。

 //*********************************************************** //This gives us the ability to resize the borderless from any borders instead of just the lower right corner protected override void WndProc(ref Message m) { const int wmNcHitTest = 0x84; 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 = 16; const int htBottomRight = 17; if (m.Msg == wmNcHitTest) { int x = (int)(m.LParam.ToInt64() & 0xFFFF); int y = (int)((m.LParam.ToInt64() & 0xFFFF0000) >> 16); Point pt = PointToClient(new Point(x, y)); Size clientSize = ClientSize; ///allow resize on the lower right corner if (pt.X >= clientSize.Width - 16 && pt.Y >= clientSize.Height - 16 && clientSize.Height >= 16) { m.Result = (IntPtr)(IsMirrored ? htBottomLeft : htBottomRight); return; } ///allow resize on the lower left corner if (pt.X <= 16 && pt.Y >= clientSize.Height - 16 && clientSize.Height >= 16) { m.Result = (IntPtr)(IsMirrored ? htBottomRight : htBottomLeft); return; } ///allow resize on the upper right corner if (pt.X <= 16 && pt.Y <= 16 && clientSize.Height >= 16) { m.Result = (IntPtr)(IsMirrored ? htTopRight : htTopLeft); return; } ///allow resize on the upper left corner if (pt.X >= clientSize.Width - 16 && pt.Y <= 16 && clientSize.Height >= 16) { m.Result = (IntPtr)(IsMirrored ? htTopLeft : htTopRight); return; } ///allow resize on the top border if (pt.Y <= 16 && clientSize.Height >= 16) { m.Result = (IntPtr)(htTop); return; } ///allow resize on the bottom border if (pt.Y >= clientSize.Height - 16 && clientSize.Height >= 16) { m.Result = (IntPtr)(htBottom); return; } ///allow resize on the left border if (pt.X <= 16 && clientSize.Height >= 16) { m.Result = (IntPtr)(htLeft); return; } ///allow resize on the right border if (pt.X >= clientSize.Width - 16 && clientSize.Height >= 16) { m.Result = (IntPtr)(htRight); return; } } base.WndProc(ref m); } //*********************************************************** //*********************************************************** //This gives us the ability to drag the borderless form to a new location public const int WM_NCLBUTTONDOWN = 0xA1; public const int HT_CAPTION = 0x2; [DllImportAttribute("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImportAttribute("user32.dll")] public static extern bool ReleaseCapture(); private void YOURCONTROL_MouseDown(object sender, MouseEventArgs e) { //ctrl-leftclick anywhere on the control to drag the form to a new location if (e.Button == MouseButtons.Left && Control.ModifierKeys == Keys.Control) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); } } //*********************************************************** //*********************************************************** //This gives us the drop shadow behind the borderless form private const int CS_DROPSHADOW = 0x20000; protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ClassStyle |= CS_DROPSHADOW; return cp; } } //*********************************************************** 

使用面板的MouseDown和MouseMove事件将面板或其他控件放在角落中,适当调整表单大小。

在MouseDown中,我会记录坐标,然后在MouseMove中,您可以计算与原始位置的差异,以调整表格大小。