現在需要對窗體標題進行居中顯示,透過在標題內容前增加空格的方式達到該目的。
實測是發現視窗標題的字元數量受到作業系統限制
網上查詢的最大標題字元數是260個字元
實測最大字元數為587個
下面的程式碼可以勉強解決“由於最大字元數受到作業系統的限制導致最大化時顯示不全”的問題
1 string iniTitle = ""; 2 private void TitleCenterize() 3 { 4 this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea; 5 string titleMsg; 6 if (this.Text.Length<=20) 7 { 8 iniTitle = this.Text; 9 titleMsg = this.Text; 10 } 11 else 12 { 13 titleMsg = iniTitle; 14 } 15 16 Graphics g = this.CreateGraphics(); 17 Double startingPoint = (this.Width / 2) - (g.MeasureString(titleMsg, this.Font).Width / 2); 18 Double widthOfASpace = g.MeasureString(" ", this.Font).Width; 19 String tmp = " "; 20 Double tmpWidth = 0; 21 int maxTitleLenght = 587; 22 if (startingPoint >= maxTitleLenght) 23 { 24 startingPoint = maxTitleLenght; 25 } 26 while ((tmpWidth + widthOfASpace) < startingPoint) 27 { 28 tmp += " "; 29 tmpWidth += widthOfASpace; 30 } 31 this.Text = tmp + titleMsg; 32 int textLength = tmp.Length; 33 }