代码如下:
LRESULT CRotatextView::OnPaint(UINT
/**/
/*uMsg*/
, WPARAM
/**/
/*wParam*/
, LPARAM
/**/
/*lParam*/
, BOOL
&
/**/
/*bHandled*/
)
... {
CPaintDC dc(m_hWnd);
CRect rcClient;
GetClientRect(&rcClient);
CPoint ptCenter = rcClient.CenterPoint();
int r = 100; //园的半径
CRect rcEllipse(ptCenter.x - r,ptCenter.y-r,ptCenter.x+r,ptCenter.y+r);
dc.Ellipse(&rcEllipse);
double angle; //角度
double radian; //弧度
int n = 12;
#ifdef M_PI
double pi = M_PI;
#else
double pi = 3.14159265358979323846;
#endif
for(int i = 0 ; i < n; ++i)
...{
angle = 360 / n * i;
radian = 2 * pi / n * i;
CFont font;
m_logfont.lfEscapement = static_cast<long>((360 - (angle+90))*10);
font.CreateFontIndirect(&m_logfont);
CFontHandle oldfont = dc.SelectFont(font);
CString str;
CSize szText;
str.Format(_T("%d"),i);
dc.GetTextExtent(str,str.GetLength(),&szText);
CPoint ptTextCenter; //输出的文本串的中心点
ptTextCenter.x = ptCenter.x+static_cast<long>((r+szText.cy/2) * cos(radian));
ptTextCenter.y = ptCenter.y+static_cast<long>((r+szText.cy/2)*sin(radian));
//DrawRotatedText(dc,m_strview,&m_rcBound,m_angle);
DrawRotatedText(dc,str,ptTextCenter,(360-(angle+90)));
dc.SelectFont(oldfont);
}
return 0;
}
... {
CPaintDC dc(m_hWnd);
CRect rcClient;
GetClientRect(&rcClient);
CPoint ptCenter = rcClient.CenterPoint();
int r = 100; //园的半径
CRect rcEllipse(ptCenter.x - r,ptCenter.y-r,ptCenter.x+r,ptCenter.y+r);
dc.Ellipse(&rcEllipse);
double angle; //角度
double radian; //弧度
int n = 12;
#ifdef M_PI
double pi = M_PI;
#else
double pi = 3.14159265358979323846;
#endif
for(int i = 0 ; i < n; ++i)
...{
angle = 360 / n * i;
radian = 2 * pi / n * i;
CFont font;
m_logfont.lfEscapement = static_cast<long>((360 - (angle+90))*10);
font.CreateFontIndirect(&m_logfont);
CFontHandle oldfont = dc.SelectFont(font);
CString str;
CSize szText;
str.Format(_T("%d"),i);
dc.GetTextExtent(str,str.GetLength(),&szText);
CPoint ptTextCenter; //输出的文本串的中心点
ptTextCenter.x = ptCenter.x+static_cast<long>((r+szText.cy/2) * cos(radian));
ptTextCenter.y = ptCenter.y+static_cast<long>((r+szText.cy/2)*sin(radian));
//DrawRotatedText(dc,m_strview,&m_rcBound,m_angle);
DrawRotatedText(dc,str,ptTextCenter,(360-(angle+90)));
dc.SelectFont(oldfont);
}
return 0;
}
上面是WM_PAINT 的处理,主要是窗口中心画圆,然后是按12等分的位置在圆周上写字。
两个要点:
- 字体。每个角度的字体要单独创建,就是修改 LOGFONT.lfEscapement . llfEscapement的单位是1/10度,所以赋值时是要旋转的角度乘10。
- DrawRotatedText 的POINT参数是要输出的文本的中心点。
下面是 DrawRotatedText的代码。
void
CRotatextView::DrawRotatedText(HDC hdc,LPCTSTR str,
const
CPoint
&
ptcenter,
double
angle,UINT nOption)
... {
double radian = M_PI * angle / 180;
CSize TextSize;
GetTextExtentPoint32(hdc,str,lstrlen(str),&TextSize);
CRect rcBoundText(ptcenter.x-TextSize.cx/2,ptcenter.y-TextSize.cy/2,ptcenter.x+TextSize.cx/2,ptcenter.y+TextSize.cy/2);
//Rectangle(hdc,rcBoundText.left,rcBoundText.top,rcBoundText.right,rcBoundText.bottom);
CPoint rcenter;
rcenter.x = long(cos(radian) * (TextSize.cx/2) - sin(radian) * (TextSize.cy/2));
rcenter.y = long(sin(radian) * (TextSize.cx/2) + cos(radian) * (TextSize.cy/2));
SetTextAlign(hdc, TA_BASELINE);
SetBkMode(hdc, TRANSPARENT);
ExtTextOut(hdc, rcBoundText.left + (rcBoundText.right - rcBoundText.left) / 2 - rcenter.x,
rcBoundText.top + rcBoundText.Height() / 2 + rcenter.y,
nOption, &rcBoundText, str, strlen(str), NULL);
}
... {
double radian = M_PI * angle / 180;
CSize TextSize;
GetTextExtentPoint32(hdc,str,lstrlen(str),&TextSize);
CRect rcBoundText(ptcenter.x-TextSize.cx/2,ptcenter.y-TextSize.cy/2,ptcenter.x+TextSize.cx/2,ptcenter.y+TextSize.cy/2);
//Rectangle(hdc,rcBoundText.left,rcBoundText.top,rcBoundText.right,rcBoundText.bottom);
CPoint rcenter;
rcenter.x = long(cos(radian) * (TextSize.cx/2) - sin(radian) * (TextSize.cy/2));
rcenter.y = long(sin(radian) * (TextSize.cx/2) + cos(radian) * (TextSize.cy/2));
SetTextAlign(hdc, TA_BASELINE);
SetBkMode(hdc, TRANSPARENT);
ExtTextOut(hdc, rcBoundText.left + (rcBoundText.right - rcBoundText.left) / 2 - rcenter.x,
rcBoundText.top + rcBoundText.Height() / 2 + rcenter.y,
nOption, &rcBoundText, str, strlen(str), NULL);
}