Drawing text without an offset - Ping Bob Powell (Repost)

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.

What I really need to know is the offset that graphics.drawstring prints
the first character at.


Bob, I know from your (excellent) GDI+ faq you have extensive experience
with graphics.
Any ideas?

Cheers
JB
 
M

Michael C

John B said:
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.

You could always use the same method to measure this space at the start and
subtract it from your rect. Set your first character range from 0 to 1, get
the rect for this chr range and subtract the left of this rect off the rect
for the last chr.

Michael
 
M

Michael C

John B said:
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 other option is to draw the entire text twice, once in each color using
Graphics.SetClip to set the clipping region each time. Possibly not the most
efficient method but for a label it won't make any difference.

Michael
 
J

John B

Michael said:
The other option is to draw the entire text twice, once in each color using
Graphics.SetClip to set the clipping region each time. Possibly not the most
efficient method but for a label it won't make any difference.

Michael
Good idea(s).
Thanks :)

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