码迷,mamicode.com
首页 > 其他好文 > 详细

变色字体

时间:2018-07-05 21:38:01      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:compress   you   plain   processor   nowrap   erp   height   graph   orange   

CRect rect;

    GetClientRect(hWnd, rect);
    HDC hdc = GetDC(hWnd);
    HDC memDC = CreateCompatibleDC(hdc);
 
    BITMAPINFO bmi;
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biHeight = rect.Height();
    bmi.bmiHeader.biWidth = rect.Width();
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biClrImportant = 0;
    bmi.bmiHeader.biClrUsed = 0;
    bmi.bmiHeader.biXPelsPerMeter = 0;
    bmi.bmiHeader.biYPelsPerMeter = 0;
    bmi.bmiHeader.biSizeImage = rect.Width()*rect.Height() * 32 / 8;
     
    HBITMAP bmp = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
    SelectObject(memDC, bmp);
 
    Gdiplus::Graphics *graphics = new Gdiplus::Graphics(memDC);
    graphics->SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
    graphics->SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAlias);
 
#ifdef _UNICODE
    LPWSTR str = (LPWSTR)_lyric;
#else
    USES_CONVERSION;
    LPWSTR str = A2W(_lyric);
#endif // _UNICODE
    Gdiplus::FontFamily fontFamily(L"微软雅黑");
    Gdiplus::Font m_pFont(&fontFamily, 48, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
    Gdiplus::Font m_pFont2(&fontFamily, 48, Gdiplus::FontStyleUnderline, Gdiplus::UnitPixel);//下划线
 
    Gdiplus::StringFormat format; 
    RectF layoutRect(0, 0, 1000, 100);
    RectF boundingBox;
    StringFormat strfmt;
    strfmt.SetTrimming(StringTrimmingWord);
    strfmt.SetFormatFlags(StringFormatFlagsNoWrap);
    strfmt.SetLineAlignment(StringAlignmentCenter);
    strfmt.SetAlignment(StringAlignmentCenter);
    graphics->MeasureString(str, -1, &m_pFont, layoutRect, &strfmt, &boundingBox);
     
     
    Gdiplus::GraphicsPath stringPath(FillModeAlternate);//创建路径
 
    stringPath.AddString(str, -1, &fontFamily, m_pFont.GetStyle(), 48, boundingBox,&strfmt); //把文字加入路径
     
    graphics->DrawPath(&Pen(Color(150,0,0,0)), &stringPath);//绘制边框
     
    Gdiplus::LinearGradientBrush brush(boundingBox, Color::Blue, Color::Red, LinearGradientModeHorizontal);
 
    int colors[] = { Color::Red ,Color::Orange ,Color::Yellow ,Color::Green ,Color::Cyan,Color::Blue ,Color::Purple };
    REAL positions[] = { 0.0f,0.16f,0.33f,0.50f,0.67f,0.83f,1.0f };
    brush.SetInterpolationColors((Color*)colors, positions, 7);
    graphics->FillPath(&brush, &stringPath);//填充路径
 
    RectF fillrect = boundingBox;
    fillrect.Width = (boundingBox.Width*0.2);
    stringPath.AddString(str, -1, &fontFamily, m_pFont2.GetStyle(), 48, boundingBox, &strfmt); //把文字加入路径
 
    graphics->IntersectClip(fillrect);
    graphics->FillPath(&SolidBrush(Color::Red), &stringPath);
 
    RectF fillrect2 = boundingBox;
    fillrect2.X += 500;
    fillrect2.Width = (boundingBox.Width*0.2);
    graphics->SetClip(fillrect2);   
    graphics->FillPath(&SolidBrush(Color::Green), &stringPath);
 
    delete graphics;
     
    BLENDFUNCTION bldfun;
    bldfun.AlphaFormat = AC_SRC_ALPHA;
    bldfun.BlendFlags = 0;
    bldfun.BlendOp = AC_SRC_OVER;
    bldfun.SourceConstantAlpha = 255;
    UpdateLayeredWindow(hWnd, hdc, NULL, &CSize(1000, 100), memDC, &CPoint(0, 0), 0, &bldfun, ULW_ALPHA);
     
    ::ReleaseDC(hWnd, hdc);
    ::DeleteDC(memDC);
    DeleteObject(bmp);
 
技术分享图片

 

变色字体

标签:compress   you   plain   processor   nowrap   erp   height   graph   orange   

原文地址:https://www.cnblogs.com/ye-ming/p/9270027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!