views:

689

answers:

4

Can you measure the width of a string more exactly in WIN32 than using the GetTextMetrics function and using tmAveCharWidth*strSize?

A: 
Kirill V. Lyadvinsky
+6  A: 

Try using GetTextExtentPoint32. That uses the current font for the given device context to measure the width and height of the rendered string in pixels.

Nick Meyer
Thanks, this is what i was lokking for. :)
Razvi
+1  A: 

Depending on how you are using this, you can use DrawText with DT_CALCRECT specified and it will (its always done it fairly accurately for me) calculate the size of the required rectangle based on the text/font/etc.

DeusAduro
+4  A: 

I don't know for certain, but it seems that:

RECT r = { 0, 0, 0, 0 };
char str[] = "Whatever";
DrawText(hDC, str, strlen(str), &r, DT_CALCRECT);

might work.

Evan Teran
Thanks, tried it and shows same width as GetTextExtentPoint32 :).
Razvi