How to Place Static Text Labels Over Columns in a List BoxLast reviewed: November 10, 1997Article ID: Q140158 |
1.00 1.50 1.51 1.52 | 2.00 2.10 2.20 4.00
WINDOWS | WINDOWS NTkbui kbcode kbgraphic kbhowto The information in this article applies to:
SUMMARYWhen 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.
MORE INFORMATION
Steps to Create Columns in a List Box
How to Place the Static Text LabelsTo place the static text labels, you need to convert the tab stops to pixel locations in the client area of the dialog box, and move the static text labels to the appropriate location. For this example, you need only be concerned with the horizontal position of each column. The horizontal position is calculated according to the following formula:
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. Windows rounds up when it calculates tab stop positions, so the previous formula needs the following modification to force it to round up too:
xpos = (avg_char_width * tab_stop_position + 2) / 4For more information on how to calculate dialog units for all possible fonts, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q125681 TITLE : How to Calculate Dialog Base Units with Non-System-Based FontArticle Q125681 describes the following technique. Note that if the font is a fixed-width font, you could just use GetTextMetrics to obtain the average character width of the font, but that will only be accurate for fixed-width fonts, so the general-purpose technique, which works for fixed-width and variable-width fonts, is used below. Use the following steps to create a list box with columns and static text labels placed above each column:
Sample CodeBOOL 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 reference words: kbinf 1.50 1.51 1.52 2.00 2.10 2.20 4.00 1.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |