旋轉字符輸出

代碼如下:

 

 

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;
}

上面是WM_PAINT 的處理,主要是窗口中心畫圓,然後是按12等分的位置在圓周上寫字。

兩個要點:

  1. 字體。每個角度的字體要單獨創建,就是修改 LOGFONT.lfEscapement .  llfEscapement的單位是1/10度,所以賦值時是要旋轉的角度乘10。
  2. 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);



}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章