How to Place Static Text Labels Over Columns in a List BoxID: Q140158
|
When creating Microsoft Foundation Class (MFC) applications that contain dialog boxes, developers often find it useful to add static text labels to identify dialog controls. One particular use of static text labels is to identify contents of columns in a list box. This article describes a technique that allows placement of these labels regardless of the font used in the dialog box.
xpos = (avg_char_width * tab_stop_position) / 4.
The factor of 4 is required because each character is four dialog units
wide. Dialog units were designed around the System font, so other fonts may
not provide exact results. The accuracy depends on the value of
avg_char_width in the previous formula.
xpos = (avg_char_width * tab_stop_position + 2) / 4
For more information on how to calculate dialog units for all possible
fonts, please see the following article in the Microsoft Knowledge Base:
Q125681 How to Calculate Dialog Base Units with Non-System-Based FontArticle Q125681 describes the following technique.
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set tabstops at 100 and 200 dialog units
int tab_stop[2] = {100, 200};
VERIFY(m_listbox.SetTabStops(2, tab_stop));
m_listbox.AddString("String1\tString2\tString3");
CDC *pDC = m_listbox.GetDC();
// Get a handle to the font used in the list box
CFont *pFont;
pFont = m_listbox.GetFont();
// Select the list box font into the temporary DC
CFont *pFontOld = pDC->SelectObject(pFont);
// Call GetTextExtentPoint to compute the string dimensions
// NOTE: use GetTextExtentPoint32 in Win32 for better results
CSize size;
GetTextExtentPoint(pDC->GetSafeHdc(),
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
52, &size);
int avg_char_width = (size.cx/26 +1)/2;
// Restore Original Font
pDC->SelectObject(pFontOld);
m_listbox.ReleaseDC(pDC);
// Convert DLU to pixel positions
int xPos1 = (tab_stop[0] * avg_char_width + 2)/4;
int xPos2 = (tab_stop[1] * avg_char_width + 2)/4;
// Only concerned about x right now, so y is set to 0
CPoint mypt1(xPos1, 0);
CPoint mypt2(xPos2, 0);
// Convert the coordinates to be relative to the list box
m_listbox.ClientToScreen(&mypt1);
m_listbox.ClientToScreen(&mypt2);
// Convert the screen coordinates back to coordinates that
// are relative to the dialog box
ScreenToClient(&mypt1);
ScreenToClient(&mypt2);
// Get the current position / width of the text labels
CRect rect1,rect2;
m_text1.GetWindowRect(&rect1);
m_text2.GetWindowRect(&rect2);
// Convert the rectangles to be relative to the dialog box
ScreenToClient(&rect1);
ScreenToClient(&rect2);
// Move the text labels over the columns
m_text1.MoveWindow(mypt1.x, rect1.top,
rect1.Width(),rect1.Height());
m_text2.MoveWindow(mypt2.x, rect2.top,
rect2.Width(),rect2.Height());
return TRUE;
}
Additional query words: kbinf 1.50 1.51 1.52 2.00 2.10 2.20 4.00 1.00 2.5 2.50 2.51 2.52 3.0 3.00 3.1 3.10 3.2 3.20
Keywords : kbcode kbMFC KbUIDesign kbVC
Version : 1.00 1.50 1.51 1.52 | 2.00 2.10
Platform : NT WINDOWS
Issue type :
Last Reviewed: August 8, 1999