MFC 可以设置背景色、字体、字体颜色、透明背景的 Static 静态文本控件

简介: MFC库里没有符合这个条件的控件,于是我自己写了一个,初步测试有效。注:可以设置透明背景,但还不能做到透明度设置(如50%透明度)        如果设置了背景色,就不保留透明背景        默认背景色是透明的[cpp] view plaincopy// 设置背景色(若clr...

MFC库里没有符合这个条件的控件,于是我自己写了一个,初步测试有效。

注:可以设置透明背景,但还不能做到透明度设置(如50%透明度)

        如果设置了背景色,就不保留透明背景

        默认背景色是透明的

[cpp]  view plain copy
  1. // 设置背景色(若clr为CLR_NONE,则背景透明)  
  2. void SetBackgroundColor(COLORREF clr){m_clrBackground = clr;}  
  3. // 设置文字前景色  
  4. void SetTextColor(COLORREF clr){m_clrText = clr;}  
  5. // 设置文字字体  
  6. void SetFont(CString strFaceName, LONG nHeight);  


如何使用:

    1.先将RichStatic.h和RichStatic.cpp添加入工程
    2.对话框添加Static控件后,增加一个控件变量,类型设置为CRichStatic(或手动添加,在对话框类DoDataExchange中添加DDX_Control)


源码:

[cpp]  view plain copy
  1. #pragma once  
  2.   
  3.   
  4. // CRichStatic  
  5.   
  6. class CRichStatic : public CStatic  
  7. {  
  8.     DECLARE_DYNAMIC(CRichStatic)  
  9.   
  10. public:  
  11.     CRichStatic();  
  12.     virtual ~CRichStatic();  
  13.       
  14. protected:  
  15.     afx_msg BOOL OnEraseBkgnd(CDC* pDC);  
  16.     afx_msg LRESULT OnSetText(WPARAM,LPARAM);  
  17.     DECLARE_MESSAGE_MAP()  
  18.     virtual void PreSubclassWindow();  
  19.   
  20. private:  
  21.     COLORREF m_clrText;          // 文字前景色  
  22.     COLORREF m_clrBackground;    // 文字背景色  
  23.     CFont *m_pTextFont;          // 文字字体  
  24.     CBitmap m_Bmp;               // 保存背景用的位图对象  
  25. public:  
  26.     // 设置背景色(若clr为CLR_NONE,则背景透明)  
  27.     void SetBackgroundColor(COLORREF clr){m_clrBackground = clr;}  
  28.     // 设置文字前景色  
  29.     void SetTextColor(COLORREF clr){m_clrText = clr;}  
  30.     // 设置文字字体  
  31.     void SetFont(CString strFaceName, LONG nHeight);  
  32.   
  33. public:  
  34.     virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);  
  35. };  

