C++ GDI+ 帶陰影的文字功能的實現

來源:互聯網
上載者:User

標籤:gdi+   c++   

      用GDI+的優秀圖形輸出功能可以非常方便的實現文字特效,其中一個帶陰影的文字便是其中一例。

      許多簡單的文字特效只是簡單的將文字用不同的顏色與不同的位置輸出一次或多次,本文所討論的陰影製作效果藉助GDI+的反走樣能力產生透明的陰影與半陰影。 這兒所述的方法先在繪圖平面上繪製一個比預期小的文字,然後放大它

     由於代碼注釋很詳細,直接上代碼:

ULONG_PTRg_gdiPlusToken = NULL;  //GDI+ 初始化void CMFCApplication1Dlg::OnBnClickedOk(){    using namespace Gdiplus;    if (NULL == g_gdiPlusToken)    {        Gdiplus::GdiplusStartupInput gdiplusStartupInput;        Gdiplus::GdiplusStartup(&g_gdiPlusToken, &gdiplusStartupInput, NULL);    }    CRect    ClientRC;        CStringW strTxt("19:20:30");    m_picBox.GetClientRect(&ClientRC);    RectF         desRC(ClientRC.left, ClientRC.top, ClientRC.Width(), ClientRC.Height());    PointF        txtPos(0, 0);    FontFamily    fontFamily(L"Times New Roman");    Gdiplus::Font font(&fontFamily, 100, FontStyleBold, UnitPixel);    Graphics      g(m_picBox.GetDC()->m_hDC);    //1.0 填充背景色    g.FillRectangle(&Gdiplus::SolidBrush(Color::LightSlateGray), desRC);    //2.0 建立一個小尺寸的記憶體位元影像,設定它的長寬為總尺寸的1/4    Bitmap bmp(ClientRC.Width() / 4, ClientRC.Height() / 4, &g);    //2.1 設定繪製模式為反走樣模式    Graphics* pTempG = Graphics::FromImage(&bmp);    pTempG->SetTextRenderingHint(TextRenderingHintAntiAlias);        //2.2 建立一個矩陣,使字型為原來的1/4,陰影距離也為你要設定文本的1/4左右    Matrix mx(0.25f, 0, 0, 0.25f, 3, 3);    pTempG->SetTransform(&mx);    //2.3 在位元影像上繪製文本,使用有透明度的畫筆(比如50%透明)    pTempG->DrawString(strTxt, -1, &font, txtPos, NULL, &SolidBrush(Color(128, 0, 0, 0)));    //3.1 插值模式為高品質雙立方插值法,插值法非常重要,因為雙立方插值使文本的邊模糊,這樣就出現陰影與半影效果    g.SetInterpolationMode(InterpolationModeHighQualityBicubic);       //3.2 設定繪製模式為反走樣模式以保證正確的範圍    g.SetTextRenderingHint(TextRenderingHintAntiAlias);    //3.3 把位元影像顯示在螢幕上,在兩個方向上都放大4倍    g.DrawImage(&bmp, desRC, 0, 0, bmp.GetWidth(), bmp.GetHeight(), UnitPixel);    //3.4 把文本繪製到繪圖平面上, 使用白色字型    g.DrawString(strTxt, -1, &font, txtPos, NULL, &SolidBrush(Color::White));    //4.0 釋放記憶體    if (NULL != pTempG)    {        delete pTempG;        pTempG = NULL;    }}



C++ GDI+ 帶陰影的文字功能的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.