1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
long getTextLength(HWND hwndListBox, LPCTSTR lpszText)
{
//Use GetDC to retrieve the handle to the display context for the list box
//and store it in hDCListBox:
HDC hDCListBox = ::GetDC(hwndListBox);
//Send the list box a WM_GETFONT message to retrieve the handle to the font
//that the list box is using, and store this handle in hFontNew:
HFONT hFontNew = (HFONT) ::SendMessage(hwndListBox, WM_GETFONT, NULL, NULL);
//Use SelectObject to select the font into the display context. Retain the
//return value from the SelectObject call in hFontOld:
HFONT hFontOld = (HFONT) SelectObject(hDCListBox, hFontNew);
//Call GetTextMetrics to get additional information about the font being used:
TEXTMETRIC tm;
GetTextMetrics(hDCListBox, (LPTEXTMETRIC)&tm);
//For each string, the value of the extent to be used is calculated as follows:
SIZE sizeRect;
::GetTextExtentPoint32(hDCListBox, lpszText, strlen(lpszText), &sizeRect);
sizeRect.cx += tm.tmAveCharWidth;
//After all the extents have been calculated, select the old font back into
//hDCListBox and then release it:
SelectObject(hDCListBox, hFontOld);
::ReleaseDC(hwndListBox, hDCListBox);
return sizeRect.cx;
} |
Partager