静态文本控件背景颜色为绿色字体为透明色。
相关代码:
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;}运行程序