Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取Winforms标题栏高度的大小?_C#_.net_Winforms - Fatal编程技术网

C# 如何获取Winforms标题栏高度的大小?

C# 如何获取Winforms标题栏高度的大小?,c#,.net,winforms,C#,.net,Winforms,所以,如果它是toolwindow或一个可最小化的表单,我希望能够通过编程获得它的高度 这可能吗?如果是,如何确定?您可以使用以下方法确定工具窗口和法线窗体的标题栏高度: Rectangle screenRectangle = this.RectangleToScreen(this.ClientRectangle); int titleHeight = screenRectangle.Top - this.Top; “这”是你的表格 ClientRectangle返回窗体的客户端区域的边界。

所以,如果它是toolwindow或一个可最小化的表单,我希望能够通过编程获得它的高度


这可能吗?如果是,如何确定?

您可以使用以下方法确定工具窗口和法线窗体的标题栏高度:

Rectangle screenRectangle = this.RectangleToScreen(this.ClientRectangle);

int titleHeight = screenRectangle.Top - this.Top;
“这”是你的表格


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

如果表单是MDI应用程序中的视图,则会出现额外的褶皱。在这种情况下,RectangleToScreen(this.ClientRectangle)返回相对于表单本身的坐标(正如人们所期望的),而是相对于承载表单的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;

这将使您获得标题栏大小:

form.ClientRectangle.Height - form.Height;

要修正S.Norman的答案,即他的分钟数和减数切换,以下是最简单的答案:

intheightoftitlebar_=this.Height-this.ClientRectangle.Height

顺便说一句,标准硬编码标题栏是25dpi,这是最小高度,可以更改为最大50dpi


好的好的,。。。是的,正如Cody Grey所说,它在技术上是不正确的,但它是有效的,应该得到与公认答案相同的答案。无需创建矩形。

在我的例子中,我必须更改窗体的高度,使其刚好位于其中一个控件的下方,我注意到

  int titleHeight = this.Height - screenRectangle.Height;
返回39,而接受的答案为:

  int titleHeight =  screenRectangle.Top - this.Top;
返回31


可能是因为表单的底部边框。

我认为这是更好的解决方案。SystemInformation.CaptionHeight只会给出顶层窗口的标题栏高度(我相信),而不适用于ToolWindow,因此这是一种更通用的方法。+1我也认为这是更好的解决方案。”SystemInformation.CaptionHeight似乎不适用于ToolWindows。我不太确定这是否正确。由于appcompat的原因,Aero在窗口位置上撒谎。它的肥边是个棘手的问题。嗯,我没想到这个。所以你认为没有办法做到这一点?我当时真的很惊讶。我不知道可能的Aero问题,但是当窗体是Developer Express XtraForm时,这种技术对我确定窗体标题栏高度和边框宽度非常有用,其中这些因素取决于用户选择的外观。不完全如此。这将为您提供非客户端区域的高度。在实践中,如果没有其他非客户区,这可能与标题栏的高度相同,但不一定相同。那么,从语义上来说,代码是错误的。它只是错了,句号。他正在从客户区减去表格。这是倒退。