Drawing an image at the end of a string

  • Thread starter Thread starter rpatel4
  • Start date Start date
R

rpatel4

I'm drawing text to the screen, and I need to place an image at the end
of the text.
The text is multiline.
Here's how I'm drawing the text

e.Graphics.DrawString(_text, this._font, textBrush, textRectangle);

So On screen it appears like
--------------------------------------
| There is some text |
| on this screen. It |
| needs an image |
| right here *** |
----------------------------------
where *** is an image (whose height is the same as the line height)
(pardon the lack of formatting)
I'm open to putting the text in some kind of container instead of
drawing it outright to the screen.
The question is, what containers/etc can I used to get the image drawn
directly at the end
of the string.
And if theres no containers, is there a way to measure the width of the
last line of text?
 
I'm not sure what you mean by putting it into somekind of container, why
would you want to do that?

The easiest way to do what you want to do is to inherit from the UserControl
class if using CF2.0 then override the OnPaint event to paint your text
and image then use the Grpahics.MeasureString method to calculate the
height/width. Note; many peoples code I see
simply calculate the width of "A" using a specified font then multiple this
by the length, but to get a true width/height you need to calculate the
width of each character if not using a fixed width font.

Regards
Simon.
 
Hi,

Thanks for the reply. I'm a c#/.net/windows newbie (seasoned
c++/embedded developer), so I might not have the lingo down.

By 'somekind of container', I meant if there's a control (such as a
label) that would take care of the drawing/placement for me I'd go that
route.

Now the measurestring will tell me the height/width of the entire
string, but will it tell me the width of the last line (since the
picture will be going at the end of the last character which could be
anywhere on the last line).

It looks like I can use solution #1 from here, but i'm not sure if its
really optimal


I'm using .net 1.1 btw, but I'll look into overrividing the usercontrol
(if it exists in 1.1)
 
By 'somekind of container', I meant if there's a control (such as a
label) that would take care of the drawing/placement for me I'd go that
route.

Now the measurestring will tell me the height/width of the entire
string, but will it tell me the width of the last line (since the
picture will be going at the end of the last character which could be
anywhere on the last line).

As I said, you need to calculate each character if not fixed length
character set in
order to calculate a whole string.
It looks like I can use solution #1 from here, but i'm not sure if its
really optimal


I'm using .net 1.1 btw, but I'll look into overrividing the usercontrol
(if it exists in 1.1)

There was never a 1.1 version of CF. 2.0 and onwards is the only version
that supports the
UserControl class. If using 1.0 (VS 2003) then you will need to inherit from
Control and handle complete painting
yourself.

Regards
Simon.
 
Back
Top