Question : How can I force Graphics.MeasureString to consider the font style?

I am converting a MessageDialog replacement I wrote some time ago to be fully managed instead of using API calls for some of its functions.  This dialog can have a main heading and a subheading or detail view, and it automatically adjusts its font dependiing on the OS in use (Segoe UI for Vista and 7, Tahoma for XP).  Of there is a sub heading then the heading is rendered in bold, otherwise in normal style.

In order to correctly size the dialog I use Graphics.MeasureString and pass in the font for comparison, using the returned SizeF to calculate the size of the labels required and thus the size of the dialog.

However, when I pass a bold font to MeasureString the value returned ignores the bold style and returns a smaller value than is actually needed which typically causes the last word in the heading to be discarded (presumably it is outside the calculated area).  The size returned is always correct for the non-bold version of the text provided.

How can I cause the correct size to be returned allowing for the difference in size that the bold font requires?  Note that I have tried the overloads that accept a stringformat parameter and they make no difference to the result.

Chirs Bray
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
-------------------------------
            
SizeF headingSize;
            SizeF subHeadingSize;

            // Measure string sizes
            // Heading size 
            headingSize = MeasureString(heading, this.headingLabel.Width, headingLabel.Font);

            this.headingLabel.Size = headingSize.ToSize();

            // Subheading (size will be zero if singleMessage)
            subHeadingSize = MeasureString(subHeading, this.subHeadingLabel.Width, subHeadingLabel.Font);
 
------------------------------------------------------------       


private Size MeasureString(string str, int maxWidth, Font font)
        {
            using (Graphics g = this.CreateGraphics())
            {
                SizeF strRectSizeF = g.MeasureString(str, font, maxWidth);
                g.Dispose();

                return new Size((int)Math.Ceiling(strRectSizeF.Width), (int)Math.Ceiling(strRectSizeF.Height));
            }
        }

Answer : How can I force Graphics.MeasureString to consider the font style?


the code that worked for me for auto-sizing is below...I added a Label control with AutoSize = false with some text with font Arial 16 regular with border = fixedsingle. Then in a button click handler i had the below code....when button clicked

two things to note: i used label1.CreateGraphics() and label1.ClientSize (if it is working fine with Size, then better to use size)
This sample is from => http://msdn.microsoft.com/en-us/library/system.windows.forms.control.creategraphics.aspx
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
private void button1_Click(object sender, EventArgs e)
        {
            // Create a Graphics object for the Control.
            Graphics g = label1.CreateGraphics();

            Font nf = new Font(label1.Font, FontStyle.Bold);

            // Get the Size needed to accommodate the formatted Text.
            Size preferredSize = g.MeasureString(
               label1.Text, nf).ToSize();

            label1.Font = nf;
                        
            label1.ClientSize = new Size(preferredSize.Width,preferredSize.Height); // you could add some padding space to the width and height here

            // Clean up the Graphics object.
            g.Dispose();
        }
Random Solutions  
 
programming4us programming4us