We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


dialog caption

pbsmbc asked
Medium Priority
Last Modified: 2013-11-20

How do I make a dialog caption in the center of the dialog?
The problem we are having if we manually center the caption of the dialog it works in windows XP however, in NT it's
moving to the right.

Watch Question

How are you manually drawing it at the moment? And what do you mean by it doesn't work under NT, it's not really centered, won't draw at all, etc?

Here's a draw text function for drawing centered text within a rect:

void DrawText(CDC* pdc, const CString& strText, CRect& rcRect)
     CRect rcSavedRect = rcRect;
     CRect rcNewRect = rcRect;
     CRgn rgn;

     rgn.CreateRectRgn(rcRect.left, rcRect.top, rcRect.right, rcRect.bottom);

     // Call DrawText with DT_WORDBREAK | DT_CALCRECT flags, this will adjust the rectagle size
     // after breaking it into lines but without drawing

     pdc->DrawText(strText, &rcSavedRect,  DT_WORDBREAK | DT_CALCRECT | DT_CENTER | DT_VCENTER);

     // Modify the caption rectagle according to the new coordinates
     rcNewRect.top = rcRect.top + (rcRect.Height() - rcSavedRect.Height()) / 2;
     rcNewRect.bottom = rcRect.top + rcSavedRect.Height();
     HRGN hrgnOld = NULL;
     ::GetClipRgn(pdc->m_hDC, hrgnOld);

     pdc->SelectClipRgn(&rgn, RGN_COPY);

     // Draw the caption
     pdc->DrawText(strText, &rcNewRect, DT_WORDBREAK | DT_CENTER | DT_VCENTER);

     ::SelectClipRgn(pdc->m_hDC, hrgnOld);



Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.