转载自:MFC中改变按钮颜色的方法。
1.使用CMFCButton类
MFC自带的MFC Button Control控件等按钮可以修改颜色,还能添加图片,设置字体颜色等。这些控件就在工具箱的下面,前面带MFC开头的那些。
在需要改颜色的地方调用m_btn.SetFaceColor(RGB(0, 255, 0));即可。
CMFCButton中的SetFaceColor()可能会显示不出颜色,需要设置
m_button_sure.m_bTransparent=FALSE;
m_button_sure.m_bDontUseWinXPTheme= TRUE;才可。
在button里面点击会有黑圈,要消除黑色圆圈需要使button里面的属性m_bDrawFocus = FALSE。
链接http://www.cnblogs.com/weiqubo/archive/2011/03/31/2000953.html
消除button的边框,不是很立体的,使
m_button_cancel.m_nFlatStyle = CMFCButton::BUTTONSTYLE_NOBORDERS; 使其样式变化,CMFCButton可以设置属性。
2.手动添加消息映射的方法实现改变按钮的颜色,不必创建新的类!
将button修改为owner draw 类型, 重载OnDrawItem函数,并对其改写,在适当的位置添加下面的语句
afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
ON_WM_DRAWITEM()
在消息响应函数里添加如下代码:
1 void CXXX::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 2 { 3 CDC dc; 4 dc.Attach(lpDrawItemStruct->hDC); 5 ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON); 6 7 CString strText; 8 ((CButton *)GetDlgItem(nIDCtl))->GetWindowText(strText); 9 10 SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT); 11 //if (lpDrawItemStruct->itemState&ODS_SELECTED) 12 { 13 CBrush brush(RGB(255, 0, 0)); 14 dc.FillRect(&(lpDrawItemStruct->rcItem), &brush); 15 DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), 16 &lpDrawItemStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER); 17 SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT); 18 } 19 dc.Detach(); 20 }
可以参考:http://blog.sina.com.cn/s/blog_65cab32d01013uad.html
3.OnCtlColor
在控件显示之前,每一个控件会向父对话框发送一个WM_CTLCOLOR消息,这个消息缺省处理函数CWnd::OnCtlColor返回一个HBRUSH类型的句柄,这样,就可以设置前景和背景文本颜色,并为控件或者对话框的非文本区域选定一个刷子。
实现:
① 先创建一个基于对话框的工程,命名为test,然后在对话框上加入一个ListBox控件。
② 在testDlg.h中加入一个成员变量:HBRUSH m_hbrush;
③ 在OnInitDialog()中,加入m_hbrush=CreateSolidBrush(RGB(0,0,0);此处设置的RGB值可以改变ListBox的背景色。为了观看ListBox中字的颜色变化,给ListBox加入几个字:利用Class Wizard给ListBox加入一个Control类型的成员变量m_ctrlListBox,然后在OnInitDialog()加入如下所示的代码:
m_ctrlListBox.AddString("第一行");
m_ctrlListBox.AddString("第二行");
④ 点击Class Wizard,给testDlg加入WM_CTLCOLOR事件,单击Edit Code按钮,然后把改函数的内容替换为如下代码:
1 if(nCtlColor== CTLCOLOR_LISTBOX) 2 { 3 pDC->SetBkMode(TRANSPARENT); 4 //此处设置字体的颜色 5 pDC->SetTextColor(RGB(255,255,255)); 6 return m_hbrush; 7 } 8 else 9 return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); 10 例: 11 // ColorChooseToolDlg.h : header file 12 // 13 #pragma once 14 15 // CColorChooseToolDlg dialog 16 class CColorChooseToolDlg : public CDialogEx 17 { 18 // Construction 19 public: 20 CColorChooseToolDlg(CWnd* pParent = NULL); // standard constructor 21 22 // Dialog Data 23 enum { IDD = IDD_COLORCHOOSETOOL_DIALOG }; 24 25 protected: 26 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support 27 28 29 // Implementation 30 protected: 31 HICON m_hIcon; 32 33 // Generated message map functions 34 virtual BOOL OnInitDialog(); 35 afx_msg void OnSysCommand(UINT nID, LPARAM lParam); 36 afx_msg void OnPaint(); 37 afx_msg HCURSOR OnQueryDragIcon(); 38 afx_msg void OnBnClickedColorButton(UINT uID); 39 DECLARE_MESSAGE_MAP() 40 41 private: 42 void initColor(); 43 CRect getFrameRect(int frameId); 44 void createColorCtr(int ctrId, int strId, int num, void *ctr, long btnStyle,int btnXPos, int btnYPos, int originalCtrId); 45 void createColorBtns(); 46 void createColorString(); 47 48 private: 49 enum { COLORSTR_NUM = 3, COLORBTN_NUM = 3}; 50 int ctrId; 51 int btnId; 52 CStatic colorStr[COLORSTR_NUM]; 53 CMFCButton colorBtn[COLORBTN_NUM]; 54 COLORREF color[COLORBTN_NUM]; 55 }; 56 57 // ColorChooseToolDlg.cpp : implementation file 58 // 59 60 #include "stdafx.h" 61 #include "ColorChooseTool.h" 62 #include "ColorChooseToolDlg.h" 63 #include "afxdialogex.h" 64 65 #ifdef _DEBUG 66 #define new DEBUG_NEW 67 #endif 68 69 #define BUTTON_HEIGHT 30 70 #define ADJACENT_BTN_DISTANCE 80 71 #define STRING_CONTROL 0 72 #define BUTTON_CONTROL 1 73 74 // CAboutDlg dialog used for App About 75 class CAboutDlg : public CDialogEx 76 { 77 public: 78 CAboutDlg(); 79 // Dialog Data 80 enum { IDD = IDD_ABOUTBOX }; 81 protected: 82 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support 83 // Implementation 84 protected: 85 DECLARE_MESSAGE_MAP() 86 }; 87 88 CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) 89 { 90 } 91 92 void CAboutDlg::DoDataExchange(CDataExchange* pDX) 93 { 94 CDialogEx::DoDataExchange(pDX); 95 } 96 97 BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) 98 END_MESSAGE_MAP() 99 100 // CColorChooseToolDlg dialog 101 CColorChooseToolDlg::CColorChooseToolDlg(CWnd* pParent /*=NULL*/) 102 : CDialogEx(CColorChooseToolDlg::IDD, pParent) 103 { 104 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 105 initColor(); 106 } 107 108 void CColorChooseToolDlg::DoDataExchange(CDataExchange* pDX) 109 { 110 CDialogEx::DoDataExchange(pDX); 111 } 112 113 BEGIN_MESSAGE_MAP(CColorChooseToolDlg, CDialogEx) 114 ON_WM_SYSCOMMAND() 115 ON_WM_PAINT() 116 ON_WM_QUERYDRAGICON() 117 ON_COMMAND_RANGE(IDC_CHOOSE_COLOR_A_BUTTON, IDC_CHOOSE_COLOR_C_BUTTON, OnBnClickedColorButton) 118 END_MESSAGE_MAP() 119 120 121 // CColorChooseToolDlg message handlers 122 123 BOOL CColorChooseToolDlg::OnInitDialog() 124 { 125 CDialogEx::OnInitDialog(); 126 127 // Add "About..." menu item to system menu. 128 129 // IDM_ABOUTBOX must be in the system command range. 130 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 131 ASSERT(IDM_ABOUTBOX < 0xF000); 132 133 CMenu* pSysMenu = GetSystemMenu(FALSE); 134 if (pSysMenu != NULL) 135 { 136 BOOL bNameValid; 137 CString strAboutMenu; 138 bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); 139 ASSERT(bNameValid); 140 if (!strAboutMenu.IsEmpty()) 141 { 142 pSysMenu->AppendMenu(MF_SEPARATOR); 143 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 144 } 145 } 146 147 // Set the icon for this dialog. The framework does this automatically 148 // when the application's main window is not a dialog 149 SetIcon(m_hIcon, TRUE); // Set big icon 150 SetIcon(m_hIcon, FALSE); // Set small icon 151 152 // TODO: Add extra initialization here 153 createColorString(); 154 createColorBtns(); 155 156 return TRUE; // return TRUE unless you set the focus to a control 157 } 158 159 void CColorChooseToolDlg::OnSysCommand(UINT nID, LPARAM lParam) 160 { 161 if ((nID & 0xFFF0) == IDM_ABOUTBOX) 162 { 163 CAboutDlg dlgAbout; 164 dlgAbout.DoModal(); 165 } 166 else 167 { 168 CDialogEx::OnSysCommand(nID, lParam); 169 } 170 } 171 172 // If you add a minimize button to your dialog, you will need the code below 173 // to draw the icon. For MFC applications using the document/view model, 174 // this is automatically done for you by the framework. 175 176 void CColorChooseToolDlg::OnPaint() 177 { 178 if (IsIconic()) 179 { 180 CPaintDC dc(this); // device context for painting 181 182 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); 183 184 // Center icon in client rectangle 185 int cxIcon = GetSystemMetrics(SM_CXICON); 186 int cyIcon = GetSystemMetrics(SM_CYICON); 187 CRect rect; 188 GetClientRect(&rect); 189 int x = (rect.Width() - cxIcon + 1) / 2; 190 int y = (rect.Height() - cyIcon + 1) / 2; 191 192 // Draw the icon 193 dc.DrawIcon(x, y, m_hIcon); 194 } 195 else 196 { 197 CDialogEx::OnPaint(); 198 } 199 } 200 201 // The system calls this function to obtain the cursor to display while the user drags 202 // the minimized window. 203 HCURSOR CColorChooseToolDlg::OnQueryDragIcon() 204 { 205 return static_cast<HCURSOR>(m_hIcon); 206 } 207 208 //Click different radio button to update corresponding checkbox buttons. 209 void CColorChooseToolDlg::OnBnClickedColorButton(UINT uID) 210 { 211 // TODO: Add your control notification handler code here 212 btnId = uID - IDC_CHOOSE_COLOR_A_BUTTON; 213 CColorDialog colorDlg(color[btnId]); 214 215 if (IDOK == colorDlg.DoModal()) 216 { 217 color[btnId] = colorDlg.GetColor(); 218 colorBtn[btnId].m_bTransparent = FALSE; 219 colorBtn[btnId].m_bDontUseWinXPTheme = TRUE; 220 colorBtn[btnId].m_bDrawFocus = FALSE; 221 colorBtn[btnId].SetFaceColor(color[btnId]); 222 } 223 } 224 225 void CColorChooseToolDlg::initColor() 226 { 227 int i; 228 for(i = 0;i < COLORBTN_NUM;i++) 229 { 230 color[i] = RGB(255, 0, 0); 231 } 232 } 233 234 CRect CColorChooseToolDlg::getFrameRect(int frameId) 235 { 236 CRect frameRect; 237 GetDlgItem(frameId)->GetWindowRect(&frameRect); 238 ScreenToClient(frameRect); 239 return frameRect; 240 } 241 242 void CColorChooseToolDlg::createColorString() 243 { 244 CRect frameRect; 245 frameRect = getFrameRect(IDC_STATIC); 246 const int strXpos = frameRect.left - 4 * BUTTON_HEIGHT; 247 const int strYpos = frameRect.top + BUTTON_HEIGHT + 10; 248 249 createColorCtr(0, IDS_STRING102, COLORSTR_NUM, colorStr, WS_CHILD|WS_VISIBLE, strXpos, strYpos, IDC_CHOOSE_COLOR_A_STRING); 250 } 251 252 void CColorChooseToolDlg::createColorCtr(int id, int strId, int num, void *ctr, long btnStyle,int btnXPos, int btnYPos, int originalCtrId) 253 { 254 int length; 255 CString strCtrName; 256 CClientDC dc(this); 257 dc.SelectObject(GetFont()); 258 259 for(ctrId=0; ctrId<num; ctrId++) 260 { 261 strCtrName.LoadStringW(strId+ctrId); 262 length = dc.GetTextExtent(strCtrName).cx + 2 * BUTTON_HEIGHT; 263 if(STRING_CONTROL == id) { 264 ((CStatic*)ctr)[ctrId].Create(strCtrName, btnStyle, CRect(btnXPos, btnYPos+ctrId*ADJACENT_BTN_DISTANCE, length, BUTTON_HEIGHT), this, originalCtrId+ctrId); 265 ((CStatic*)ctr)[ctrId].MoveWindow(btnXPos, btnYPos+ctrId*ADJACENT_BTN_DISTANCE, length, BUTTON_HEIGHT); 266 ((CStatic*)ctr)[ctrId].SetFont(GetFont(),TRUE); 267 }else if(BUTTON_CONTROL == id) { 268 ((CMFCButton*)ctr)[ctrId].Create(strCtrName, btnStyle, CRect(btnXPos, btnYPos+ctrId*ADJACENT_BTN_DISTANCE, length, BUTTON_HEIGHT), this, originalCtrId+ctrId); 269 ((CMFCButton*)ctr)[ctrId].MoveWindow(btnXPos, btnYPos+ctrId*ADJACENT_BTN_DISTANCE, length, BUTTON_HEIGHT); 270 ((CMFCButton*)ctr)[ctrId].SetFont(GetFont(),TRUE); 271 } 272 } 273 } 274 275 void CColorChooseToolDlg::createColorBtns() 276 { 277 CRect frameRect; 278 frameRect = getFrameRect(IDC_STATIC); 279 const int btnXpos = frameRect.left + 2 * BUTTON_HEIGHT; 280 const int btnYpos = frameRect.top + BUTTON_HEIGHT; 281 282 createColorCtr(1, IDS_STRING105, COLORBTN_NUM, colorBtn, WS_CHILD|WS_VISIBLE|WS_TABSTOP, btnXpos, btnYpos, IDC_CHOOSE_COLOR_A_BUTTON); 283 }