分类: C/C++
2011-06-01 12:53:13
m-colorChangeColor(COLORREF color)color,WM-DRAWITEM上面代码中数据成员m-color和来保存按钮的颜色.ChangeColor(COLORREF color)函数负责改变按钮颜色值为color,然后通过使控制的客户区无效而激发WM-DRAWITEM消息.现在这个按钮控制类就算搭好了.下面我们把它加入到对话框中来试验一下(中) 用VC++实现自绘按钮控制1.首先通过AppWizard创建一个单文档的应用.
2.紧接着启动AppStadio创建一个对话框.添加一个按钮控制,
并将其ID设置为 IDC-COLORBUTTON.最后一定要记住将push Button Properties对话框中的Owner Draw检查框置上检查标志.
3.在AppStadio内运行ClassWiz?zand来产生CTestDialog类.然后在CTestdialog类中加入数据成员,在CTestDialog类说明加入如下的private型数据成员:
private:
ccolorButton m-ColorButton;
4.现在剩下的问题是到底要怎样才能使m-ColorButton的DrawItem函数能响应系统发往ID值为IDC-COLORBUTTON的按钮控制的WM-DRAWITEM消息.这时就要用到CWnd类的成员函数BOOL CWnd::Subcla
ssDlgItem(UINT nID,CWnd *pParent).通过调用这个函数,我们可以动态地接管从对话框模板产生的控制,并把它隶属于CWnd对象.即用当前的CWnd对象接管发向隶属于pParent的ID号为nID的控制的一切消息.对于按钮控制而言,它把当前的按钮控制的位置和大小也清成和nID对应的按钮控制一样.于是我们对CTestDialog的源文件进行如下的编辑:
BOOL CTestDialog::OnInitDialog()
{ CDialog::OnInitDialog();
//TODO:Add extra initialization here
m-ColorButton.SubclassDlgtem(IDC-COLORBUTTON,this)
;//接管消息
m-ColorButton.ChangeColor(RGB(255,0,0);//设置为红色(可设为任何颜色)
return(TRUE);
}
接着通过ClassWizzard在CTestDialog中加入一个响应鼠标点击IDC-COLORBUTTON按钮的消息的函数:
void CTestDialog::OnColerbutton()
{ //TODO:Add your control notification handler code
here
int r=int(((float)rand()/RAND-MAX)*255
int g=int(((float)rand()/RAND-MAX)*255
int b=int(((float)rand()/RAND-MAX)*255
m-ColorButton.ChangeColor(RGB(r,g,b));
}5.最后,利用ClassWizzard为View加入一个响应WM-LBUTTONDOWN的函数,以便激活对话框.请按如下代码对其进行编辑.
void CTestView::OnLButton?Down(UINT nflags,POINT point)
{ CTestDialog dlg;
dlg.Domodal();
}
6.编译并测试该程序.当鼠标在落视窗中时,按下鼠标左键应能弹出一个对话框.在对话框中的红色矩形区域内按下鼠标左键就会使其边框变成高亮状态,若在这块区域内释放左键则这城区域就会改变颜色且颜色是随机的.
只需更改DrawItem函数中的重绘代码,就可以得到自己的需要图形按钮.
This article was contributed by .
Download and
One of the problems with the Windows architecture is in the inability to draw buttons in a color other than gray. While the class offered here isn’t cutting-edge technology, it is something that a lot of people ask for and use (especially in multi-media applications). The only way to draw the buttons in color is to use the owner-draw capabilities (or create a bitmap button which is a horrible solution). CColorButton does all this behind the scene and allows the developer to:
Consider this simple dialog, displayed in the first example with light blue buttons and blue text and in the second example, with multiple colored buttons and different text colors: Note: CTLCOLOR changes the background but not the buttons):
Operations
There is only one function, Attach(), which initializes an owner-draw button – usually in a dialog’s OnInitDialog() function.
BOOL Attach(const UINT nID, CWnd* pParent,
const COLORREF BGColor = RGB(192, 192, 192),
const COLORREF FGColor = RGB(1, 1, 1),
const COLORREF DisabledColor = RGB(128, 128, 128),
const UINT nBevel = 2);
Where:
Only the first two arguments are required; background, foreground, and disabled colors default to a gray button, black text, and dark gray disabled text.
How to use CColorButton
VERIFY(m_btn1.SetColors(IDOK, this, CLOUDBLUE, DKBLUE, WHITE));
VERIFY(m_btn2.SetColors(IDCANCEL, this, DKBLUE, WHITE));
Note: the colors used in this example - BLACK, WHITE, BLUE, DKGRAY, etc. - are COLORREF constants that you define in your own program via the RGB() macro:
const COLORREF CLOUDBLUE = RGB(128, 184, 223);
const COLORREF WHITE = RGB(255, 255, 255);
const COLORREF BLACK = RGB(1, 1, 1);
const COLORREF DKGRAY = RGB(128, 128, 128);
const COLORREF etc...
That's it. It's pretty easy to use and very useful for multi-media applications. This is a very simple class to work with and anyone with even the slightest GDI background can add or modify functions.
Limitations:
Last updated: 9 May 1998
注意:在已运行过的project中再添加代码修改颜色时,有时候要把原来的project.obj删除重新编译再运行。(自己遇到的问题)