Right-aligning text with CDC::SetTextAlign

20 November, 2008 § Leave a comment

Aligning text in an MFC application dialog can be done in many ways. I’ve seen some that calculate the width of the text it is going to print and then recalculate the CRect that the text should be placed it to give it the look of being aligned.

Today I was trying to do just that and was looking through the MSDN documentation for the different method calls when I stumbled upon CDC::SetTextAlign. The MSDN documentation reads:

UINT SetTextAlign(
   UINT nFlags
);

TA_RIGHT   Aligns the point with the right side of the bounding rectangle.
TA_CENTER   Aligns the point with the horizontal center of the bounding rectangle.
TA_LEFT   Aligns the point with the left side of the bounding rectangle. This is the default setting.

Specifies text-alignment flags. The flags specify the relationship between a point and a rectangle that bounds the text. The point can be either the current position or coordinates specified by a text-output function. The rectangle that bounds the text is defined by the adjacent character cells in the text string. The nFlags parameter can be one or more flags from the following three categories. Choose only one flag from each category. The first category affects text alignment in the x-direction:

My quick and dirty read told me that if I wanted to right-align text, I should just set the CRect.right property, call SetTextAlign( TA_RIGHT), and draw the text. This didn’t work as expected though. I learned that the text align actually works with the rectangle like below:

text-align1

It’s not what it sounded like, and not what I expected. I hope this helps anyone else trying to figure out why the text-align isn’t working correctly.

Tagged: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Right-aligning text with CDC::SetTextAlign at JAWS.

meta

Follow

Get every new post delivered to your Inbox.

Join 1,003 other followers