Tech Support > Microsoft Windows > Development Resources > Should GetTextExtentPoint32 correctly measure treeview item text?
Should GetTextExtentPoint32 correctly measure treeview item text?
Posted by dan on February 3rd, 2007


I create an ordinary treeview (no TVS_EDITLABELS) (does have
checkboxes to left of each item) and an ordinary window. I notice the
treeview item text is smaller than the ordinary window text, which is
fine.

I'd like to size the treeview width at runtime, based on the longest
string to be displayed as a treeview item, and taking into account the
indent width and checkbox width.

However, when I use GetTextExtentPoint32 to measure a string, it
reports the same width whether the device context is from a treeview
or ordinary window. For example, a 45 character string is reported as
extent width of 312 in either case, although the displayed size
(window text; treeview item) differs. So the treeview ends up too
wide.

Should GetTextExtentPoint32 correctly measure treeview item text, and
I'm doing something wrong? Or do I need to do something else? Maybe
fill the treeview with items, measure each item with TVM_GETITEMRECT
(just measuring the text), and then resize the treeview width?

I'm sorry I don't have a compilable code example. It's all embedded
within a large application. Here are the two cases in skeletal format.

// Measure string for ordinary window
hdc = GetDC (hwnd);
GetTextExtentPoint32 (hdc, string, chars, &size);
ReleaseDC (hwnd, hdc);

// Measure string for treeview
hdc = GetDC (htree);
GetTextExtentPoint32 (hdc, string, chars, &size);
ReleaseDC (htree, hdc);

The code is all C. I compile under windows 2000 using: gcc 3.4.2
(mingw-special)

Thanks,
Daniel Goldman

Posted by Norman Bullen on February 3rd, 2007


dan wrote:
select it into the DC. It's not selected automatically.

With most controls you can get the font with
hFont = (HFONT)SendMessage(hwndControl, WM_GETFONT, 0, 0);
(I've never tried that with a TreeView but I have no reason to believe
it won't work.)

Then you select it into the DC with
hFont2 = (HFONT)SelectObject(hDC, hFont);

When you're finished, remember to select the original font back into the
DC before you release it.
SelectObject(hDC, hFont2);

Norm

--
--
To reply, change domain to an adult feline.


Posted by dan on February 4th, 2007


On Feb 3, 12:25 pm, Norman Bullen
<n...@BlackKittenAssociates.com.INVALID> wrote:
Thanks so much. It worked exactly as you said.

When I get the font the TreeView control is using and select it into
the DC, GetTextExtentPoint32 returns the correct (smaller) extent. I
didn't realize the font was not automatically selected into the device
context.

Daniel Goldman



Similar Posts