static text box , GetClientRect, GetTextExtent [modified]
-
CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); cs2 fits the width of the static control MessageBox reports 260 - 178 because the text is the same width as the static control I was expecting size.cx == stbRect.Width() Where am i going wrong ? -- modified at 11:19 Wednesday 16th May, 2007
-
CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); cs2 fits the width of the static control MessageBox reports 260 - 178 because the text is the same width as the static control I was expecting size.cx == stbRect.Width() Where am i going wrong ? -- modified at 11:19 Wednesday 16th May, 2007
-
CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); cs2 fits the width of the static control MessageBox reports 260 - 178 because the text is the same width as the static control I was expecting size.cx == stbRect.Width() Where am i going wrong ? -- modified at 11:19 Wednesday 16th May, 2007
CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); If the static control is from a dialog, it may use a different font. staticTextBox.GetDC() is returning a DC for the control but you've selected no font into it before calculating the text extents. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); cs2 fits the width of the static control MessageBox reports 260 - 178 because the text is the same width as the static control I was expecting size.cx == stbRect.Width() Where am i going wrong ? -- modified at 11:19 Wednesday 16th May, 2007
Whats problem ?
WhiteSky
-
CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); If the static control is from a dialog, it may use a different font. staticTextBox.GetDC() is returning a DC for the control but you've selected no font into it before calculating the text extents. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
>> staticTextBox.GetDC() is returning a DC for the control but you've selected no font into it Greetings Mark I now have this ... void CmyAppDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here //OnOK(); CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CFont * font = GetDC()->GetCurrentFont(); staticTextBox.SetFont(font,1); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); } Now when the text fills the width of the static control textSize.cx equals stbRect.Width(). This is a good thing . I am setting the font of the control to the same font used by GetTextExtent . How may I instead , set the font used by GetTextExtent to the same font used by the static control ? Thanks for reading this .
-
>> staticTextBox.GetDC() is returning a DC for the control but you've selected no font into it Greetings Mark I now have this ... void CmyAppDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here //OnOK(); CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); CFont * font = GetDC()->GetCurrentFont(); staticTextBox.SetFont(font,1); CSize textSize = staticTextBox.GetDC()->GetTextExtent(cs2); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); } Now when the text fills the width of the static control textSize.cx equals stbRect.Width(). This is a good thing . I am setting the font of the control to the same font used by GetTextExtent . How may I instead , set the font used by GetTextExtent to the same font used by the static control ? Thanks for reading this .
Here's an example - using the dialog's font... void CmyAppDlg::OnBnClickedOk() { CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); // Get a DC for the static control //*edit* this could be "CClientDC ClientDC(this);" //*edit* (any window/screen-compatible DC will do) CClientDC StaticDC(&staticTextBox); // Select the dialog's font into it CFont *pOldFont = StaticDC.SelectObject(GetFont()); // Get the text extent based on the dialog font CSize textSize = StaticDC.GetTextExtent(cs2); // restore the DC StaticDC.SelectObject(pOldFont); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); } "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder -- modified at 14:29 Wednesday 16th May, 2007
-
Here's an example - using the dialog's font... void CmyAppDlg::OnBnClickedOk() { CRect stbRect; CString cs2; staticTextBox.GetClientRect(stbRect); staticTextBox.GetWindowTextW(cs2); // Get a DC for the static control //*edit* this could be "CClientDC ClientDC(this);" //*edit* (any window/screen-compatible DC will do) CClientDC StaticDC(&staticTextBox); // Select the dialog's font into it CFont *pOldFont = StaticDC.SelectObject(GetFont()); // Get the text extent based on the dialog font CSize textSize = StaticDC.GetTextExtent(cs2); // restore the DC StaticDC.SelectObject(pOldFont); cs2.Format((_T("%d - %d")), textSize.cx, stbRect.Width()); AfxMessageBox(cs2,0,0); } "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder -- modified at 14:29 Wednesday 16th May, 2007
Thank you Mark ! You have made many things very clear . I have this now :) CRect stbRect; CString stbStr; staticTextBox.GetClientRect (stbRect); staticTextBox.GetWindowTextW(stbStr); CClientDC stbCDC(&staticTextBox);; CFont *pOldFont = stbCDC.SelectObject(GetFont()); CSize textSize = stbCDC.GetTextExtent(stbStr); stbCDC.SelectObject(pOldFont); int stbLines = stbRect.bottom / textSize.cy ; stbStr = _T("this is on the top line"); for(int i=1 ; i
-
Thank you Mark ! You have made many things very clear . I have this now :) CRect stbRect; CString stbStr; staticTextBox.GetClientRect (stbRect); staticTextBox.GetWindowTextW(stbStr); CClientDC stbCDC(&staticTextBox);; CFont *pOldFont = stbCDC.SelectObject(GetFont()); CSize textSize = stbCDC.GetTextExtent(stbStr); stbCDC.SelectObject(pOldFont); int stbLines = stbRect.bottom / textSize.cy ; stbStr = _T("this is on the top line"); for(int i=1 ; i
Cool does it work like you need it to? I was never sure what you were up to :) Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder