【发布时间】:2010-01-07 18:32:22
【问题描述】:
因此,如果它是工具窗口或最小化表单,我希望能够以编程方式获取其高度。
这可能吗?如果有怎么办?
【问题讨论】:
因此,如果它是工具窗口或最小化表单,我希望能够以编程方式获取其高度。
这可能吗?如果有怎么办?
【问题讨论】:
您可以使用以下方法确定工具窗口和普通表单的标题栏高度:
Rectangle screenRectangle = this.RectangleToScreen(this.ClientRectangle);
int titleHeight = screenRectangle.Top - this.Top;
“this”是您的表单。
ClientRectangle 返回表单客户区的边界。 RectangleToScreen 将此转换为屏幕坐标,该坐标系与表单屏幕位置相同。
【讨论】:
如果您的表单是 MDI 应用程序中的视图,则会出现额外的问题。在这种情况下,RectangleToScreen(this.ClientRectangle) 返回的坐标不是相对于 Form 本身(正如人们可能期望的那样),而是相对于 MainForm ,它承载了承载 Form 的 MDIClient 控件。
您可以通过
来说明这一点Point pnt = new Point(0, 0);
Point corner = this.PointToScreen(pnt); // upper left in MainFrame coordinates
Point origin = this.Parent.PointToScreen(pnt); // MDIClient upperleft in MainFrame coordinates
int titleBarHeight = corner.Y - origin.Y - this.Location.Y;
【讨论】:
这将为您提供 TitleBarsize:
form.ClientRectangle.Height - form.Height;
【讨论】:
在我的例子中,我不得不更改表单的高度,使其刚好低于其中一个控件,我注意到了
int titleHeight = this.Height - screenRectangle.Height;
返回 39 而接受的答案:
int titleHeight = screenRectangle.Top - this.Top;
返回 31
可能是因为表单的底部边框。
【讨论】:
要修正 S. Norman 的答案,即简单地将他的被减数和被减数转换,以下是最简单的答案:
int HeightOfTheTitleBar_ofThis = this.Height - this.ClientRectangle.Height;
顺便说一句,标准的硬编码标题栏是 25dpi,这是最小高度,可以更改为最大 50dpi。
好的好的,...是的,它在技术上是不正确的正如 Cody Gray 所说,但它可以工作并且应该得到与接受的答案相同的答案。无需创建矩形。
【讨论】: