Drawing Rotate Text with
GetTextExtent, SetTextAlign
and ExtTextOut.
First, create a
CFont
with the angle of rotation
specified in
nEscapement
.
Let your DC select this font
and call the following
function:
Collapse
Copy Code
#include <cmath>
void DrawRotatedText(CDC* pDC, const CString str, CRect rect,
double angle, UINT nOptions = 0)
{
double pi = 3.141592654;
double radian = pi * 2 / 360 * angle;
CSize TextSize = pDC->GetTextExtent(str);
CPoint center;
center.x = TextSize.cx / 2;
center.y = TextSize.cy / 2;
CPoint rcenter;
rcenter.x = long(cos(radian) * center.x - sin(radian) * center.y);
rcenter.y = long(sin(radian) * center.x + cos(radian) * center.y);
pDC->SetTextAlign(TA_BASELINE);
pDC->SetBkMode(TRANSPARENT);
pDC->ExtTextOut(rect.left + rect.Width() / 2 - rcenter.x,
rect.top + rect.Height() / 2 + rcenter.y,
nOptions, rect, str, NULL);
}