2

I need to get the space required for a string (custom widget stuff - so legitimate use!), it is needed before any draw calls so I haven't got a paint DC (yet) to call http://docs.wxwidgets.org/trunk/classwx_d_c.html#ae55cbf1bc7b7e836cb192eb48d31efab on

Now I'd have thought that the space required would be a function of the font, not the DC, but I haven't found a way yet.

I did find some wxPython answers (which isn't a bad thing, the two are easy to switch between!) but they involved getting a DC then calling GetTextExtent on it.

There must be an efficient way!

(I am using wxWidgets 2.8)

Another "answer"

http://forums.wxwidgets.org/viewtopic.php?t=17143&p=74448

It creates a DC also

Another, The right way to find the size of text in wxPython

That one creates a wxDC, I can't do that as it is an abstract base class (rightfully so ofc)

Community
  • 1
  • 1
Alec Teal
  • 5,770
  • 3
  • 23
  • 50
  • 3
    You absolutely need a DC to calculate this, because the answer changes when the DC is attached to a printer vs. a screen. – Mark Ransom Jul 16 '14 at 16:30

1 Answers1

2

You can create a wxClientDC at any point if you need it just for measurement. And GetTextExtent() is the most efficient way to measure a single string. For multiple strings, GetPartialTextExtents() can be more useful.

VZ.
  • 21,740
  • 3
  • 39
  • 42