CDC::GetTextExtent() and CWnd::GetClientRect()
-
Hi Quick question - am I comparing like for like if I do the following in a CEdit derived class? I am trying to determine whether or not the text will be completely visible when placed in the edit control. This code indicates that the text will not be completely visible, even though it quite clearly is!
void CMyEdit::DoSomething(const CString & strText) { CRect rectText; GetClientRect(rectText); CDC * pDC = GetDC(); CSize sizeExtent = pDC->GetTextExtent(strText); if (sizeExtent.cx > rectText.Width()) // Is this valid? { // Text too large } ReleaseDC(pDC); }
Thanks in advance! -
Hi Quick question - am I comparing like for like if I do the following in a CEdit derived class? I am trying to determine whether or not the text will be completely visible when placed in the edit control. This code indicates that the text will not be completely visible, even though it quite clearly is!
void CMyEdit::DoSomething(const CString & strText) { CRect rectText; GetClientRect(rectText); CDC * pDC = GetDC(); CSize sizeExtent = pDC->GetTextExtent(strText); if (sizeExtent.cx > rectText.Width()) // Is this valid? { // Text too large } ReleaseDC(pDC); }
Thanks in advance!I just did a quick lookup on the GetTextExtent function and noticed that there's another one called GetOutputTextExtent, which may be the one you need. Regards, Alvaro
All you need in this life is ignorance and confidence, and then success is sure. -- Mark Twain
-
Hi Quick question - am I comparing like for like if I do the following in a CEdit derived class? I am trying to determine whether or not the text will be completely visible when placed in the edit control. This code indicates that the text will not be completely visible, even though it quite clearly is!
void CMyEdit::DoSomething(const CString & strText) { CRect rectText; GetClientRect(rectText); CDC * pDC = GetDC(); CSize sizeExtent = pDC->GetTextExtent(strText); if (sizeExtent.cx > rectText.Width()) // Is this valid? { // Text too large } ReleaseDC(pDC); }
Thanks in advance!When you call GetDC() you get a DC with a default font in it. This will (most probably) not be the font being used by the edit control. You need to use that font when measuring your text. Check the result of a call to CEdit::GetFont(). Roger Allen Sonork 100.10016 WHats brown and sticky? A stick or some smelly stuff!
-
When you call GetDC() you get a DC with a default font in it. This will (most probably) not be the font being used by the edit control. You need to use that font when measuring your text. Check the result of a call to CEdit::GetFont(). Roger Allen Sonork 100.10016 WHats brown and sticky? A stick or some smelly stuff!
Thanks for that - calling CEdit::GetFont() and passing the result to CDC::SelectObject() before doing the comparison worked a treat!