2014-11-03 31 views
1

我想以全屏方式顯示兩個數字,
彼此高於,
儘可能不考慮實際的屏幕尺寸。與此代碼(除clumsyness)標籤文字顯得太低

 //getting screen size and setting window to maximized 
     Rectangle screenEdge = Screen.PrimaryScreen.Bounds; 
     this.Width = screenEdge.Width; 
     this.Height = screenEdge.Height; 
     this.FormBorderStyle = FormBorderStyle.FixedToolWindow; 
     this.WindowState = FormWindowState.Maximized; 

     //using 90% of width and 40% (times two) of height 
     int lWidth = (int)(this.Width * 0.9); 
     int lHeight = (int)(this.Height * 0.4); 

     //horiz. spacing: remainder, divided up for left and right 
     int lLeft = (this.Width - lWidth)/2; 
     //vert. spacing: remainder divided for top, bottom and between 
     int lTop = (this.Height - (2 * lHeight))/3 ; 

     //the labels holding the numbers 
     lSoll = new Label(); 
     lIst = new Label(); 

     //setting label lSoll to calc'd dimensions, adding & aligning text 
     lSoll.Left = lLeft; 
     lSoll.Width = lWidth; 
     lSoll.Top = lTop; 
     lSoll.Height = lHeight; 
     Font sollFont = new Font(FontFamily.GenericSansSerif, lSoll.Height); 
     Font sFSized = new Font(sollFont.FontFamily, lSoll.Height); 
     lSoll.Font = sFSized; 
     lSoll.TextAlign = ContentAlignment.MiddleCenter; 
     lSoll.ForeColor = Color.Blue; 
     lSoll.BackColor = Color.White; 
     updateSollText(42); 

     //same as above, just a bit lower 
     lIst.Left = lLeft; 
     lIst.Width = lWidth; 
     lIst.Top = lTop * 2 + lSoll.Height; 
     lIst.Height = lHeight; 
     Font istFont = new Font(FontFamily.GenericSansSerif, lIst.Height); 
     Font iFSized = new Font(istFont.FontFamily, lIst.Height); 
     lIst.Font = iFSized; 
     lIst.TextAlign = ContentAlignment.TopCenter; 
     lIst.ForeColor = Color.Red; 
     lIst.BackColor = Color.White; 
     updateIstText(39); 

問題:
文本在標籤被部分標籤下限,即不可見下面顯示, 見截圖在底部。
我仔細檢查了我的計算結果,發現除了1點(頂部)的舍入誤差之外,這一切都應該起作用。
我也嘗試使字體大小小於標籤高度,這雖然有所幫助,但肯定不是修正。
我其實雖然textalign應該覆蓋這一點,因爲這是它的目的。
也chaning textAlign設置的高度-COMP(低中間頂部)沒有任何改變,而左/中/右做賺取差價預計

Screen of misaligned numbers

可能是什麼造成的?

+1

標籤始終在其邊緣和字體之間填充一點,以便字母不會與其他控件重疊。在你的代碼中,你將字體大小設置爲標籤女巫的高度意味着它永遠不會適合。爲什麼不把它翻過來計算字體的高度,並將標籤上的autoEllipsis屬性設置爲true(使其自動縮放爲文本)。請注意,如果您希望覆蓋表格的整個寬度,您仍然必須手動執行標籤的寬度。 – 2014-11-03 10:46:22

回答

3

字體的默認測量單位是點,而不是像素。例如,默認DPI設置爲96,9磅字體佔用9 * 96/72 = 12像素。所以你要求的字體太大,不適合。

解決方法很簡單,您可以使用帶有GraphicsUnit參數的Font構造函數重載來指定您喜歡的度量單位。修復:

Font sollFont = new Font(FontFamily.GenericSansSerif, lSoll.Height, GraphicsUnit.Pixel); 
Font sFSized = new Font(sollFont.FontFamily, lSoll.Height, GraphicsUnit.Pixel); 
+0

我想知道72從哪裏來,然後發現它是每英寸72點(http://stackoverflow.com/questions/139655/convert-pixels-to-points) – Jeb 2014-11-03 10:54:23

+0

完美的作品!謝謝。 – Mark 2014-11-04 05:37:26