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()
在消息响应函数里添加如下代码:
void CXXX::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
CString strText;
((CButton *)GetDlgItem(nIDCtl))->GetWindowText(strText);
SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
//if (lpDrawItemStruct->itemState&ODS_SELECTED)
{
CBrush brush(RGB(255, 0, 0));
dc.FillRect(&(lpDrawItemStruct->rcItem), &brush);
DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
}
dc.Detach();
}
可以参考: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按钮,然后把改函数的内容替换为如下代码: if(nCtlColor== CTLCOLOR_LISTBOX) { pDC->SetBkMode(TRANSPARENT); //此处设置字体的颜色 pDC->SetTextColor(RGB(255,255,255)); return m_hbrush; } else return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);例:
// ColorChooseToolDlg.h : header file
//
#pragma once
// CColorChooseToolDlg dialog
class CColorChooseToolDlg : public CDialogEx
{
// Construction
public:
CColorChooseToolDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_COLORCHOOSETOOL_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnBnClickedColorButton(UINT uID);
DECLARE_MESSAGE_MAP()
private:
void initColor();
CRect getFrameRect(int frameId);
void createColorCtr(int ctrId, int strId, int num, void *ctr, long btnStyle,int btnXPos, int btnYPos, int originalCtrId);
void createColorBtns();
void createColorString();
private:
enum { COLORSTR_NUM = 3, COLORBTN_NUM = 3};
int ctrId;
int btnId;
CStatic colorStr[COLORSTR_NUM];
CMFCButton colorBtn[COLORBTN_NUM];
COLORREF color[COLORBTN_NUM];
};
// ColorChooseToolDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ColorChooseTool.h"
#include "ColorChooseToolDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define BUTTON_HEIGHT 30
#define ADJACENT_BTN_DISTANCE 80
#define STRING_CONTROL 0
#define BUTTON_CONTROL 1
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CColorChooseToolDlg dialog
CColorChooseToolDlg::CColorChooseToolDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CColorChooseToolDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
initColor();
}
void CColorChooseToolDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CColorChooseToolDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND_RANGE(IDC_CHOOSE_COLOR_A_BUTTON, IDC_CHOOSE_COLOR_C_BUTTON, OnBnClickedColorButton)
END_MESSAGE_MAP()
// CColorChooseToolDlg message handlers
BOOL CColorChooseToolDlg::OnInitDialog()
{
CDialogEx::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)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
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 dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
createColorString();
createColorBtns();
return TRUE; // return TRUE unless you set the focus to a control
}
void CColorChooseToolDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::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.
void CColorChooseToolDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CColorChooseToolDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
//Click different radio button to update corresponding checkbox buttons.
void CColorChooseToolDlg::OnBnClickedColorButton(UINT uID)
{
// TODO: Add your control notification handler code here
btnId = uID - IDC_CHOOSE_COLOR_A_BUTTON;
CColorDialog colorDlg(color[btnId]);
if (IDOK == colorDlg.DoModal())
{
color[btnId] = colorDlg.GetColor();
colorBtn[btnId].m_bTransparent = FALSE;
colorBtn[btnId].m_bDontUseWinXPTheme = TRUE;
colorBtn[btnId].m_bDrawFocus = FALSE;
colorBtn[btnId].SetFaceColor(color[btnId]);
}
}
void CColorChooseToolDlg::initColor()
{
int i;
for(i = 0;i < COLORBTN_NUM;i++)
{
color[i] = RGB(255, 0, 0);
}
}
CRect CColorChooseToolDlg::getFrameRect(int frameId)
{
CRect frameRect;
GetDlgItem(frameId)->GetWindowRect(&frameRect);
ScreenToClient(frameRect);
return frameRect;
}
void CColorChooseToolDlg::createColorString()
{
CRect frameRect;
frameRect = getFrameRect(IDC_STATIC);
const int strXpos = frameRect.left - 4 * BUTTON_HEIGHT;
const int strYpos = frameRect.top + BUTTON_HEIGHT + 10;
createColorCtr(0, IDS_STRING102, COLORSTR_NUM, colorStr, WS_CHILD|WS_VISIBLE, strXpos, strYpos, IDC_CHOOSE_COLOR_A_STRING);
}
void CColorChooseToolDlg::createColorCtr(int id, int strId, int num, void *ctr, long btnStyle,int btnXPos, int btnYPos, int originalCtrId)
{
int length;
CString strCtrName;
CClientDC dc(this);
dc.SelectObject(GetFont());
for(ctrId=0; ctrId<num; ctrId++)
{
strCtrName.LoadStringW(strId+ctrId);
length = dc.GetTextExtent(strCtrName).cx + 2 * BUTTON_HEIGHT;
if(STRING_CONTROL == id) {
((CStatic*)ctr)[ctrId].Create(strCtrName, btnStyle, CRect(btnXPos, btnYPos+ctrId*ADJACENT_BTN_DISTANCE, length, BUTTON_HEIGHT), this, originalCtrId+ctrId);
((CStatic*)ctr)[ctrId].MoveWindow(btnXPos, btnYPos+ctrId*ADJACENT_BTN_DISTANCE, length, BUTTON_HEIGHT);
((CStatic*)ctr)[ctrId].SetFont(GetFont(),TRUE);
}else if(BUTTON_CONTROL == id) {
((CMFCButton*)ctr)[ctrId].Create(strCtrName, btnStyle, CRect(btnXPos, btnYPos+ctrId*ADJACENT_BTN_DISTANCE, length, BUTTON_HEIGHT), this, originalCtrId+ctrId);
((CMFCButton*)ctr)[ctrId].MoveWindow(btnXPos, btnYPos+ctrId*ADJACENT_BTN_DISTANCE, length, BUTTON_HEIGHT);
((CMFCButton*)ctr)[ctrId].SetFont(GetFont(),TRUE);
}
}
}
void CColorChooseToolDlg::createColorBtns()
{
CRect frameRect;
frameRect = getFrameRect(IDC_STATIC);
const int btnXpos = frameRect.left + 2 * BUTTON_HEIGHT;
const int btnYpos = frameRect.top + BUTTON_HEIGHT;
createColorCtr(1, IDS_STRING105, COLORBTN_NUM, colorBtn, WS_CHILD|WS_VISIBLE|WS_TABSTOP, btnXpos, btnYpos, IDC_CHOOSE_COLOR_A_BUTTON);
}