2010-01-07 72 views

回答

45

您可以通過決定兩個工具窗口和正常形態標題欄高度:

Rectangle screenRectangle=RectangleToScreen(this.ClientRectangle); 

int titleHeight = screenRectangle.Top - this.Top; 

在哪裏「這」是你的形式。

ClientRectangle返回窗體客戶區域的邊界。 RectangleToScreen將其轉換爲與屏幕位置相同的座標系的屏幕座標。

+1

我相信這是更好的解決方案。 SystemInformation.CaptionHeight只會給頂層窗口的標題欄高度(我相信),並且不適用於ToolWindows,所以這是更通用的。 – Nick 2010-01-07 18:41:23

+1

+1,我也相信這是更好的解決方案。 'SystemInformation.CaptionHeight'似乎不適用於ToolWindows。 – 2010-01-07 18:54:21

+1

我不太確定這是否正確。由於appcompat的原因,Aero位於窗口位置。它的胖邊界是一個棘手的問題。 – 2010-01-07 20:13:16

2

如果您的表單是MDI應用程序中的視圖,那麼還有一個額外的摺痕。在這種情況下,RectangleToScreen(this.ClientRectangle)返回相對於Form本身的座標(正如人們所期望的那樣),但是相對於託管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; 
0

這將讓你的TitleBarsize:

form.ClientRectangle.Height - form.Height; 
+0

不完全。這給你*非客戶區域*的高度。在實踐中,如果沒有其他非客戶區域,這可能與標題欄的高度相同,但它不一定相同。在語義上,那麼代碼是錯誤的。 – 2016-12-07 13:46:27