65.9K
CodeProject 正在变化。 阅读更多。
Home

简单的文本旋转

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.27/5 (13投票s)

2000年9月27日

viewsIcon

139962

一个简单的函数,用于在矩形内围绕其中心点旋转文本

引言

我正在开发一种CAD软件,需要一个能够围绕矩形中心旋转文本的功能。 由于Windows只提供围绕指定文本左下角旋转文本的功能,我遇到了问题。 我在网站上搜索这段代码,但没有找到任何东西。 所以我自己尝试了一下,这就是我程序中使用的代码。

首先,创建一个CFont,并在nEscapement中指定旋转角度。 让你的DC选择此字体并调用以下函数

#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
// (see documentation of ExtTextOut for more details)
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);
}

许可证

本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。

作者可能使用的许可证列表可以在此处找到。

© . All rights reserved.