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. // CRichStatic
  3. class CRichStatic : public CStatic
  4. {
  5. DECLARE_DYNAMIC(CRichStatic)
  6. public:
  7. CRichStatic();
  8. virtual ~CRichStatic();
  9. protected:
  10. afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  11. afx_msg LRESULT OnSetText(WPARAM,LPARAM);
  12. DECLARE_MESSAGE_MAP()
  13. virtual void PreSubclassWindow();
  14. private:
  15. COLORREF m_clrText;          // 文字前景色
  16. COLORREF m_clrBackground;    // 文字背景色
  17. CFont *m_pTextFont;          // 文字字体
  18. CBitmap m_Bmp;               // 保存背景用的位图对象
  19. public:
  20. // 设置背景色(若clr为CLR_NONE,则背景透明)
  21. void SetBackgroundColor(COLORREF clr){m_clrBackground = clr;}
  22. // 设置文字前景色
  23. void SetTextColor(COLORREF clr){m_clrText = clr;}
  24. // 设置文字字体
  25. void SetFont(CString strFaceName, LONG nHeight);
  26. public:
  27. virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
  28. };

  1. // RichStatic.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "RichStatic.h"
  5. // CRichStatic
  6. IMPLEMENT_DYNAMIC(CRichStatic, CStatic)
  7. CRichStatic::CRichStatic():
  8. m_clrText(0), m_clrBackground(CLR_NONE), m_hFont(NULL), m_selfCreated(FALSE),
  9. m_xAlignment(X_LEFT), m_yAlignment(Y_TOP)
  10. {
  11. }
  12. CRichStatic::~CRichStatic()
  13. {
  14. if (m_selfCreated && m_hFont != NULL)
  15. {
  16. DeleteObject(m_hFont);    // 若字体对象为对象自己创建并且不为NULL,则销毁掉以释放内核对象
  17. }
  18. }
  19. BEGIN_MESSAGE_MAP(CRichStatic, CStatic)
  20. ON_MESSAGE(WM_SETTEXT,OnSetText)
  21. ON_WM_ERASEBKGND()
  22. END_MESSAGE_MAP()
  23. // CRichStatic 消息处理程序
  24. void CRichStatic::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  25. {
  26. if (m_clrBackground != CLR_NONE)    // 若背景色不为CLR_NONE(CLR_NONE表示无背景色),则绘制背景
  27. {
  28. RECT rect;
  29. GetWindowRect(&rect);
  30. CBrush brush;
  31. brush.CreateSolidBrush(m_clrBackground);
  32. ::SelectObject(lpDrawItemStruct->hDC, brush.m_hObject);    // 设置画刷颜色
  33. ::SelectObject(lpDrawItemStruct->hDC, GetStockObject(NULL_PEN));    // 设置笔为空笔(不绘制边界)
  34. Rectangle(lpDrawItemStruct->hDC, 0, 0,rect.right - rect.left, rect.bottom - rect.top);
  35. }
  36. CString strCaption;    // 标题文字
  37. GetWindowText(strCaption);
  38. if (m_hFont != NULL)
  39. {
  40. ::SelectObject(lpDrawItemStruct->hDC, m_hFont);
  41. }
  42. // 计算输出字串的横纵坐标
  43. int x = 0, y = 0;
  44. if (X_LEFT != m_xAlignment || Y_TOP != m_yAlignment)    // 不是左对齐或不是顶对齐
  45. {
  46. CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  47. CRect crect;
  48. GetWindowRect(&crect);
  49. CSize size = pDC->GetTextExtent(strCaption);
  50. if (X_RIGHT == m_xAlignment)    // 右对齐
  51. {
  52. x = crect.Width() - size.cx;
  53. }
  54. else if (X_CENTER == m_xAlignment)   // X居中对齐
  55. {
  56. x = (crect.Width()- size.cx) / 2;
  57. }
  58. if (Y_BOTTOM == m_yAlignment)   // 顶对齐
  59. {
  60. y = crect.Height() - size.cy;
  61. }
  62. else if (Y_CENTER == m_yAlignment)   // Y居中对齐
  63. {
  64. y = (crect.Height() - size.cy) / 2;
  65. }
  66. }
  67. // 设置dc字串颜色
  68. ::SetTextColor(lpDrawItemStruct->hDC, m_clrText);
  69. TextOut(lpDrawItemStruct->hDC, x, y, strCaption, strCaption.GetLength());
  70. }
  71. void CRichStatic::PreSubclassWindow()
  72. {
  73. CStatic::PreSubclassWindow();
  74. ModifyStyle(0, SS_OWNERDRAW);
  75. }
  76. void CRichStatic::SetFont(CString strFaceName, LONG nHeight)
  77. {
  78. if (m_selfCreated && m_hFont != NULL)
  79. {
  80. DeleteObject(m_hFont);    // 若字体对象为对象自己创建并且不为NULL,则销毁掉以释放内核对象
  81. }
  82. CFont cfont;
  83. LOGFONT lf;
  84. memset(&lf, 0, sizeof lf);    // 清空LOGFONT结构体,之后对其赋值
  85. lf.lfHeight = nHeight;
  86. _tcscpy_s(lf.lfFaceName, strFaceName.GetBuffer());    // 将字体名拷贝到LOGFONT结构体中
  87. VERIFY(cfont.CreateFontIndirect(&lf));    // 创建新的字体
  88. m_hFont = (HFONT)cfont.m_hObject;
  89. m_selfCreated = TRUE;    // 标记字体为自己创建的
  90. }
  91. void CRichStatic::SetFont(HFONT hFont)
  92. {
  93. if (m_selfCreated && m_hFont != NULL)
  94. {
  95. DeleteObject(m_hFont);    // 若字体对象为对象自己创建并且不为NULL,则销毁掉以释放内核对象
  96. }
  97. m_hFont = hFont;
  98. m_selfCreated = FALSE;   // 标记字体非自己创建
  99. }
  100. void CRichStatic::SetFont(const CFont *pFont)
  101. {
  102. if (m_selfCreated && m_hFont != NULL)
  103. {
  104. DeleteObject(m_hFont);    // 若字体对象为对象自己创建并且不为NULL,则销毁掉以释放内核对象
  105. }
  106. m_hFont = (HFONT)pFont->m_hObject;
  107. m_selfCreated = FALSE;   // 标记字体非自己创建
  108. }
  109. BOOL CRichStatic::OnEraseBkgnd(CDC* pDC)
  110. {
  111. // 当背景色为透明时,需要保存与拷贝显示主框的显示区域
  112. if (m_clrBackground == CLR_NONE)
  113. {
  114. if (m_Bmp.GetSafeHandle() == NULL)
  115. {
  116. CRect Rect;
  117. GetWindowRect(&Rect);
  118. CWnd *pParent = GetParent();
  119. ASSERT(pParent);
  120. pParent->ScreenToClient(&Rect);  // 将坐标转换为与主对话框相对应
  121. // 拷贝对应区域主框显示的内容
  122. CDC *pDC = pParent->GetDC();
  123. CDC MemDC;
  124. MemDC.CreateCompatibleDC(pDC);
  125. m_Bmp.CreateCompatibleBitmap(pDC,Rect.Width(),Rect.Height());
  126. CBitmap *pOldBmp = MemDC.SelectObject(&m_Bmp);
  127. MemDC.BitBlt(0,0,Rect.Width(),Rect.Height(),pDC,Rect.left,Rect.top,SRCCOPY);
  128. MemDC.SelectObject(pOldBmp);
  129. MemDC.DeleteDC();    // 删除内存DC,否则内存泄漏
  130. pParent->ReleaseDC(pDC);
  131. }
  132. else // 将主框显示的内容拷贝回去
  133. {
  134. CRect Rect;
  135. GetClientRect(Rect);
  136. CDC MemDC;
  137. MemDC.CreateCompatibleDC(pDC);
  138. CBitmap *pOldBmp = MemDC.SelectObject(&m_Bmp);
  139. pDC->BitBlt(0,0,Rect.Width(),Rect.Height(),&MemDC,0,0,SRCCOPY);
  140. MemDC.SelectObject(pOldBmp);
  141. MemDC.DeleteDC();    // 删除内存DC,否则内存泄漏
  142. }
  143. }
  144. return TRUE;
  145. }
  146. LRESULT CRichStatic::OnSetText(WPARAM wParam,LPARAM lParam)
  147. {
  148. LRESULT Result = Default();
  149. Invalidate();
  150. UpdateWindow();
  151. return Result;
  152. }
