[英]Label text appearing too low
我想以全屏方式顯示兩個數字,
彼此相對,
盡管不論實際屏幕尺寸如何。
//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 pt(tops)的舍入誤差外,它都應該有效。
我也嘗試使fontsize小於標簽高度,這有點幫助,但肯定不是一個修復。
我實際上雖然textalign應該涵蓋這個,因為它就是它的用途。
同時chaning textalign的height-comp(低中間頂部)沒有改變任何東西,而left / center / right確實有所不同
可能是什么導致了這個?
字體的默認測量單位是點,而不是像素。 例如,默認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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.