Click here to Skip to main content
15,889,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have problem in setting up font attributes to CStatic control. I have static control where I am setting user name when logged in using SetWindowText. I have trapped mouse hover and changing the color of it. I am handling OnMouseMove,OnMouseHover and OnMouseLeave events. I am drawing text in OnPaint using DrawText / DrawTextEx. But I want to set the font to it and also want to align the text vertically centered. But I am unable to make it.
Pls suggest how to handling Font and Alignment of control text.

DrawTextEx(pDC->GetSafeHdc(),text.GetBuffer(),text.GetLength(),rc,DT_EDITCONTROL | DT_CENTER | DT_VCENTER,NULL);


Regds
SNI
Posted

1 solution

You need to create a font[^] and then select it into the DC[^], and set the text alignment[^], before drawing the text. Also, if you are using MFC then use the class object methods (e.g CDC::DrawText[^]) rather than the pure Win32 functions.
 
Share this answer
 
Comments
SNI 4-Oct-13 5:38am    
Thanks it works. I tried to align text vertically centered but that is not working.
Richard MacCutchan 4-Oct-13 5:48am    
What do you mean by "that is not working"? You need to show the code you use and explain what results you expect and what results you achieve.
SNI 4-Oct-13 5:57am    
I was able to set the font of the text and also able to align text horizontally centered. I also want to align text vertically centered that is not working...
following is the code snippet.

CDC* pDC = GetWindowDC();
pDC->SetBkMode(TRANSPARENT);
pDC->SelectObject(m_font);
pDC->FillRect( &rect, &brush);
DrawTextEx(pDC->GetSafeHdc(),text.GetBuffer(),text.GetLength(),rect,DT_EDITCONTROL | DT_CENTER | DT_VCENTER,NULL);
Richard MacCutchan 4-Oct-13 6:09am    
See the documentation here, particularly the comments for DT_VCENTER.
SNI 5-Oct-13 1:36am    
Thanks....it worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900