problem with graphics.DrawString

A

Allen

I've got a control that you can resize the contents of one of the text
fields inside it. When the contents are resized to smaller than the
text, I remove some of the end of the text and substitute ... at the
end.

This is where the wierdness sets in. Any time I remove or change some
letters at the END of the text and draw, the spacing throughout the
text string changes!!! So when I resize it in and out, the whole line
looks like its expanding and contracting.

I am using Graphics.DrawString with mostly default values. My font is
Sans Serif 8.25. I've checked the microsoft .net listview and it
doesn't show this behavior with this same font so I'm trying to figure
out what they are doing that I am not!

Why is my text line expanding and contracting as it's changed near the
end?
 
F

Fredo

I'm not exactly sure why, but I think the way around this is to use GDI's
DrawText. In our own custom controls, we ran into various problems with
Graphcis.DrawString and switched everything to DrawText. It's a bit of a
paint to have to deal with GDI, but it's definitely more reliable.
 
M

Michael C

Allen said:
I've got a control that you can resize the contents of one of the text
fields inside it. When the contents are resized to smaller than the
text, I remove some of the end of the text and substitute ... at the
end.

This is where the wierdness sets in. Any time I remove or change some
letters at the END of the text and draw, the spacing throughout the
text string changes!!! So when I resize it in and out, the whole line
looks like its expanding and contracting.

I am using Graphics.DrawString with mostly default values. My font is
Sans Serif 8.25. I've checked the microsoft .net listview and it
doesn't show this behavior with this same font so I'm trying to figure
out what they are doing that I am not!

Why is my text line expanding and contracting as it's changed near the
end?

I think this is caused when you specify too small a rectangle for it to draw
into. If you just make the rectangle a bit longer it should work fine.
 
A

Allen

for anyone who has this problem in the future I figured out what the
problem was. It's a very obscure property in the graphics object
called TextRenderingHint. Setting this like so

e.Graphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;


at the beginning of my OnPaint method of my control fixes the problem
%100.
 

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