• 使用字符串输出图形效果


    [cpp] view plaincopy
     
    1. //////////////////////////////////////////////////////////////////  
    2. // StringEffect - 字符串效果  
    3. //  
    4. // Author:  木头云  
    5. // Blog:    http://blog.csdn.net/markl22222  
    6. // E-Mail:  mark.lonr@tom.com  
    7. // Version: 1.0.1002.1308  
    8. //////////////////////////////////////////////////////////////////  
    9.   
    10. #if !defined(__STRING_EFFECT_H__)  
    11. #define __STRING_EFFECT_H__  
    12.   
    13. #if _MSC_VER > 1000  
    14. #pragma once  
    15. #endif // _MSC_VER > 1000  
    16.   
    17. #include "MemDC.h"  
    18.   
    19. //////////////////////////////////////////////////////////////////  
    20.   
    21. class CStringEffect  
    22. {  
    23. public:  
    24.     // 将字符串转换为 Color  
    25.     static COLORREF StringToColor(CString strColor)  
    26.     {  
    27.         if( strColor.IsEmpty() ) return RGB(0, 0, 0);  
    28.   
    29.         COLORREF clr = RGB(0, 0, 0);  
    30.   
    31.         if( strColor[0] == _T('#') &&   
    32.             strColor.GetLength() >= 7 )  
    33.         {  
    34.             strColor.Delete(0);  
    35.             TCHAR str_rgb[7]        = {0};  
    36.             TCHAR a_str_rgb[3][3]   = {0};  
    37.             _tcscpy(str_rgb, strColor.GetBuffer(0));  
    38.             strColor.ReleaseBuffer();  
    39.             _tcscpy(a_str_rgb[0], str_rgb + 0);  
    40.             a_str_rgb[0][2] = _T('/0');  
    41.             _tcscpy(a_str_rgb[1], str_rgb + 2);  
    42.             a_str_rgb[1][2] = _T('/0');  
    43.             _tcscpy(a_str_rgb[2], str_rgb + 4);  
    44.             a_str_rgb[2][2] = _T('/0');  
    45.             UINT rgb[3] = {0};  
    46.             rgb[0] = _tcstol(a_str_rgb[0], NULL, 16);  
    47.             rgb[1] = _tcstol(a_str_rgb[1], NULL, 16);  
    48.             rgb[2] = _tcstol(a_str_rgb[2], NULL, 16);  
    49.             clr = RGB(rgb[0], rgb[1], rgb[2]);  
    50.         }  
    51.         else if(   
    52.             strColor.GetLength() >= 9 )  
    53.         {  
    54.             TCHAR str_rgb[10]       = {0};  
    55.             TCHAR a_str_rgb[3][4]   = {0};  
    56.             _tcscpy(str_rgb, strColor.GetBuffer(0));  
    57.             strColor.ReleaseBuffer();  
    58.             _tcscpy(a_str_rgb[0], str_rgb + 0);  
    59.             a_str_rgb[0][3] = _T('/0');  
    60.             _tcscpy(a_str_rgb[1], str_rgb + 3);  
    61.             a_str_rgb[1][3] = _T('/0');  
    62.             _tcscpy(a_str_rgb[2], str_rgb + 6);  
    63.             a_str_rgb[2][3] = _T('/0');  
    64.             UINT rgb[3] = {0};  
    65.             rgb[0] = _tcstol(a_str_rgb[0], NULL, 10);  
    66.             rgb[1] = _tcstol(a_str_rgb[1], NULL, 10);  
    67.             rgb[2] = _tcstol(a_str_rgb[2], NULL, 10);  
    68.             clr = RGB(rgb[0], rgb[1], rgb[2]);  
    69.         }  
    70.   
    71.         return clr;  
    72.     }  
    73.   
    74.     // 将字符串转换为 Rect  
    75.     static CRect StringToRect(CString strRect)  
    76.     {  
    77.         if( strRect.IsEmpty() ) return CRect(0, 0, 0, 0);  
    78.   
    79.         CRect rc(0, 0, 0, 0);  
    80.   
    81.         strRect.Remove(_T(' '));  
    82.         CStringArray sa;  
    83.         StringToArray(strRect, sa, _T(","));  
    84.   
    85.         if( sa.GetSize() >= 4 )  
    86.         {  
    87.             rc.SetRect(   
    88.                 _tcstol(sa[0], NULL, 10),   
    89.                 _tcstol(sa[1], NULL, 10),   
    90.                 _tcstol(sa[2], NULL, 10),   
    91.                 _tcstol(sa[3], NULL, 10) );  
    92.         }  
    93.   
    94.         return rc;  
    95.     }  
    96.   
    97.     // 将字符串转换为 Size  
    98.     static CSize CSkinManager::StringToSize(CString strSize)  
    99.     {  
    100.         if( strSize.IsEmpty() ) return CSize(0, 0);  
    101.   
    102.         CSize sz(0, 0);  
    103.   
    104.         strSize.Remove(_T(' '));  
    105.         CStringArray sa;  
    106.         StringToArray(strSize, sa, _T(","));  
    107.   
    108.         if( sa.GetCount() >= 2 )  
    109.         {  
    110.             sz.SetSize(   
    111.                 _tcstol(sa[0], NULL, 10),   
    112.                 _tcstol(sa[1], NULL, 10) );  
    113.         }  
    114.   
    115.         return sz;  
    116.     }  
    117.   
    118.     // 将字符串转换为 Array  
    119.     static int StringToArray(const CString& s, CStringArray& sa, LPCTSTR spl)  
    120.     {  
    121.         int nLen = s.GetLength(), nLastPos, nPos;  
    122.         bool bContinue;  
    123.   
    124.         sa.RemoveAll();  
    125.         nLastPos = 0;  
    126.         CString sspl(spl);  
    127.         do  
    128.         {  
    129.             bContinue = false;  
    130.             nPos = s.Find(spl, nLastPos);  
    131.             if( -1 != nPos )  
    132.             {  
    133.                 sa.Add(s.Mid(nLastPos, nPos - nLastPos));  
    134.                 nLastPos = nPos + sspl.GetLength();  
    135.                 if(nLastPos != nLen) bContinue=true;  
    136.             }  
    137.         }while( bContinue );  
    138.   
    139.         if( nLastPos != nLen )  
    140.             sa.Add(s.Mid(nLastPos, nLen - nLastPos));  
    141.   
    142.         return (int)sa.GetSize();  
    143.     }  
    144.   
    145.     /////////////////////////////////  
    146.   
    147.     // 按区域截断字符串  
    148.     static CString TruncateString(const CString& strTru, CRect rcTru, CDC* pDC/* = NULL*/)  
    149.     {  
    150.         if( strTru.IsEmpty() ) return strTru;  
    151.   
    152.         CString str = strTru;  
    153.         const CString STR_ELLIPSIS  = _T("...");  
    154.         const CString STR_NULL      = _T("");  
    155.   
    156.         BOOL bIsGetDC = FALSE;  
    157.         if( !pDC || !pDC->m_hDC )  
    158.         {  
    159.             pDC = CWnd::GetDesktopWindow()->GetDC();  
    160.             bIsGetDC = TRUE;  
    161.         }  
    162.   
    163.         CSize size = pDC->GetTextExtent(str);  
    164.         if( size.cx <= rcTru.Width() ) return str;  
    165.         size = pDC->GetTextExtent(STR_ELLIPSIS);  
    166.         if( size.cx > rcTru.Width() ) return STR_NULL;  
    167.   
    168.         int nStringLen = str.GetLength();  
    169.         for(int i = nStringLen - 1; i >= 0; i--)  
    170.         {  
    171.             str.Delete(i);  
    172.             if( (pDC->GetTextExtent(str).cx + size.cx) <= rcTru.Width() )  
    173.                 break;  
    174.         }  
    175.   
    176.         if( bIsGetDC )  
    177.             CWnd::GetDesktopWindow()->ReleaseDC(pDC);  
    178.   
    179.         str += STR_ELLIPSIS;  
    180.         return str;  
    181.     }  
    182.   
    183.     /////////////////////////////////  
    184.   
    185.     // 反色字  
    186.     static void DrawInvertString(const CString str, CDC& dc, CRect& rc, UINT nFormat)  
    187.     {  
    188.         if( str.IsEmpty() ) return ;  
    189.         // 创建临时DC  
    190.         CBufDC dc_buf( &dc, &rc );  
    191.         dc_buf.FillSolidRect( &rc, RGB(0, 0, 0) );  
    192.         // 绘制文字  
    193.         dc_buf.SetBkMode(TRANSPARENT);  
    194.         dc_buf.SetTextColor(RGB(255, 255, 255));  
    195.         CFont* fnt = dc_buf.SelectObject(dc.GetCurrentFont());  
    196.         dc_buf.DrawText(str, &rc, nFormat);  
    197.         dc.BitBlt(rc.left, rc.top, rc.Width(), rc.Height(), &dc_buf, rc.left, rc.top, SRCINVERT);  
    198.         // 清理内存  
    199.         if( fnt ) dc_buf.SelectObject(fnt);  
    200.     }  
    201.   
    202.     // 镂空字  
    203.     static void DrawEmptyString(const CString str, CDC& dc, CRect& rc, UINT nFormat)  
    204.     {  
    205.         if( str.IsEmpty() ) return ;  
    206.         // 绘制边框  
    207.         COLORREF clr = dc.GetTextColor();  
    208.         dc.SetTextColor(clr ^ RGB(255, 255, 255));  
    209.         CRect rc_tmp(rc);  
    210.         rc_tmp.OffsetRect(1, 0);  
    211.         dc.DrawText(str, &rc_tmp, nFormat);  
    212.         rc_tmp = rc;  
    213.         rc_tmp.OffsetRect(-1, 0);  
    214.         dc.DrawText(str, &rc_tmp, nFormat);  
    215.         rc_tmp = rc;  
    216.         rc_tmp.OffsetRect(0, 1);  
    217.         dc.DrawText(str, &rc_tmp, nFormat);  
    218.         rc_tmp = rc;  
    219.         rc_tmp.OffsetRect(0, -1);  
    220.         dc.DrawText(str, &rc_tmp, nFormat);  
    221.         // 绘制文字  
    222.         dc.SetTextColor(clr);  
    223.         dc.DrawText(str, &rc, nFormat);  
    224.     }  
    225.   
    226.     // 阴影字  
    227.     static void DrawShadowString(const CString str, CDC& dc, CRect& rc, UINT nFormat)  
    228.     {  
    229.         if( str.IsEmpty() ) return ;  
    230.   
    231.         // 创建临时DC  
    232.         CRect  rc_sha( rc );  
    233.         rc_sha.right  += 3;  
    234.         rc_sha.bottom += 3;   
    235.         CBufDC dc_tmp( &dc, &rc_sha );  
    236.         CBufDC dc_buf( &dc, &rc_sha );  
    237.         dc_buf.BitBlt(rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), &dc, rc_sha.left, rc_sha.top, SRCCOPY);  
    238.   
    239.         // 绘制阴影  
    240.         dc_tmp.SetBkMode(TRANSPARENT);  
    241.         dc_tmp.SetTextColor(RGB(0, 0, 0));  
    242.         CFont* fnt = dc_tmp.SelectObject(dc.GetCurrentFont());  
    243.         CRect rc_tmp( rc );  
    244.         BLENDFUNCTION blend;  
    245.         blend.BlendOp             = AC_SRC_OVER;  
    246.         blend.BlendFlags          = 0;  
    247.         blend.AlphaFormat         = 0;  
    248.         //////////////////////////////////  
    249.         rc_tmp.OffsetRect(1, 1);  
    250.         dc_tmp.BitBlt(rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), &dc, rc_sha.left, rc_sha.top, SRCCOPY);  
    251.         dc_tmp.DrawText(str, &rc_tmp, nFormat);  
    252.         blend.SourceConstantAlpha = 158;  
    253.         ::AlphaBlend(dc_buf.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(),   
    254.             dc_tmp.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), blend);  
    255.         //////////////////////////////////  
    256.         rc_tmp.OffsetRect(1, 1);  
    257.         dc_tmp.BitBlt(rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), &dc, rc_sha.left, rc_sha.top, SRCCOPY);  
    258.         dc_tmp.DrawText(str, &rc_tmp, nFormat);  
    259.         blend.SourceConstantAlpha = 37;  
    260.         ::AlphaBlend(dc_buf.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(),   
    261.             dc_tmp.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), blend);  
    262.         //////////////////////////////////  
    263.         rc_tmp.OffsetRect(0, 1);  
    264.         dc_tmp.BitBlt(rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), &dc, rc_sha.left, rc_sha.top, SRCCOPY);  
    265.         dc_tmp.DrawText(str, &rc_tmp, nFormat);  
    266.         blend.SourceConstantAlpha = 14;  
    267.         ::AlphaBlend(dc_buf.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(),   
    268.             dc_tmp.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), blend);  
    269.         //////////////////////////////////  
    270.         dc.BitBlt(rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), &dc_buf, rc_sha.left, rc_sha.top, SRCCOPY);  
    271.   
    272.         // 绘制文字  
    273.         dc.DrawText(str, &rc, nFormat);  
    274.     }  
    275. };  
    276.   
    277. //////////////////////////////////////////////////////////////////  
    278.   
    279. #endif // !defined(__STRING_EFFECT_H__)  

    其中的CBufDC为专门用于缓存的DC类,继承自CMemDC.

    http://blog.csdn.net/markl22222/article/details/5308102

  • 相关阅读:
    WPF 制作高性能的透明背景异形窗口(使用 WindowChrome 而不要使用 AllowsTransparency=True)
    【ASP.NET Core】自己编程来生成自签名的服务器证书
    WPF:DataGrid可过滤、多语言
    将python脚本打包为exe可执行文件
    C# WPF DATAGRID 分组(GROUP)
    皮肤样式备份
    wpf – 如何在UIElement.Margin上为绑定设置FallbackValue?
    Wpf虚拟屏幕键盘
    ubuntu docker 安装 redis
    fastapi 返回请求头信息
  • 原文地址:https://www.cnblogs.com/findumars/p/5017827.html
Copyright © 2020-2023  润新知