I want to find the exact height of the text displayed on Windows. I tried both GetTextExtentPoint32 and calling DrawText with the DT_CALCRECT flag, and both give the same result.
The returned height seems to be based on the full height of the cell, regardless of the actual text to be drawn.
The code below is the WM_PAINT handler for the standard Win32 Visual Studio 2013 Win32 project. He creates a (large) font and draws a sample text. The highest part of the text is 98 pixels, but the value returned by GetTextExtentPoint32 is 131.
I understand that some applications may require the full height of the cell, but also some applications (like mine) just want the actual height to be used by the text.
Does anyone know how to find this information?
Yes, I can display DC and scan down, looking for the first non-phonic color pixel, but it will be very slow.
thanks
case WM_PAINT: { hdc = BeginPaint (hWnd, &ps); HFONT hfont = CreateFont (-99, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH, L"Segoe UI Semibold"); auto old_hfont = SelectObject (hdc, hfont); wchar_t sample_text[] = L"123 Testing 123"; size_t sample_text_length = wcslen (sample_text); SIZE s; GetTextExtentPoint32 (hdc, sample_text, sample_text_length, &s); RECT r = {10, 10, 10 + s.cx, 10 + s.cy}; SetBkColor (hdc, RGB (80, 120, 160)); SetTextColor (hdc, RGB (220, 220, 220)); DrawText (hdc, sample_text, sample_text_length, &r, DT_SINGLELINE | DT_NOPREFIX | DT_LEFT | DT_TOP); SelectObject (hdc, old_hfont); DeleteObject (hfont); EndPaint (hWnd, &ps); break; }