#include <cmath>
// pDC : pointer to your device-context
// str : the text
// rect: the rectangle
// nOptions: can be a combination of ETO_CLIPPED and ETO_OPAQUE
void DrawRotatedText(CDC* pDC, const CString str, CRect rect,
double angle, UINT nOptions = 0)
{
// convert angle to radian
double pi = 3.141592654;
double radian = pi * 2 / 360 * angle;
// get the center of a not-rotated text
CSize TextSize = pDC->GetTextExtent(str);
CPoint center;
center.x = TextSize.cx / 2;
center.y = TextSize.cy / 2;
// now calculate the center of the rotated text
CPoint rcenter;
rcenter.x = long(cos(radian) * center.x - sin(radian) * center.y);
rcenter.y = long(sin(radian) * center.x + cos(radian) * center.y);
// finally draw the text and move it to the center of the rectangle
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);
}