[2.0] An accurate formula to measure the length of a string (drawing)
I am painting on my buttons, however the painting erases the text on them, so I am trying to use 'DrawString' to write it back on them...
However, I need to accurately measure the length of what the string will be when it is drawn to the button... I also can't specifically position the text on the button because I have an array of multiple buttons, with different text...
I currently have:
Code:
e.Graphics.DrawString(button.Text, new Font(button.Font.OriginalFontName, button.Font.Size), Brushes.Red, new Point(0, 0));
I've tried font.Height works perfectly for the height of the string, but font.Width * button.Text.Length, does not work properly?
Any idea's?
Thanks
Re: [2.0] An accurate formula to measure the length of a string (drawing)
From a previous thread of yours I assume that this code is being executed inside a class derived from Button. In that case there's no need to do what you're doing. Just override the OnPaintBackground method instead of OnPaint.
That said, for future reference you have the Graphics.MeasureString and .MeasureCharacterRanges methods. I believe that MeasureCharacterRanges is more reliable in some situations.
Re: [2.0] An accurate formula to measure the length of a string (drawing)
Hmmm... maybe I lied about that. Just tried OnPaintBackground and the drawing didn't appear. I'll dig around a bit more and see if I'm missing something.
Re: [2.0] An accurate formula to measure the length of a string (drawing)
Yeah, it looks like I was lying. :( I looked around a bit and I don't think OnPaintBackground is for what I thought it was for. Ah well, my own GDI+ experience went up a little over b*gger all as a result, so it was worthwhile.
Re: [2.0] An accurate formula to measure the length of a string (drawing)