C# 中的垂直(仅)可调整大小的窗口窗体
我遇到的情况是,允许用户调整窗口窗体的大小(但仅限垂直方向)对我来说是有益的。经过一番搜索,似乎关于这个特定主题的内容并不多。是否可以?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要将表单的
MinimumSize
和MaximumSize
属性设置为高度不同但宽度相等的两种尺寸。如果您根本不希望出现水平调整大小光标,则需要处理
WM_NCHITTEST
消息,如下所示:You need to set the form's
MinimumSize
andMaximumSize
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:只是一个想法...
编辑:请注意,最小/最大尺寸解决方案比这个 hack 工作得更好:)
Just an idea...
EDIT: please note that the min/max size solutions work much better than this hack :)
设置最大和最小尺寸仅适用于表格的宽度。
Set the max & min size for the width of the form only.
将 FormBorderStyle 设置为 Ressized 并设置 MaximumSize 和 MaximumSize = new Size(this.Width, 0)
修正:
Let the FormBorderStyle to Resizable and set MaximumSize and MinimumSize = new Size(this.Width, 0)
Correction:
是的,这是可能的。只需设置 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).
为了避免 @orsogufo 解决方案的“橡皮筋”效果:
如果调整屏幕边界的大小,它不会正确调整其最大高度以适应更大的屏幕,但对于静态屏幕尺寸,它效果很好。
To avoid the "rubber-banding" effect of @orsogufo's solution:
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.