from:http://blog.csdn.net/cashey1991/article/details/7545614

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

  1. MFC小程序003------MFC使用WebBrowser组件,在对话框中创建滚动视图,动态创建一个静态文本控件并设置鼠标单击的消息响应

    MFC小程序截图: 一.在MFC中简单使用WebBrowser的ActiveX插件的方法: 见博文:  http://blog.csdn.net/supermanking/article/detail ...

  2. 让图片适合在静态文本控件窗口大小 MFC

    1.加入 IDC_STATIC1 静态文本控件. 2.为文本控件添加 STATIC 型变量 3.在OnInitDialog函数中添加以下代码 //设置该静态控件为显示位图的 m_bitmap.Modi ...

  3. VS2010,MFC动态按钮和窗体背景图片,以及是静态文字控件透明,并避免静态文字刷新出现的重叠问题

    1.动态按钮的四种动作 1)正常 2)按下 3)滑过 4)失效 在MFC中,4个动作对应着四种位图bmp, 首先,将代表四种状态的位图加载入资源中,将对应的按钮设置为BitmapButton 第二,在 ...

  4. ASP.Net ListBox DropdownList 不同条目设置背景色和字体颜色( 转· 载 )

    ASP.Net ListBox DropdownList 不同条目设置背景色和字体颜色 2009-09-30  来自:真有意思 [ZU14.CN]  字体大小:[大 中 小] 摘要:在HTML展现页面 ...

  5. WPF通过不透明蒙板切割显示子控件

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Backspace110/article/ ...

  6. PNG透明窗体全攻略(控件不透明)

    http://blog.csdn.net/riklin/article/details/4417247 看好了,这是XP系统,未装.net.我的Photoshop学的不太好,把玻璃片弄的太透了些,如果 ...

  7. 一个字体,大小,颜色可定义的自绘静态框控件-XColorStatic 类(比较好看,一共19篇自绘文章)

    翻译来源:https://www.codeproject.com/Articles/5242/XColorStatic-a-colorizing-static-control XColor Stati ...

  8. 窗体透明,但窗体上的控件不透明(简单好用)good

    1.在Delphi中,设置窗体的AlphaBlend := true;AlphaBlendValue := 0-255; AlphaBlendValue越小窗体的透明度就越高.这种方法将会使窗体和窗体 ...

  9. (Winform)控件中添加GIF图片以及运用双缓冲使其不闪烁以及背景是gif时使控件(如panel)变透明

    Image img = Image.FromFile(@"C:\Users\joeymary\Desktop\3.gif"); pictureBox1.Image =img.Clo ...

