Article ID: 140158
Article Last Modified on 11/21/2006
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) / 4For more information on how to calculate dialog units for all possible fonts, please see the following article in the Microsoft Knowledge Base:
125681 How to Calculate Dialog Base Units with Non-System-Based Font
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 kbctrl kbhowto kblistbox kbnoupdate kbtooltip kbuidesign KB140158