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

发布于 2024-08-18 20:42:36 字数 75 浏览 7 评论 0原文

我遇到的情况是,允许用户调整窗口窗体的大小(但仅限垂直方向)对我来说是有益的。经过一番搜索,似乎关于这个特定主题的内容并不多。是否可以?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时间你老了 2024-08-25 20:42:36

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

如果您根本不希望出现水平调整大小光标,则需要处理 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
}

You need to set the form's MinimumSize and MaximumSize properties to two sizes with different heights but equal widths.

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

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
}
你的笑 2024-08-25 20:42:36

只是一个想法...

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);
    }
}

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

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 :)

柳若烟 2024-08-25 20:42:36

设置最大和最小尺寸仅适用于表格的宽度。

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

一抹微笑 2024-08-25 20:42:36

将 FormBorderStyle 设置为 Ressized 并设置 MaximumSize 和 MaximumSize = new Size(this.Width, 0)

修正

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

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

Correction:

this.MinimumSize = new Size(this.Width, 0);
this.MaximumSize = new Size(this.Width, Int32.MaxValue);
云裳 2024-08-25 20:42:36

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

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

◇流星雨 2024-08-25 20:42:36

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

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文