使用字符串输出图形效果
- //////////////////////////////////////////////////////////////////
- // StringEffect - 字符串效果
- //
- // Author: 木头云
- // Blog: http://blog.csdn.net/markl22222
- // E-Mail: mark.lonr@tom.com
- // Version: 1.0.1002.1308
- //////////////////////////////////////////////////////////////////
- #if !defined(__STRING_EFFECT_H__)
- #define __STRING_EFFECT_H__
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- #include "MemDC.h"
- //////////////////////////////////////////////////////////////////
- class CStringEffect
- {
- public:
- // 将字符串转换为 Color
- static COLORREF StringToColor(CString strColor)
- {
- if( strColor.IsEmpty() ) return RGB(0, 0, 0);
- COLORREF clr = RGB(0, 0, 0);
- if( strColor[0] == _T('#') &&
- strColor.GetLength() >= 7 )
- {
- strColor.Delete(0);
- TCHAR str_rgb[7] = {0};
- TCHAR a_str_rgb[3][3] = {0};
- _tcscpy(str_rgb, strColor.GetBuffer(0));
- strColor.ReleaseBuffer();
- _tcscpy(a_str_rgb[0], str_rgb + 0);
- a_str_rgb[0][2] = _T('/0');
- _tcscpy(a_str_rgb[1], str_rgb + 2);
- a_str_rgb[1][2] = _T('/0');
- _tcscpy(a_str_rgb[2], str_rgb + 4);
- a_str_rgb[2][2] = _T('/0');
- UINT rgb[3] = {0};
- rgb[0] = _tcstol(a_str_rgb[0], NULL, 16);
- rgb[1] = _tcstol(a_str_rgb[1], NULL, 16);
- rgb[2] = _tcstol(a_str_rgb[2], NULL, 16);
- clr = RGB(rgb[0], rgb[1], rgb[2]);
- }
- else if(
- strColor.GetLength() >= 9 )
- {
- TCHAR str_rgb[10] = {0};
- TCHAR a_str_rgb[3][4] = {0};
- _tcscpy(str_rgb, strColor.GetBuffer(0));
- strColor.ReleaseBuffer();
- _tcscpy(a_str_rgb[0], str_rgb + 0);
- a_str_rgb[0][3] = _T('/0');
- _tcscpy(a_str_rgb[1], str_rgb + 3);
- a_str_rgb[1][3] = _T('/0');
- _tcscpy(a_str_rgb[2], str_rgb + 6);
- a_str_rgb[2][3] = _T('/0');
- UINT rgb[3] = {0};
- rgb[0] = _tcstol(a_str_rgb[0], NULL, 10);
- rgb[1] = _tcstol(a_str_rgb[1], NULL, 10);
- rgb[2] = _tcstol(a_str_rgb[2], NULL, 10);
- clr = RGB(rgb[0], rgb[1], rgb[2]);
- }
- return clr;
- }
- // 将字符串转换为 Rect
- static CRect StringToRect(CString strRect)
- {
- if( strRect.IsEmpty() ) return CRect(0, 0, 0, 0);
- CRect rc(0, 0, 0, 0);
- strRect.Remove(_T(' '));
- CStringArray sa;
- StringToArray(strRect, sa, _T(","));
- if( sa.GetSize() >= 4 )
- {
- rc.SetRect(
- _tcstol(sa[0], NULL, 10),
- _tcstol(sa[1], NULL, 10),
- _tcstol(sa[2], NULL, 10),
- _tcstol(sa[3], NULL, 10) );
- }
- return rc;
- }
- // 将字符串转换为 Size
- static CSize CSkinManager::StringToSize(CString strSize)
- {
- if( strSize.IsEmpty() ) return CSize(0, 0);
- CSize sz(0, 0);
- strSize.Remove(_T(' '));
- CStringArray sa;
- StringToArray(strSize, sa, _T(","));
- if( sa.GetCount() >= 2 )
- {
- sz.SetSize(
- _tcstol(sa[0], NULL, 10),
- _tcstol(sa[1], NULL, 10) );
- }
- return sz;
- }
- // 将字符串转换为 Array
- static int StringToArray(const CString& s, CStringArray& sa, LPCTSTR spl)
- {
- int nLen = s.GetLength(), nLastPos, nPos;
- bool bContinue;
- sa.RemoveAll();
- nLastPos = 0;
- CString sspl(spl);
- do
- {
- bContinue = false;
- nPos = s.Find(spl, nLastPos);
- if( -1 != nPos )
- {
- sa.Add(s.Mid(nLastPos, nPos - nLastPos));
- nLastPos = nPos + sspl.GetLength();
- if(nLastPos != nLen) bContinue=true;
- }
- }while( bContinue );
- if( nLastPos != nLen )
- sa.Add(s.Mid(nLastPos, nLen - nLastPos));
- return (int)sa.GetSize();
- }
- /////////////////////////////////
- // 按区域截断字符串
- static CString TruncateString(const CString& strTru, CRect rcTru, CDC* pDC/* = NULL*/)
- {
- if( strTru.IsEmpty() ) return strTru;
- CString str = strTru;
- const CString STR_ELLIPSIS = _T("...");
- const CString STR_NULL = _T("");
- BOOL bIsGetDC = FALSE;
- if( !pDC || !pDC->m_hDC )
- {
- pDC = CWnd::GetDesktopWindow()->GetDC();
- bIsGetDC = TRUE;
- }
- CSize size = pDC->GetTextExtent(str);
- if( size.cx <= rcTru.Width() ) return str;
- size = pDC->GetTextExtent(STR_ELLIPSIS);
- if( size.cx > rcTru.Width() ) return STR_NULL;
- int nStringLen = str.GetLength();
- for(int i = nStringLen - 1; i >= 0; i--)
- {
- str.Delete(i);
- if( (pDC->GetTextExtent(str).cx + size.cx) <= rcTru.Width() )
- break;
- }
- if( bIsGetDC )
- CWnd::GetDesktopWindow()->ReleaseDC(pDC);
- str += STR_ELLIPSIS;
- return str;
- }
- /////////////////////////////////
- // 反色字
- static void DrawInvertString(const CString str, CDC& dc, CRect& rc, UINT nFormat)
- {
- if( str.IsEmpty() ) return ;
- // 创建临时DC
- CBufDC dc_buf( &dc, &rc );
- dc_buf.FillSolidRect( &rc, RGB(0, 0, 0) );
- // 绘制文字
- dc_buf.SetBkMode(TRANSPARENT);
- dc_buf.SetTextColor(RGB(255, 255, 255));
- CFont* fnt = dc_buf.SelectObject(dc.GetCurrentFont());
- dc_buf.DrawText(str, &rc, nFormat);
- dc.BitBlt(rc.left, rc.top, rc.Width(), rc.Height(), &dc_buf, rc.left, rc.top, SRCINVERT);
- // 清理内存
- if( fnt ) dc_buf.SelectObject(fnt);
- }
- // 镂空字
- static void DrawEmptyString(const CString str, CDC& dc, CRect& rc, UINT nFormat)
- {
- if( str.IsEmpty() ) return ;
- // 绘制边框
- COLORREF clr = dc.GetTextColor();
- dc.SetTextColor(clr ^ RGB(255, 255, 255));
- CRect rc_tmp(rc);
- rc_tmp.OffsetRect(1, 0);
- dc.DrawText(str, &rc_tmp, nFormat);
- rc_tmp = rc;
- rc_tmp.OffsetRect(-1, 0);
- dc.DrawText(str, &rc_tmp, nFormat);
- rc_tmp = rc;
- rc_tmp.OffsetRect(0, 1);
- dc.DrawText(str, &rc_tmp, nFormat);
- rc_tmp = rc;
- rc_tmp.OffsetRect(0, -1);
- dc.DrawText(str, &rc_tmp, nFormat);
- // 绘制文字
- dc.SetTextColor(clr);
- dc.DrawText(str, &rc, nFormat);
- }
- // 阴影字
- static void DrawShadowString(const CString str, CDC& dc, CRect& rc, UINT nFormat)
- {
- if( str.IsEmpty() ) return ;
- // 创建临时DC
- CRect rc_sha( rc );
- rc_sha.right += 3;
- rc_sha.bottom += 3;
- CBufDC dc_tmp( &dc, &rc_sha );
- CBufDC dc_buf( &dc, &rc_sha );
- dc_buf.BitBlt(rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), &dc, rc_sha.left, rc_sha.top, SRCCOPY);
- // 绘制阴影
- dc_tmp.SetBkMode(TRANSPARENT);
- dc_tmp.SetTextColor(RGB(0, 0, 0));
- CFont* fnt = dc_tmp.SelectObject(dc.GetCurrentFont());
- CRect rc_tmp( rc );
- BLENDFUNCTION blend;
- blend.BlendOp = AC_SRC_OVER;
- blend.BlendFlags = 0;
- blend.AlphaFormat = 0;
- //////////////////////////////////
- rc_tmp.OffsetRect(1, 1);
- dc_tmp.BitBlt(rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), &dc, rc_sha.left, rc_sha.top, SRCCOPY);
- dc_tmp.DrawText(str, &rc_tmp, nFormat);
- blend.SourceConstantAlpha = 158;
- ::AlphaBlend(dc_buf.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(),
- dc_tmp.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), blend);
- //////////////////////////////////
- rc_tmp.OffsetRect(1, 1);
- dc_tmp.BitBlt(rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), &dc, rc_sha.left, rc_sha.top, SRCCOPY);
- dc_tmp.DrawText(str, &rc_tmp, nFormat);
- blend.SourceConstantAlpha = 37;
- ::AlphaBlend(dc_buf.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(),
- dc_tmp.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), blend);
- //////////////////////////////////
- rc_tmp.OffsetRect(0, 1);
- dc_tmp.BitBlt(rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), &dc, rc_sha.left, rc_sha.top, SRCCOPY);
- dc_tmp.DrawText(str, &rc_tmp, nFormat);
- blend.SourceConstantAlpha = 14;
- ::AlphaBlend(dc_buf.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(),
- dc_tmp.GetSafeHdc(), rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), blend);
- //////////////////////////////////
- dc.BitBlt(rc_sha.left, rc_sha.top, rc_sha.Width(), rc_sha.Height(), &dc_buf, rc_sha.left, rc_sha.top, SRCCOPY);
- // 绘制文字
- dc.DrawText(str, &rc, nFormat);
- }
- };
- //////////////////////////////////////////////////////////////////
- #endif // !defined(__STRING_EFFECT_H__)
其中的CBufDC为专门用于缓存的DC类,继承自CMemDC.
http://blog.csdn.net/markl22222/article/details/5308102