|
-
Dec 23rd, 2007, 02:22 AM
#1
Thread Starter
Frenzied Member
[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
-
Dec 23rd, 2007, 04:27 AM
#2
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.
-
Dec 23rd, 2007, 04:32 AM
#3
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.
-
Dec 23rd, 2007, 05:17 AM
#4
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.
-
Dec 23rd, 2007, 05:29 PM
#5
Thread Starter
Frenzied Member
Re: [2.0] An accurate formula to measure the length of a string (drawing)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|