如何获取Winforms表单标题栏高度的大小?

Joa*_*nge 36 .net c# winforms

因此,如果它是工具窗口或可最小化的形式,我希望能够以编程方式获得其高度.

这可能吗?如果是这样的话?

Ash*_*Ash 58

您可以使用以下命令确定工具窗口和普通表单的标题栏高度:

Rectangle screenRectangle=RectangleToScreen(this.ClientRectangle);

int titleHeight = screenRectangle.Top - this.Top;
Run Code Online (Sandbox Code Playgroud)

你的表格在哪里'这个'.

ClientRectangle返回表单客户区的边界.RectangleToScreen将其转换为屏幕坐标,这是与窗体屏幕位置相同的坐标系.

  • 我相信这是更好的解决方案.SystemInformation.CaptionHeight只会为顶级窗口提供标题栏高度(我相信),并且不适用于ToolWindows,所以这更通用一些. (3认同)
  • 来自我的+1,我也相信这是更好的解决方案.'SystemInformation.CaptionHeight'似乎不适用于ToolWindows. (3认同)
  • 我不知道可能的Aero问题,但是当表单是Developer Express XtraForm时,这种技术对于我确定表单的标题栏高度和边框宽度是有用的,其中这些因素取决于用户选择的外观. (2认同)

LRa*_*aiz 5

如果您的表单是MDI应用程序中的视图,则还会有其他折痕。在那种情况下,RectangleToScreen(this.ClientRectangle)返回的坐标不是相对于Form本身(正如人们可能期望的那样),而是相对于承载MDIClient控件的MainForm的坐标。

您可以通过以下方式解决这个问题

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;
Run Code Online (Sandbox Code Playgroud)