如何计算 .net windows 窗体的最小_客户端_大小?

发布于 2024-08-13 10:30:30 字数 284 浏览 4 评论 0原文

我正在尝试根据表单最小大小与其客户端大小之间的差异以编程方式在表单中定位某些控件 - 不幸的是,这会根据用户加载的主题产生不同的结果(主要问题似乎是由于以下事实)标题栏和表单边框在不同主题中具有不同的高度/宽度)。我尝试过使用整个窗口的高度(包括标题栏等),但这似乎没有按预期工作:(

不幸的是,这会导致包含的控件未对齐。在这个特定的使用场景中,使用自动布局控件(例如因为流程布局面板)不是一个可行的解决方案,

我一定错过了一些非常明显的东西 - 有没有更好的方法来做到这

一点 戴夫

I am trying to programmatically position certain controls within a form, based on the difference between the forms minimum size and its client size - this unfortunately produces different results depending on what theme the user has loaded (mostly the problem seems to be due to the fact that the title bar and form border have different heights/widths in different themes). I have tried using the height of the entire window (including the title bar etc) but this doesn't seem to work as expected :(

this unfortunately causes contained controls to misalign. in this particular usage scenario, use of automatic layout controls (such as the flow layout panel) isn't a viable solution.

I must be missing something really obvious - is there a better way of doing this?

Apologies if this question sounds dumb

Many Thanks
Dave

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

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

发布评论

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

评论(3

有木有妳兜一样 2024-08-20 10:30:30

我发现准确解决这个问题的唯一方法是做类似的事情:

int delta = this.Height - this.ClientRectangle.Height;

然后当我需要根据客户的形式进行某些操作时使用它
(当我想要一个表单自动调整某些按钮的大小并在它们周围有相等的边框时,我使用了它)。

所以对你来说:

int delta = this.Height - this.ClientRectangle.Height;
int actualMinHeight = this.MinimumSize.Height - delta;

HTH

编辑:我确实尝试使用 SystemInformation.Border3DSize 和 SystemInformation.BorderSize 属性,但它们也没有为我提供正确的宽度。

The only way i have found of accurately working it out is to do something like:

int delta = this.Height - this.ClientRectangle.Height;

and then use that when ever i need to base something off the client are of the form
(I used it when i wanted a form to auto size to some buttons and have an equal border around them).

So for you:

int delta = this.Height - this.ClientRectangle.Height;
int actualMinHeight = this.MinimumSize.Height - delta;

HTH

Edit: I did try using the SystemInformation.Border3DSize and SystemInformation.BorderSize properties but they also did not give the correct widths for me.

你对谁都笑 2024-08-20 10:30:30

我不是 100% 确定您是否要求一种能够在运行时根据形式、边框厚度、图标间距等动态调整控件大小的方法,但是,如果这被否决,我自己会因误解您的内容而受到责备问题,请参见 CodeProject 上的文章。由于您提到了不同的边框大小等,您可能想查看使用 GetSystemMetrics pinvokes 控制边框大小的系统指标,请查看 此处了解此类内容,以及此处。另请参阅 pinvoke.net 网站 的 GetSystemMetrics。

希望这有帮助,
此致,
汤姆.

I am not 100% sure if you are asking for a means to be able to resize controls dynamically at runtime based on the form, thickness of border, icon spacing etc, however, if this gets downvoted, I have myself to blame for misunderstanding your question, the article here on CodeProject. Since you mentioned different border sizes etc, you might want to look at system metrics that controls the border sizes by using GetSystemMetrics pinvokes, have a look here for such a thing, and here. Look here also at the pinvoke.net website for the GetSystemMetrics.

Hope this helps,
Best regards,
Tom.

只怪假的太真实 2024-08-20 10:30:30

对于任何也遇到这个问题的人来说,这里的好人可以找到的最佳解决方案是使用以下内容:

Dim clientRectDelta As Integer = Me.Height - Me.ClientRectangle.Height - (SystemInformation.Border3DSize.Height * 2)
Dim actualMinimumHeight As Integer = Me.MinimumSize.Height - clientRectDelta
Dim deltaHeight As Integer = Me.ClientRectangle.Height - actualMinimumHeight

但是,这忽略了任何控件特定主题相关的高度(即列表视图中列标题的高度发生变化,这似乎改变了列表视图的整体高度,这可能会导致重叠等) - 但它似乎大部分工作。

非常感谢 Pondidum、tommieb75 和 nobugz 帮助解决了这个问题! (对大家抱歉,但我目前没有足够的声誉来将您的所有答案标记为+1)。

For anyone who stumbles on this problem as well, the best solution the good people here could find was to use something along the lines of:

Dim clientRectDelta As Integer = Me.Height - Me.ClientRectangle.Height - (SystemInformation.Border3DSize.Height * 2)
Dim actualMinimumHeight As Integer = Me.MinimumSize.Height - clientRectDelta
Dim deltaHeight As Integer = Me.ClientRectangle.Height - actualMinimumHeight

However, this ignores any control specific theme dependant heights (i.e. the height of column headers in listviews change, which seem to alter the overall height of the listview, which can cause overlaps etc) - but it seems to mostly work.

A big Thank You to Pondidum, tommieb75 and nobugz who helped with this problem! (sorry to all but i dont currently have enough reputation to mark all your answers up +1).

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