Я хочу отображать два номера в полноэкранном режиме,
друг над другом,
как независимо от фактического размера экрана.
//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 (верхушки) все должно работать.
Я также попытался сделать шрифты меньше высоты ярлыка, что помогло немного, но, конечно же, не было исправлено.
Я на самом деле, хотя textalign должен покрыть это, потому что для этого он и есть.
Также изменение высоты-comp (low middle top) textalign ничего не меняло, тогда как left/center/right действительно делали разницу ожидаемой
Что может быть причиной этого?
Единицей измерения по умолчанию для шрифта являются точки, а не пиксели. Например, с настройкой DPI по умолчанию 96, шрифт 9 точек занимает 9 * 96/72 = 12 пикселей. Поэтому шрифт, который вы просите, слишком велик и не подходит.
Обходной путь прост, вы можете указать единицу измерения, которую вы предпочитаете, с перегрузкой конструктора шрифтов, которая принимает аргумент GraphicsUnit. Fix:
Font sollFont = new Font(FontFamily.GenericSansSerif, lSoll.Height, GraphicsUnit.Pixel);
Font sFSized = new Font(sollFont.FontFamily, lSoll.Height, GraphicsUnit.Pixel);