如何防止在 VB.NET Windows 窗体中垂直调整大小 [英] How to prevent vertically resize in VB.NET Windows Forms

查看:43
本文介绍了如何防止在 VB.NET Windows 窗体中垂直调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Windows 窗体,我想知道是否有某种方法可以防止窗体的垂直调整大小.我想允许用户在除垂直之外的所有方向调整表单的大小.此外,我想允许向上垂直调整大小,而不是向下.

Working with Windows Forms, I wonder if there is some way to prevent vertically resize of the form. I would like to allow user to resize a form in all directions except vertically. Moreover, I would like to allow vertically resize in the upward direction, but not downward.

我尝试通过将其设置为:Me.maximumsize = new size(0, me.height)

I have tried to use maximumsize by setting it to: Me.maximumsize = new size(0, me.height)

我将宽度设置为 0,因为我想允许用户更改表单宽度.

I set width to 0, because I want to allow user to change the form width.

不幸的是它不起作用.

有什么想法吗?

推荐答案

您必须小心让表单在启动时自行调整大小.有必要适应具有不同视频 DPI 设置或不同系统字体大小的机器所需的缩放.或者更改标题栏高度的用户覆盖.所有这些都在 Load 事件运行时整理出来.因此:

You have to be careful to allow the form to resize itself at startup. It is necessary to accommodate scaling needed on a machine that has a different video DPI setting or a different system font size. Or a user override that changed the height of the title bar. All of this is sorted out by the time the Load event runs. Thus:

protected override void OnLoad(EventArgs e) {
  Screen scr = Screen.FromControl(this);
  this.MinimumSize = new Size(this.MinimumSize.Width, this.Height);
  this.MaximumSize = new Size(scr.WorkingArea.Width, this.Height);
}

接下来您应该做的是修复当用户将光标移动到允许垂直调整窗口大小的窗口边缘时光标的行为.这有点难看;您必须使用 WndProc 捕获 WM_NCHITTEST 消息并更改消息返回值:

The next thing you ought to do is fix the behavior of the cursor when the user moves it on an edge of the window that allows resizing the window vertically. That's a bit ugly; you have to trap the WM_NCHITTEST message with WndProc and change the message return value:

protected override void WndProc(ref Message m) {
  base.WndProc(ref m);
  if (m.Msg == 0x84) {  // Trap WM_NCHITTEST
    switch (m.Result.ToInt32()) {
      case 12: m.Result = (IntPtr)2; break;  // HTTOP to HTCAPTION
      case 13: m.Result = (IntPtr)10; break; // etc..
      case 14: m.Result = (IntPtr)11; break;
      case 15: m.Result = (IntPtr)1; break;
      case 16: m.Result = (IntPtr)10; break;
      case 17: m.Result = (IntPtr)11; break;
    }
  }
}

这篇关于如何防止在 VB.NET Windows 窗体中垂直调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