我的代码:
HBRUSH CStylebtnDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
CBrush m_brush; if(pWnd->GetDlgCtrlID()==IDC_LINE_WIDTH)
{
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkColor(RGB(0,0,255));
return m_brush;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
但不起效果!
HBRUSH CStylebtnDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
CBrush m_brush; if(pWnd->GetDlgCtrlID()==IDC_LINE_WIDTH)
{
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkColor(RGB(0,0,255));
return m_brush;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
但不起效果!
解决方案 »
- 虚拟漫游加小地图
- 请问Cgridctrl网格控件中怎么样设置文字颜色的?
- 如何将BMP图片保存到默认的路径下
- 控件属性页(Apply响应)问题请教。
- 找一个精通NSIS的朋友交流
- 我用PostMessage(hw,WM_CLOSE,0,0)能关闭另一个程序,但是用PostMessage(hw,WM_KEYDOWN,VK_NEXT,0)却没有反应。
- OLE DB如何定位到纪录集中满足条件的记录上去?难道要用MOVENEXT()一个一个的查找吗?
- 假设MFC中实时监控文件夹内所有音频文件与列表一致,用ReadDirectoryChangesW具体该怎么写呢?
- 求哪位大牛将DELPHI代码翻译成C++代码下
- HTML Help Workshop做帮助文件时,目录效果出现不了,请支招
- 如何调用非unicode 的win32 API?
- 怎么添加一个子项目作为树控件的第一个子项目
至于按钮的颜色修改,可以去vckbase网站下载颜色按钮的类,通过自绘制的方式实现
{
DECLARE_DYNAMIC(CColorButton)public:
void SetBackColor(COLORREF cr); //设置按钮的背景颜色
private:
COLORREF m_BackColor; //按钮背景颜色
public:
BOOL OnEraseBkgnd(CDC* pDC);
CColorButton();
virtual ~CColorButton();protected:
DECLARE_MESSAGE_MAP()
};#include "ColorButton.h"
// CColorButtonIMPLEMENT_DYNAMIC(CColorButton, CButton)CColorButton::CColorButton()
{
m_BackColor=RGB((255),(255),(255));
}CColorButton::~CColorButton()
{
}void CColorButton::SetBackColor(COLORREF cr)
{
BackColor=cr;
CDC *dc=GetDC();
CRect rect;
this->GetClientRect(&rect);
dc->FillRect(&rect,CBrush::FromHandle(CreateSolidBrush(m_BackColor)));
}BOOL CColorButton::OnEraseBkgnd(CDC* pDC)
{
CDC *dc=GetDC();
CRect rect;
this->GetClientRect(&rect);
dc->FillRect(&rect,CBrush::FromHandle(CreateSolidBrush(m_BackColor)));
return true;
} BEGIN_MESSAGE_MAP(CColorButton, CButton)
//{{AFX_MSG_MAP(My)
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
然后把按钮添加为CColorButton类的变量,就可以了。
// This OnCtlColor handler will change the color of a static control
// with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog
// class has an initialized and created CBrush member named m_brush.
// The control will be painted with red text and a background
// color of m_brush.HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// Call the base class implementation first! Otherwise, it may
// undo what we're trying to accomplish here.
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // Are we painting the IDC_MYSTATIC control? We can use
// CWnd::GetDlgCtrlID() to perform the most efficient test.
if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
{
// Set the text color to red
pDC->SetTextColor(RGB(255, 0, 0)); // Set the background mode for text to transparent
// so background will show thru.
pDC->SetBkMode(TRANSPARENT); // Return handle to our CBrush object
hbr = m_brush; } return hbr;
}
你的m_brush刚刚定义,什么都没有,另外,也怀疑局部cbrush对象是否恰当
在codeproject上有
用ccolorbutton定义按钮的时候出现:这是怎么回事,
头文件我都包含了呀!!!
只是改变颜色的话可以考虑用CBitmapButtonCBitmapButton m_BitmapBtn;
m_BitmapBtn.LoadBitmaps(IDB_BITMAPUP,IDB_BITMAPDOWN,IDB_BITMAPFOCUS,IDB_BITMAPDISABLE);
m_BitmapBtn.SubclassDlgItem(IDC_BUTTON_LENGTH,this);
IDC_BUTTON_LENGTH为按钮ID
m_BitmapBtn.LoadBitmaps(IDB_BITMAPUP,NULL);
m_BitmapBtn.SubclassDlgItem(IDC_CESHI,this);
m_BitmapBtn.SizeToContent();
但是按钮看不见啊
我将它属性设为owner draw
绘制背景颜色使用我上面的代码,文字的话,应该在DarwItem里面绘制,不过我那段代码里面没有写出DrawItem的代码来,因为我写那个类的时候不需要输出文字,只需要表现颜色。如果楼主需要的话,可以留一个邮箱,我把完整的代码发给你。