Link to home
Start Free TrialLog in
Avatar of pbsmbc
pbsmbc

asked on

dialog caption

Hi,

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.

Thanks
pbsmbc
Avatar of minnirok
minnirok

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);

     DeleteObject(rgn);
}

https://www.experts-exchange.com/questions/21375994/DrawText-DT-CENTER-DT-SINGLELINE-DT-VCENTER.html?query=center+caption&clearTAFilter=true
ASKER CERTIFIED SOLUTION
Avatar of minnirok
minnirok

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial