NO

Author Topic: Center button-text  (Read 2247 times)

pitter2206

  • Guest
Center button-text
« on: February 07, 2011, 03:36:41 PM »
Hi there,
I try to center text which has sometimes 2 lines.

So I use DrawText with DT_WORDBREAK and  DT_CALCRECT. That would be fine, if the button text would be centered.

I tried this to fix it:

Code: [Select]
GetClientRect(hwnd, & rc);
SetRect(&rc, 0, 0, bm.bmWidth, bm.bmHeight);


rcT.bottom = DrawText(lpDrawItem->hDC, buttons[nIdx].text, lstrlen(buttons[nIdx].text), &rc, DT_WORDBREAK | DT_CALCRECT);

int textHeight = bm.bmHeight/2 - rcT.bottom/2 ; //calc the half of the difference between bm.bmWidth and the result of rct.bottom to use it as int yTop in rcT

SetRect(&rcT, 50, textHeight, bm.bmWidth, bm.bmHeight);

DrawText(lpDrawItem->hDC, buttons[nIdx].text, lstrlen(buttons[nIdx].text), &rcT, DT_WORDBREAK | DT_LEFT);


The result is, that some buttons are centered and some not.  ???

Where is my missunderstanding??? Could you please give me a hint????
« Last Edit: February 07, 2011, 04:13:21 PM by pitter2206 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2094
Re: Center button-text
« Reply #1 on: February 07, 2011, 07:46:34 PM »
Remove this GetClientRect(hwnd, & rc);
Use same width:
Code: [Select]
SetRect(&rc, 50, 0, bm.bmWidth, bm.bmHeight);
May the source be with you

pitter2206

  • Guest
Re: Center button-text
« Reply #2 on: February 07, 2011, 08:44:34 PM »
arghhhh....

I think I had to see that myself....  :'(

Thank you!