wxString text = "Hello, world!"; wxDC* dc = new wxClientDC(); wxCoord w, h; dc->GetTextExtent(text, &w, &h); delete dc;
wxString text = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit."; wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL); wxMemoryDC dc; dc.SetFont(font); wxSize size = dc.GetTextExtent(text);In this example, we create a wxString object containing the text we wish to measure. We then create a new wxFont object with a size of 12 points and the default family, style, and weight. Next, we create a wxMemoryDC object, which represents a device context for off-screen drawing operations. We set the font of the device context to our custom font using the SetFont function. Finally, we call the GetTextExtent function on the device context, passing in the text. The function returns a wxSize object containing the width and height of the text. Package library: wxWidgets