C#中垂直(仅)可调整大小的窗口形式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2140882/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:50:46  来源:igfitidea点击:

Vertically (only) resizable windows form in C#

c#winformsresize

提问by Ben

I have a situation where it would be beneficial to me to allow my windows form to be resized by the user, but only vertically. After some searching, it seems like there isn't much on this particular subject. Is it possible?

我有一种情况,允许用户调整我的窗口窗体的大小对我来说是有益的,但只能垂直调整。经过一番搜索,似乎没有太多关于这个特定主题的内容。是否可以?

采纳答案by SLaks

You need to set the form's MinimumSizeand MaximumSizeproperties to two sizes with different heights but equal widths.

您需要将表单的MinimumSizeMaximumSize属性设置为高度不同但宽度相等的两种尺寸。

If you don't want the horizontal resize cursor to appear at all, you'll need to handle the WM_NCHITTESTmessage, like this:

如果您根本不希望出现水平调整大小光标,则需要处理该WM_NCHITTEST消息,如下所示:

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.Left || result == HitTest.Right)
                m.Result = new IntPtr((int)HitTest.Caption);
            if (result == HitTest.TopLeft || result == HitTest.TopRight)
                m.Result = new IntPtr((int)HitTest.Top);
            if (result == HitTest.BottomLeft || result == HitTest.BottomRight)
                m.Result = new IntPtr((int)HitTest.Bottom);

            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
}

回答by Gabe

Set the max & min size for the width of the form only.

仅设置表单宽度的最大和最小尺寸。

回答by Paolo Tedesco

Just an idea...

只是一个想法...

public partial class Form1 : Form {
    int _width;

    public Form1() {
        _width = this.Width;
        InitializeComponent();
    }

    protected override void OnResize(EventArgs e) {
        this.Width = _width;
        base.OnResize(e);
    }
}

EDIT: please note that the min/max size solutions work much better than this hack :)

编辑:请注意最小/最大尺寸解决方案比这个 hack 好得多:)

回答by Igor Korkhov

Yes, it is possible. Just set your form.MinimumSize.Width = form.MaximumSize.Width = 100 (or whatever width you want).

对的,这是可能的。只需设置您的 form.MinimumSize.Width = form.MaximumSize.Width = 100(或您想要的任何宽度)。

回答by Alex LE

Let the FormBorderStyle to Resizable and set MaximumSize and MinimumSize = new Size(this.Width, 0)

让 FormBorderStyle 为 Resizable 并设置 MaximumSize 和 MinimumSize = new Size(this.Width, 0)

Correction:

更正

this.MinimumSize = new Size(this.Width, 0);
this.MaximumSize = new Size(this.Width, Int32.MaxValue);

回答by Dathan

To avoid the "rubber-banding" effect of @orsogufo's solution:

为避免@orsogufo 解决方案的“橡皮筋”效应:

public Form1()
{
    InitializeComponent();
    this.MinimumSize = new Size(500, 0);
    this.MaximumSize = new Size(500, Screen.AllScreens.Max(s => s.Bounds.Height));
}

It won't correctly adjust its maximum height to accommodate a larger screen if you resize the screen bounds, but for static screen sizes it works great.

如果您调整屏幕边界的大小,它不会正确调整其最大高度以适应更大的屏幕,但对于静态屏幕尺寸,它效果很好。