MFC创建颜色拾取按钮
湖南三辰卡通集团(软件部)
作者:李英江
摘要:
近来要在程序中添加一个颜色拾取的按钮,经过摸索找到一种比较简单的办法,并且很方便重用。
实现如下:
1、从CButton类中派生一个自己的类class CButtonColor : public CButton
2、创建一个私有的成员函变量用于保存按钮背景颜色
private:
COLORREF m_bgColor;
3、添加两个成员函数,用于设置按钮背景颜色和获得按钮背景颜色
COLORREF CButtonColor::GetBGColor()
{
return m_bgColor;
}
BOOL CButtonColor::SetBGColor(COLORREF bgColor)
{
m_bgColor = bgColor;
InvalidateRect(NULL);
return TRUE;
}
4、添加一个虚拟成员函数:virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); 每次按钮重画时都会调用这个函数。
void CButtonColor::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
// 画按钮背景色
CBrush cb;
(GetBGColor());
CRect rc= lpDrawItemStruct->rcItem;
pDC->FillRect(&rc, &cb);
// 画按钮边框
CBrush cbFrame;
(RGB(255,255,255));
pDC->FrameRect(&rc, &cbFrame);
}
5、在对话框中使用时,添加头文件
#include ""
6、添加一个按钮控件
2015精品范文MFC创建颜色拾取按钮 来自淘豆网www.taodocs.com转载请标明出处.