windows 将表单调整大小限制为水平
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12750761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Limiting form resizing to horizontal
提问by Victor
Possible Duplicate:
Vertically (only) resizable windows form in C#
可能的重复:
C# 中垂直(仅)可调整大小的窗口形式
I have a case where I need to allow the user to resize the form just horizontally. The maximum width of the form is x. How can I do that?
我有一个案例,我需要允许用户水平调整表单的大小。表格的最大宽度为x。我怎样才能做到这一点?
回答by Adam Plocher
Set your MaximumSize and MinimumSize to the same Height, but variable widths.
将 MaximumSize 和 MinimumSize 设置为相同的高度,但宽度可变。
To make it so the resize cursor doesn't appear on the top or bottom:
为了使调整大小光标不会出现在顶部或底部:
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
switch (m.Msg) {
case 0x84: //WM_NCHITTEST
var result = (HitTest)m.Result.ToInt32();
if (result == HitTest.Top || result == HitTest.Bottom)
m.Result = new IntPtr((int)HitTest.Caption);
if (result == HitTest.TopLeft || result == HitTest.BottomLeft)
m.Result = new IntPtr((int)HitTest.Left);
if (result == HitTest.TopRight || result == HitTest.BottomRight)
m.Result = new IntPtr((int)HitTest.Right);
break;
}
}
enum HitTest {
Caption = 2,
Transparent = -1,
Nowhere = 0,
Client = 1,
Left = 10,
Right = 11,
Top = 12,
TopLeft = 13,
TopRight = 14,
Bottom = 15,
BottomLeft = 16,
BottomRight = 17,
Border = 18
}
Code copied and modified from: Vertically (only) resizable windows form in C#
代码复制和修改自:C# 中的垂直(仅)可调整大小的窗口窗体
回答by Marco Mp
Form has the MaximumSize and MinimumSize properties.
窗体具有 MaximumSize 和 MinimumSize 属性。
Set them the same as Size, except for the Width of MaximumSize of course.
将它们设置为与 Size 相同,当然,MaximumSize 的 Width 除外。
It might also be a good idea to disable the MaximizeBox as it won't really make much sense (it would just place the window at the top-left corner of the current monitor).
禁用 MaximizeBox 也可能是一个好主意,因为它实际上没有多大意义(它只会将窗口放在当前监视器的左上角)。
回答by Kyky
You can create an static variable that will hold the mex value that it can has.
您可以创建一个静态变量来保存它可以拥有的 mex 值。
In the form resize event you can check if the value is more than your static value and change it to that value:
在表单调整大小事件中,您可以检查该值是否大于您的静态值并将其更改为该值:
int maxValue = 100;
private void MainForm_ResizeEnd(object sender, EventArgs e)
{
if(this.Size.Width > maxValue)
this.Size.Width = maxValue;
}
Or you can set the Max value in the properties: MaximunSize
或者您可以在属性中设置 Max 值:MaximunSize
回答by Gabi Barrientos
set the minimum and maximum height of the form to the initial height of the form. that should prevent the form to be resized vertically and allow the user to only resize the form horizontally.
将窗体的最小和最大高度设置为窗体的初始高度。这应该可以防止表单垂直调整大小,并允许用户仅水平调整表单大小。
If you want to set bounds for horizontal resizing, do the same for the minimum or maximum width of the form.
如果要为水平调整大小设置边界,请对表单的最小或最大宽度执行相同操作。
回答by user1707399
In the form SizeChanged event, you could do something like this
在表单 SizeChanged 事件中,您可以执行以下操作
private void Form1_ResizeEnd(object sender, EventArgs e) {
//this does not prevent a resize to full screen
int i = this.Size.Height;
//force width to 300
this.Size = new Size(300, i);
return;
}

