<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    MFC 可以設置背景色、字體、字體顏色、透明背景的 Static 靜態文本控件

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    MFC庫里沒有符合這個條件的控件,于是我自己寫了一個,初步測試有效。

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

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

            默認背景色是透明的

    [cpp] view plaincopy
    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 plaincopy
    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 plaincopy
    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/7545614RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全