How to get the Top Left coordinates when drawing text

S

Satya

Hi,
I am new to GDI+ in C# and so excuse me if this sounds silly. I need
to draw text at different places on the form (mainly TopLeft, TopRight,
BottomLeft and BottomRight). I can use either DrawString or DrawText
methods to do this. My problem is how do I calculate the TopLeft point
that I need to supply to these methods? In case we need to pass a
Rectangle parameter how do I calculate the UpperLeft point, width and
height of the Rectangle?

Do I have to do this manually by calculating the width of the string
(which can be multiline) and then based on my desired location
calculate the point or are there any methods in the SDK that do this
for us? Also if anyone has done this before some sample code would be
very much appreciated.

thanks,
Satya.
 
K

Kevin Spencer

Hi Satya,

Use the Graphics.MeasureString method to get the dimensions of the text
rectangle. The TopLeft, TopRight, BottomLeft and BottomRight corners of a
ClientRectangle (the rectangle in which you want to draw the text)
correspond to (0,0), (Width, 0), (0, Height), and (Width, Height)
respectively. To calculate the upper-lefthand corner of the text rectangle
relative to the ClientRectangle, you would use (employing upper-case to
represent the ClientRectangle, and lower-case to represent the text
rectangle), you would use (0,0), (Width - width, 0), (0, Height - height),
and (Width-width, Height-height) respectively.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 

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