is it possible to change fonts in a graphics drawstirng?

G

Guest

hey, I want to draw one thing, then dynamically change fonts to a different
font in the same string. can I do that?? any help ?? it needs to be using
"Graphics.Drawstring" method

myactual problem is that I want to put certain, small writing underneath
certain strings in a print preview

but since the larger text is a different font, the amount of carriage
returns is different then the smaller font. which in turn causes inconsistent
spaces between the items.

if I could however, keep the text the same font, do a bunch of carriage
returns, THEN change to the smaller font, I could make the amount of carriage
returns equal all across the page.

confused yet?

thanks.
 
P

Peter Duniho

Rogelio said:
hey, I want to draw one thing, then dynamically change fonts to a different
font in the same string. can I do that??

Yes. Just call DrawString a second time with the same parameters
(including the string), except with a different Font.
[...]
but since the larger text is a different font, the amount of carriage
returns is different then the smaller font. which in turn causes inconsistent
spaces between the items.

if I could however, keep the text the same font, do a bunch of carriage
returns, THEN change to the smaller font, I could make the amount of carriage
returns equal all across the page.

Are you using a single call to DrawString to render all of the text for
a given page? If so, then you are going to have trouble doing what you
want. You could draw two different strings, one in each font, in the
same rectangle, but getting the second string's text to get positioned
exactly where you want it is likely to be a huge headache.

A better approach would be to handle the line-breaks yourself, drawing
one line at a time. Then you have control over where each line is
drawn, and you can go back and draw smaller text relative to the line
without losing track of where the next full-size line should go.

Pete
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Rogelio said:
hey, I want to draw one thing, then dynamically change fonts to a
different
font in the same string. can I do that?? any help ?? it needs to be using
"Graphics.Drawstring" method

Yep, that will do, just call it twice. You might have to be careful about
positioning and font size though.
myactual problem is that I want to put certain, small writing underneath
certain strings in a print preview

but since the larger text is a different font, the amount of carriage
returns is different then the smaller font. which in turn causes
inconsistent
spaces between the items.

You have to position your text as you need. Take a look at MeasureString.
 
G

Guest

I think I have an idea. I will use DrawRectangle. by using Draw Rectangle, I
can set EXACT coordinates.

once the rectangles are drawn, I can use Drawstring to draw a string inside
the rectangles.

I can set the height of the rectangles to the height of the text using
GetHeight to a font object.

any advice?
 
P

Peter Duniho

Rogelio said:
I think I have an idea. I will use DrawRectangle. by using Draw Rectangle, I
can set EXACT coordinates.

You can set exact coordinates just drawing the string too.
once the rectangles are drawn, I can use Drawstring to draw a string inside
the rectangles.

You can draw a string inside a rectangle whether or not you actually
draw the rectangle.
I can set the height of the rectangles to the height of the text using
GetHeight to a font object.

It would be better to just use MeasureString() as Ignacio suggested to
find out what rectangle the string occupies. Then you can use the
dimensions of that rectangle to determine the height of the text, where
to draw the smaller text, etc.

Pete
 
B

Ben Voigt [C++ MVP]

Peter Duniho said:
You can set exact coordinates just drawing the string too.


You can draw a string inside a rectangle whether or not you actually draw
the rectangle.


It would be better to just use MeasureString() as Ignacio suggested to
find out what rectangle the string occupies. Then you can use the
dimensions of that rectangle to determine the height of the text, where to
draw the smaller text, etc.


I think there's a MeasureCharacterRanges which may suit your purposes even
better.
 

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