1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
//Use GetDC to retrieve the handle to the display context for the list box
//and store it in hDCListBox:
HDC hDCListBox = GetDC(hwnd);
//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(hwnd, 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:
TEXTMETRICS tm;
GetTextMetrics(hDCListBox , (LPTEXTMETRIC)&tm);
//For each string, the value of the extent to be used is calculated as follows:
DWORD dwExtent = GetTextExtent(hDCListBox, lpszText, strlen(lpszText))
+ tm.tmAveCharWidth;
//After all the extents have been calculated, select the old font back into
//hDCListBox and then release it:
SelectObject(hDCListBox, hFontOld);
ReleaseDC(hwnd, hDCListBox);
return dwExtent; |
Partager