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


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

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

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

            默认背景色是透明的

    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)


    源码:

    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. };  

    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
  • 相关阅读:
    X 如何在mysql客户端即mysql提示符下执行操作系统命令
    X MySQL UNDO表空间独立和截断 ,mysql undo 的历史
    X Mysql5.7忘记root密码及mysql5.7修改root密码的方法
    X mysql密码的安全策略ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    二分法查找
    elasticsearch API
    logstash的基础
    elasticsearch的基础
    Linux的进程管理
    Linux的网卡配置
  • 原文地址:https://www.cnblogs.com/lidabo/p/3398301.html
Copyright © 2020-2023  润新知