1

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, i.e. 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.
I also tried making the fontsize less than label height, which helped a little but was certainly not a fix.
I actually though the textalign should cover this, because that is what it is for.
Also chaning the height-comp(low middle top) of textalign did not change anything, whereas left / center / right do make the difference expected

Screen of misaligned numbers

What could be causing this?

Mark
  • 419
  • 1
  • 6
  • 21
  • 1
    A label always has a bit of padding between its edges and the font so that letters won't overlap with other controls. In your code you set your font size to the height of your label witch means it will never fit. Why not turn it around and calculate the height of your font and put the autoEllipsis property on your label to true (making it auto scale to the text). Note that you still have to do the width of your label manually if you want it to cover the full width of the form. – Nick Otten Nov 03 '14 at 10:46

1 Answers1

3

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. 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. Fix:

 Font sollFont = new Font(FontFamily.GenericSansSerif, lSoll.Height, GraphicsUnit.Pixel);
 Font sFSized = new Font(sollFont.FontFamily, lSoll.Height, GraphicsUnit.Pixel);
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I was wondering where the 72 came from, then found it's 72 points per inch (http://stackoverflow.com/questions/139655/convert-pixels-to-points) – Jeb Nov 03 '14 at 10:54