[英]Label text appearing too low
I want to display two numbers in a full-screen manner, 我想以全屏方式显示两个数字,
above each other, 彼此相对,
as regardless of actual screen size as possible. 尽管不论实际屏幕尺寸如何。
//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);
Issue with this code (besides clumsyness): 这段代码的问题(除了笨拙):
The text on the labels is displayed partly below the labels' lower bounds, ie invisible, see screenshot at the bottom. 标签上的文字部分显示在标签的下限之下,即不可见,请参见底部的屏幕截图。
I double checked my calculations and found that other than a rounding error of 1 pt (tops) it all should work. 我仔细检查了我的计算结果,发现除了1 pt(tops)的舍入误差外,它都应该有效。
I also tried making the fontsize less than label height, which helped a little but was certainly not a fix. 我也尝试使fontsize小于标签高度,这有点帮助,但肯定不是一个修复。
I actually though the textalign should cover this, because that is what it is for. 我实际上虽然textalign应该涵盖这个,因为它就是它的用途。
Also chaning the height-comp(low middle top) of textalign did not change anything, whereas left / center / right do make the difference expected 同时chaning textalign的height-comp(低中间顶部)没有改变任何东西,而left / center / right确实有所不同
What could be causing this? 可能是什么导致了这个?
The default unit of measurement for a font is points, not pixels. 字体的默认测量单位是点,而不是像素。 For example, with a default DPI setting of 96, a 9 point font takes up 9 * 96 / 72 = 12 pixels.
例如,默认DPI设置为96,9磅字体占用9 * 96/72 = 12像素。 So the font you ask for is too big and doesn't fit.
所以你要求的字体太大而且不适合。
The workaround is simple, you can specify the unit of measurement you prefer with a Font constructor overload that takes a GraphicsUnit argument. 解决方法很简单,您可以使用带有GraphicsUnit参数的Font构造函数重载来指定您喜欢的度量单位。 Fix:
固定:
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.