3

Today I am trying to draw a string on a form, but need to be drawn exactly in the middle, I have sought functions but I do not see the parameters of the rectangle centered or obtained automatically.

If someone would kindly give me some function in which to center the text automatically. The commands I use are:

 gfx.DrawString(line, printFont, myBrush, leftMargin, YPosition(), new StringFormat());

Thanks for your Help

0

1 Answer 1

4

You can set parameters using the StringFormat object you're passing in.

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;      // Horizontal Alignment
stringFormat.LineAlignment = StringAlignment.Center;  // Vertical Alignment

How to: Align Drawn Text

Sign up to request clarification or add additional context in comments.

3 Comments

I Tried this Code, But Te Rectangle is not obtained Automatically in the form (Rectangle rect1 = new Rectangle(10, 10, 130, 140);), I need Auto detect the Form Size.
Yes, If you see first Create an Rectangle and the size parameters is not obtained automatycally. I need Get the X,Y size form the form
e.Graphics.DrawString(text1, font1, Brushes.Blue, rect1, stringFormat) The Rect1 = Form Size, do you understand me?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.