news 2026/8/14 6:31:22

mfc静态文本控件背景及字体颜色设置实例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
mfc静态文本控件背景及字体颜色设置实例

静态文本控件背景颜色为绿色字体为透明色。
相关代码:

CBrush m_brush1;afx_msg HBRUSHOnCtlColor(CDC*pDC,CWnd*pWnd,UINT nCtlColor);ON_WM_CTLCOLOR()m_brush1.CreateSolidBrush(RGB(0,255,0));//绿色HBRUSHCColorDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINT nCtlColor){HBRUSH hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);if((pWnd->GetDlgCtrlID()==IDC_STATIC)){pDC->SetTextColor(RGB(255,255,255));//设置文本框字体颜色pDC->SetBkMode(TRANSPARENT);return(HBRUSH)m_brush1;}returnhbr;}

完整代码:
ColorDlg.h

// ColorDlg.h : header file//#if!defined(AFX_COLORDLG_H__98214934_35D9_4788_B193_BDFE2D279BE6__INCLUDED_)#defineAFX_COLORDLG_H__98214934_35D9_4788_B193_BDFE2D279BE6__INCLUDED_#if_MSC_VER>1000#pragmaonce#endif// _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////// CColorDlg dialogclassCColorDlg:publicCDialog{// Constructionpublic:CColorDlg(CWnd*pParent=NULL);// standard constructorCBrush m_brush1;// Dialog Data//{{AFX_DATA(CColorDlg)enum{IDD=IDD_COLOR_DIALOG};// NOTE: the ClassWizard will add data members here//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CColorDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);// DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(CColorDlg)virtualBOOLOnInitDialog();afx_msgvoidOnSysCommand(UINT nID,LPARAM lParam);afx_msgvoidOnPaint();afx_msg HCURSOROnQueryDragIcon();afx_msg HBRUSHOnCtlColor(CDC*pDC,CWnd*pWnd,UINT nCtlColor);//}}AFX_MSGDECLARE_MESSAGE_MAP()};//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif// !defined(AFX_COLORDLG_H__98214934_35D9_4788_B193_BDFE2D279BE6__INCLUDED_)

ColorDlg.cpp

// ColorDlg.cpp : implementation file//#include"stdafx.h"#include"Color.h"#include"ColorDlg.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App AboutclassCAboutDlg:publicCDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);// DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg():CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}voidCAboutDlg::DoDataExchange(CDataExchange*pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CColorDlg dialogCColorDlg::CColorDlg(CWnd*pParent/*=NULL*/):CDialog(CColorDlg::IDD,pParent){//{{AFX_DATA_INIT(CColorDlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);}voidCColorDlg::DoDataExchange(CDataExchange*pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CColorDlg)// NOTE: the ClassWizard will add DDX and DDV calls here//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CColorDlg,CDialog)//{{AFX_MSG_MAP(CColorDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_WM_CTLCOLOR()//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CColorDlg message handlersBOOLCColorDlg::OnInitDialog(){CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX<0xF000);CMenu*pSysMenu=GetSystemMenu(FALSE);if(pSysMenu!=NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if(!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon,TRUE);// Set big iconSetIcon(m_hIcon,FALSE);// Set small icon// TODO: Add extra initialization herem_brush1.CreateSolidBrush(RGB(0,255,0));//绿色returnTRUE;// return TRUE unless you set the focus to a control}voidCColorDlg::OnSysCommand(UINT nID,LPARAM lParam){if((nID&0xFFF0)==IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID,lParam);}}// If you add a minimize button to your dialog, you will need the code below// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.voidCColorDlg::OnPaint(){if(IsIconic()){CPaintDCdc(this);// device context for paintingSendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);// Center icon in client rectangleintcxIcon=GetSystemMetrics(SM_CXICON);intcyIcon=GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);intx=(rect.Width()-cxIcon+1)/2;inty=(rect.Height()-cyIcon+1)/2;// Draw the icondc.DrawIcon(x,y,m_hIcon);}else{CDialog::OnPaint();}}// The system calls this to obtain the cursor to display while the user drags// the minimized window.HCURSORCColorDlg::OnQueryDragIcon(){return(HCURSOR)m_hIcon;}HBRUSHCColorDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINT nCtlColor){HBRUSH hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);if((pWnd->GetDlgCtrlID()==IDC_STATIC)){pDC->SetTextColor(RGB(255,255,255));//设置文本框字体颜色pDC->SetBkMode(TRANSPARENT);return(HBRUSH)m_brush1;}returnhbr;}

运行程序

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/10 11:03:48

终极指南:RdpGamepad远程桌面手柄控制完整方案

还在为远程桌面无法使用游戏手柄而烦恼吗&#xff1f;RdpGamepad正是你需要的解决方案&#xff01;这款强大的远程桌面插件专门针对Xbox游戏手柄设计&#xff0c;让远程游戏和操作变得像本地一样流畅自然。 【免费下载链接】RdpGamepad Remote Desktop Plugin for Xbox Gamepad…

作者头像 李华
网站建设 2026/8/2 5:54:17

OpenWrt网络加速完全指南:5个技巧让路由器性能翻倍

OpenWrt网络加速完全指南&#xff1a;5个技巧让路由器性能翻倍 【免费下载链接】turboacc 一个适用于官方openwrt(22.03/23.05/24.10) firewall4的turboacc 项目地址: https://gitcode.com/gh_mirrors/tu/turboacc 还在为路由器网络卡顿而烦恼吗&#xff1f;turboacc网络…

作者头像 李华
网站建设 2026/8/13 16:59:09

Vivado在苹果Silicon Mac上的终极安装指南:避开5大常见陷阱

Vivado在苹果Silicon Mac上的终极安装指南&#xff1a;避开5大常见陷阱 【免费下载链接】vivado-on-silicon-mac Installs Vivado on M1/M2 macs 项目地址: https://gitcode.com/gh_mirrors/vi/vivado-on-silicon-mac Vivado-on-Silicon-Mac是一个开源工具&#xff0c;专…

作者头像 李华
网站建设 2026/8/11 6:29:31

CEF4Delphi:传统开发框架的现代进化论

CEF4Delphi&#xff1a;传统开发框架的现代进化论 【免费下载链接】CEF4Delphi CEF4Delphi is an open source project to embed Chromium-based browsers in applications made with Delphi or Lazarus/FPC for Windows, Linux and MacOS. 项目地址: https://gitcode.com/gh…

作者头像 李华
网站建设 2026/8/14 4:42:10

中文语义向量化终极指南:text2vec-base-chinese快速上手教程

中文语义向量化终极指南&#xff1a;text2vec-base-chinese快速上手教程 【免费下载链接】text2vec-base-chinese 项目地址: https://ai.gitcode.com/hf_mirrors/ai-gitcode/text2vec-base-chinese 想要让计算机真正理解中文文本的深层含义吗&#xff1f;text2vec-base…

作者头像 李华
网站建设 2026/8/11 7:38:14

PyInstaller Extractor 终极指南:轻松解包Python可执行文件

PyInstaller Extractor 终极指南&#xff1a;轻松解包Python可执行文件 【免费下载链接】pyinstxtractor PyInstaller Extractor 项目地址: https://gitcode.com/gh_mirrors/py/pyinstxtractor PyInstaller Extractor 是一款专门用于从PyInstaller打包的可执行文件中提取…

作者头像 李华