[cpp]  view plain copy
  1. // RichStatic.cpp : 实现文件  
  2. //  
  3.   
  4.   
  5. #include "stdafx.h"  
  6. #include "RichStatic.h"  
  7.   
  8.   
  9.   
  10.   
  11. // CRichStatic  
  12.   
  13.   
  14. IMPLEMENT_DYNAMIC(CRichStatic, CStatic)  
  15.   
  16.   
  17. CRichStatic::CRichStatic():  
  18.     m_clrText(0), m_clrBackground(CLR_NONE), m_hFont(NULL), m_selfCreated(FALSE),   
  19.     m_xAlignment(X_LEFT), m_yAlignment(Y_TOP)  
  20. {  
  21.   
  22.   
  23. }  
  24.   
  25.   
  26. CRichStatic::~CRichStatic()  
  27. {  
  28.     if (m_selfCreated && m_hFont != NULL)  
  29.     {  
  30.         DeleteObject(m_hFont);    // 若字体对象为对象自己创建并且不为NULL,则销毁掉以释放内核对象  
  31.     }  
  32. }  
  33.   
  34.   
  35.   
  36.   
  37. BEGIN_MESSAGE_MAP(CRichStatic, CStatic)  
  38.    ON_MESSAGE(WM_SETTEXT,OnSetText)  
  39.    ON_WM_ERASEBKGND()  
  40. END_MESSAGE_MAP()  
  41.   
  42.   
  43.   
  44.   
  45.   
  46.   
  47. // CRichStatic 消息处理程序  
  48.   
  49.   
  50. void CRichStatic::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)  
  51. {  
  52.     if (m_clrBackground != CLR_NONE)    // 若背景色不为CLR_NONE(CLR_NONE表示无背景色),则绘制背景  
  53.     {  
  54.         RECT rect;  
  55.         GetWindowRect(&rect);  
  56.         CBrush brush;  
  57.         brush.CreateSolidBrush(m_clrBackground);  
  58.         ::SelectObject(lpDrawItemStruct->hDC, brush.m_hObject);    // 设置画刷颜色  
  59.         ::SelectObject(lpDrawItemStruct->hDC, GetStockObject(NULL_PEN));    // 设置笔为空笔(不绘制边界)  
  60.         Rectangle(lpDrawItemStruct->hDC, 0, 0,rect.right - rect.left, rect.bottom - rect.top);  
  61.     }  
  62.   
  63.   
  64.     CString strCaption;    // 标题文字  
  65.     GetWindowText(strCaption);  
  66.     if (m_hFont != NULL)  
  67.     {  
  68.         ::SelectObject(lpDrawItemStruct->hDC, m_hFont);  
  69.     }  
  70.   
  71.   
  72.     // 计算输出字串的横纵坐标   
  73.     int x = 0, y = 0;  
  74.     if (X_LEFT != m_xAlignment || Y_TOP != m_yAlignment)    // 不是左对齐或不是顶对齐  
  75.     {  
  76.         CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);  
  77.         CRect crect;  
  78.         GetWindowRect(&crect);  
  79.         CSize size = pDC->GetTextExtent(strCaption);  
  80.         if (X_RIGHT == m_xAlignment)    // 右对齐  
  81.         {  
  82.             x = crect.Width() - size.cx;  
  83.         }  
  84.         else if (X_CENTER == m_xAlignment)   // X居中对齐  
  85.         {  
  86.             x = (crect.Width()- size.cx) / 2;  
  87.         }  
  88.   
  89.   
  90.         if (Y_BOTTOM == m_yAlignment)   // 顶对齐  
  91.         {  
  92.             y = crect.Height() - size.cy;  
  93.         }  
  94.         else if (Y_CENTER == m_yAlignment)   // Y居中对齐  
  95.         {  
  96.             y = (crect.Height() - size.cy) / 2;  
  97.         }  
  98.     }  
  99.     // 设置dc字串颜色  
  100.     ::SetTextColor(lpDrawItemStruct->hDC, m_clrText);  
  101.     TextOut(lpDrawItemStruct->hDC, x, y, strCaption, strCaption.GetLength());  
  102. }  
  103.   
  104.   
  105. void CRichStatic::PreSubclassWindow()  
  106. {  
  107.     CStatic::PreSubclassWindow();  
  108.     ModifyStyle(0, SS_OWNERDRAW);  
  109. }  
  110.   
  111.   
  112. void CRichStatic::SetFont(CString strFaceName, LONG nHeight)  
  113. {  
  114.     if (m_selfCreated && m_hFont != NULL)  
  115.     {  
  116.         DeleteObject(m_hFont);    // 若字体对象为对象自己创建并且不为NULL,则销毁掉以释放内核对象  
  117.     }  
  118.     CFont cfont;  
  119.     LOGFONT lf;  
  120.     memset(&lf, 0, sizeof lf);    // 清空LOGFONT结构体,之后对其赋值  
  121.     lf.lfHeight = nHeight;  
  122.     _tcscpy_s(lf.lfFaceName, strFaceName.GetBuffer());    // 将字体名拷贝到LOGFONT结构体中  
  123.     VERIFY(cfont.CreateFontIndirect(&lf));    // 创建新的字体  
  124.     m_hFont = (HFONT)cfont.m_hObject;  
  125.     m_selfCreated = TRUE;    // 标记字体为自己创建的  
  126. }  
  127.   
  128.   
  129. void CRichStatic::SetFont(HFONT hFont)  
  130. {  
  131.     if (m_selfCreated && m_hFont != NULL)  
  132.     {  
  133.         DeleteObject(m_hFont);    // 若字体对象为对象自己创建并且不为NULL,则销毁掉以释放内核对象  
  134.     }  
  135.     m_hFont = hFont;  
  136.     m_selfCreated = FALSE;   // 标记字体非自己创建  
  137. }  
  138.   
  139.   
  140. void CRichStatic::SetFont(const CFont *pFont)  
  141. {  
  142.     if (m_selfCreated && m_hFont != NULL)  
  143.     {  
  144.         DeleteObject(m_hFont);    // 若字体对象为对象自己创建并且不为NULL,则销毁掉以释放内核对象  
  145.     }  
  146.     m_hFont = (HFONT)pFont->m_hObject;  
  147.     m_selfCreated = FALSE;   // 标记字体非自己创建  
  148. }  
  149.   
  150.   
  151. BOOL CRichStatic::OnEraseBkgnd(CDC* pDC)  
  152. {  
  153.     // 当背景色为透明时,需要保存与拷贝显示主框的显示区域  
  154.     if (m_clrBackground == CLR_NONE)  
  155.     {  
  156.         if (m_Bmp.GetSafeHandle() == NULL)  
  157.         {  
  158.             CRect Rect;  
  159.             GetWindowRect(&Rect);  
  160.             CWnd *pParent = GetParent();  
  161.             ASSERT(pParent);  
  162.             pParent->ScreenToClient(&Rect);  // 将坐标转换为与主对话框相对应  
  163.         
  164.             // 拷贝对应区域主框显示的内容  
  165.             CDC *pDC = pParent->GetDC();  
  166.             CDC MemDC;  
  167.             MemDC.CreateCompatibleDC(pDC);  
  168.             m_Bmp.CreateCompatibleBitmap(pDC,Rect.Width(),Rect.Height());  
  169.             CBitmap *pOldBmp = MemDC.SelectObject(&m_Bmp);  
  170.             MemDC.BitBlt(0,0,Rect.Width(),Rect.Height(),pDC,Rect.left,Rect.top,SRCCOPY);  
  171.             MemDC.SelectObject(pOldBmp);  
  172.             MemDC.DeleteDC();    // 删除内存DC,否则内存泄漏  
  173.             pParent->ReleaseDC(pDC);  
  174.         }  
  175.         else // 将主框显示的内容拷贝回去  
  176.         {  
  177.             CRect Rect;  
  178.             GetClientRect(Rect);  
  179.             CDC MemDC;  
  180.             MemDC.CreateCompatibleDC(pDC);  
  181.             CBitmap *pOldBmp = MemDC.SelectObject(&m_Bmp);  
  182.             pDC->BitBlt(0,0,Rect.Width(),Rect.Height(),&MemDC,0,0,SRCCOPY);  
  183.             MemDC.SelectObject(pOldBmp);  
  184.             MemDC.DeleteDC();    // 删除内存DC,否则内存泄漏  
  185.         }  
  186.     }  
  187.   
  188.   
  189.     return TRUE;  
  190. }  
  191.   
  192.   
  193. LRESULT CRichStatic::OnSetText(WPARAM wParam,LPARAM lParam)  
  194. {  
  195.     LRESULT Result = Default();  
  196.     Invalidate();  
  197.     UpdateWindow();  
  198.     return Result;  
  199. }  
