GetTextExtend and DrawRotatedText

Laurent Humbertclaude laurent.humbertclaude at gmail.com
Mon Apr 2 08:07:29 PDT 2007


Hi wxUsers,


I am trying to draw some rotated text with precision on a wxMemoryDC and wxDC.
By using GetTextExtend to get the width and height of the text, I want
to transform the bounding box to emulate a GetRotatedTextExtend.

Unfortunately, rendering some rotated text gives wrong size compared
to the width of the string. The string drawn is of the same size when
using a rotation of 0, but the text is longer when drawn rotated.

See screen shot:
[wxWidget 2.6.3 - 2.4.2, VC6 and VC8 on winXP SP2]
http://img251.imageshack.us/img251/1853/gettextextend2be2.png

The cyan circle is centered on text draw point and the radius is set
to the width returned by GetTextExtend().

Place this code snippet on a OnPaint()
{
	double angle = 20.0;
	wxCoord x, y;
	dc.SetFont(*wxSWISS_FONT);
	wxString testString = wxString::Format("wxSWISS_FONT text rotated of
%f deg", angle);
	wxPoint textPos(50,200);
	dc.GetTextExtent(testString, &x, &y);
	wxRect bbox(0,0,x, y);
	bbox.Offset(textPos);
	dc.SetPen(wxPen(*wxRED_PEN));
	dc.SetBrush(*wxTRANSPARENT_BRUSH);
	dc.DrawRectangle(bbox);
	dc.SetPen(wxPen(*wxCYAN_PEN));
	dc.DrawCircle(textPos, bbox.GetWidth());
	dc.DrawRotatedText(testString, textPos, angle);
}

Any thought ?

Laurent.




More information about the wx-users mailing list