随机推荐

  1. linux初学(CentOS)之注意事项(一)

    linux严格区分大小写(命令,文件名,用户名等) linux所有内容以文件形式保存,包括硬件 硬盘文件是/dev/sd[a-p](a,p为盘符名) 光盘文件是/dev/sr0等 linux不靠扩展名 ...

  2. 数独破解c++代码

    数独破解c++代码 #include <iostream> #include <cstring> #include <cstdio> #include <st ...

  3. Delphi下创建异形窗体

    procedure TForm1.FormCreate(Sender: TObject);var  pt: array [0 .. 4] of TPoint;  m_rgn: HRGN;begin   ...

  4. 谷歌page speed 安装使用及页面问题详解

    原文地址:http://wenku.baidu.com/view/b0a61f3ebcd126fff7050b40.html 谷歌page speed 安装使用及页面问题详解 谷歌page speed ...

  5. IE 创建条件样式

    1.HTML 注释 <div id="header">Header Section</div> <!-- End Header Section Con ...

  6. MAX Average Problem(斜率优化dp)

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. 面试题之——将文件夹下java文件写入到新的文件夹,并修改扩展名

    题目:将d:/code/java文件夹下的所有.java文件复制到d:/code/java/jad文件夹下并且将原来的文件的扩展名.java改为.jad 源代码: package com.zyh.in ...

  8. [置顶] ※数据结构※→☆非线性结构(tree)☆============树结点 链式存储结构(tree node list)(十四)

    结点: 包括一个数据元素及若干个指向其它子树的分支:例如,A,B,C,D等. 在数据结构的图形表示中,对于数据集合中的每一个数据元素用中间标有元素值的方框表示,一般称之为数据结点,简称结点. 在C语言 ...

  9. C#中各种计时器

    1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch) Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 S ...

  10. sessionStorage和localStorage之间的差别

    <!DOCTYPE html><html> <head lang="en"> <meta charset="utf-8" ...