from:http://blog.csdn.net/cashey1991/article/details/7545614
目录
相关文章
|
5天前
|
前端开发
css 字体渐变样式(设置字体渐变样式+附加实现源码)
css 字体渐变样式(设置字体渐变样式+附加实现源码)
7 0
|
5月前
纯html网页,如何快速把所有字体的颜色都改成黑色的,原来模板默认的字体是灰色
纯html网页,如何快速把所有字体的颜色都改成黑色的,原来模板默认的字体是灰色
|
11月前
CSS3文本居中显示、圆形圆角绘制、立体阴影效果设置实例演示
CSS3文本居中显示、圆形圆角绘制、立体阴影效果设置实例演示
92 0
设置 窗体 静态控件颜色
设置 窗体 静态控件颜色
62 0
|
程序员 API iOS开发
iOS开发:字符串设置指定内容的文字颜色、文字大小、文字字体类型
在iOS开发过程中,会有一些为了提高APP的视觉效果而设置的特别一点的效果,比如一行文字需要自定义不同的颜色和文字大小,这就用到通过富文本来设置字符串的颜色、大小和文字类型。这篇博文我打算只介绍怎么设置指定内容的一些文字属性设置,如果之前看过我写的博文,就会发现有一篇类似介绍通过富文本来设置字符串内容的博文,但是那篇是综合性的,包括介绍button的,以及UItextfield的设置,所以在这里我只介绍怎么设置字符串指定位置的一些自定义设置的方法,如有不妥之处,欢迎指正。
524 0
PyQt5 技术篇-调用字体对话框(QFontDialog)获取字体,控件设置字体。
PyQt5 技术篇-调用字体对话框(QFontDialog)获取字体,控件设置字体。
315 0
PyQt5 技术篇-调用字体对话框(QFontDialog)获取字体,控件设置字体。
Dialog是逻辑字体,实际绘制时会选择不同字体
Dialog是逻辑字体,实际绘制时会选择不同字体
193 0
修改QLabel的文本大小/颜色/背景颜色,使用QSS/QColor转换为QString的方法
修改QLabel的文本大小/颜色/背景颜色,使用QSS/QColor转换为QString的方法
513 0
|
C#
【WPF】TextBlock文本文字分段显示不同颜色
原文:【WPF】TextBlock文本文字分段显示不同颜色 需求:一行文字中,不同字符显示不同颜色。如注册页面,为表示必填项,在文本最后加一个红色的型号* 目标效果: 方法一: 用< StackPanel >嵌套两个< TextBlock >。
1990 0
|
C#
WPF去除窗体边框及白色边框
原文:WPF去除窗体边框及白色边框        0
1400 0

热门文章

最新文章