Drawing text without an offset

J

John B

I need to create a label with the last character drawn in red.
My idea was to create a label and override OnPaint.
So far so good.
I then measure the character range for the last character.
All good.
Then when I call drawstring and pass the rectangle I get via
Region.GetBounds(Graphics) it draws it with the same offset as the
beginning of the label.
Not good.

Question is, how can I stop it from doing this?
TextRenderer.DrawText does the same thing.

Failing this, is there another way I should be doing this?

The region is correct as when I fill the region and draw the whole
string normally, the last character falls smack into the filled region.


Cheers
JB
 
K

Kevin Spencer

I'm not sure why you're using a Region here. The PaintEventArgs class has a
property called ClipRectangle that defines the drawing area. You can use
Graphics.MeasureString to find the size of the rectangle that the first part
of the string will occupy, then combine the X and Y properties of that
Rectangle added to the X and Y properties of the ClipRectangle to define
where to draw the last character.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

This is, by definition, not that.
 
J

John B

Kevin said:
I'm not sure why you're using a Region here. The PaintEventArgs class has a
property called ClipRectangle that defines the drawing area. You can use
Graphics.MeasureString to find the size of the rectangle that the first part
of the string will occupy, then combine the X and Y properties of that
Rectangle added to the X and Y properties of the ClipRectangle to define
where to draw the last character.
If you have a look at the doc's for measurestring it specifically says
it is approximate only and to get more accurate measure, use
measurecharacterranges which returns an array of characterranges from
which I then get the region.
I would still have the same problem no matter what I used to calculate
where to paint. Where to paint is not a problem, where it _will_ paint
is the problem.
If I paint a dot @ 10,10 then draw a string at 10,10 the dot is offset
back and up by ?, ?.
That is the problem.

Cheers
JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top