添加dlg类的WM_DRAWITEM消息处理函数 void CBtncolorDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your message handler code here and/or call default if(nIDCtl==IDC_BUTTON1) //checking for the button { CDC dc; RECT rect; dc.Attach(lpDrawItemStruct ->hDC); // Get the Button DC to CDC
rect = lpDrawItemStruct->rcItem; //Store the Button rect to our local rect.
dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0)); dc.FillSolidRect(&rect,RGB(100,100,255));//Here you can define the required color to appear on the Button. UINT state=lpDrawItemStruct->itemState; //This defines the state of the Push button either pressed or not. if((state & ODS_SELECTED)) { dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT); } else { dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT); } dc.SetBkColor(RGB(100,100,255)); //Setting the Text Background color dc.SetTextColor(RGB(255,0,0)); //Setting the Text Color TCHAR buffer[MAX_PATH]; //To store the Caption of the button. ZeroMemory(buffer,MAX_PATH ); //Intializing the buffer to zero ::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH); //Get the Caption of Button Window
dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);//Redraw the Caption of Button Window
dc.Detach(); // Detach the Button DC } CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);} 把要定义的按钮设置为Owner Draw
1>.class CMyBt : public CButton 2>.void CMyBt::OnPaint() { static int count=0; CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here CString strText; GetWindowText(strText); // CRect rect; GetClientRect(&rect); dc.SetBkMode(TRANSPARENT); dc.Draw3dRect(&rect,RGB(255,255,255),RGB(128,128,128)); // COLORREF crOldColor; // switch(count) { case 0: // Draw the button text using the text color red. crOldColor = dc.SetTextColor(RGB(255,0,0)); break; case 1: crOldColor = dc.SetTextColor(RGB(0,255,0)); break; case 2: crOldColor = dc.SetTextColor(RGB(0,0,255)); break; } dc.DrawText(strText, strText.GetLength(),rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER); dc.SetTextColor(crOldColor); // count++; count %= 3; // Do not call CButton::OnPaint() for painting messages } 3>.In class CDlgTest : public CDialog CMyBt m_btTest; 4>.void CDlgTest::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default m_btTest.Invalidate(); // CDialog::OnTimer(nIDEvent); } 5>.In BOOL CDlgTest::OnInitDialog() SetTimer(1,1000,NULL);
然后通过OnTimer改变一下按钮自身的绘制参数就可以了
你要详细解释这里有个例子
http://blog.csdn.net/xianglitian/archive/2007/08/06/1728242.aspx
网上有很多代码的,楼主不妨找找看。。
void CBtncolorDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
if(nIDCtl==IDC_BUTTON1) //checking for the button
{
CDC dc;
RECT rect;
dc.Attach(lpDrawItemStruct ->hDC); // Get the Button DC to CDC
rect = lpDrawItemStruct->rcItem; //Store the Button rect to our local rect.
dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0)); dc.FillSolidRect(&rect,RGB(100,100,255));//Here you can define the required color to appear on the Button. UINT state=lpDrawItemStruct->itemState; //This defines the state of the Push button either pressed or not. if((state & ODS_SELECTED))
{
dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT); }
else
{
dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
} dc.SetBkColor(RGB(100,100,255)); //Setting the Text Background color
dc.SetTextColor(RGB(255,0,0)); //Setting the Text Color
TCHAR buffer[MAX_PATH]; //To store the Caption of the button.
ZeroMemory(buffer,MAX_PATH ); //Intializing the buffer to zero
::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH); //Get the Caption of Button Window
dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);//Redraw the Caption of Button Window
dc.Detach(); // Detach the Button DC
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);}
把要定义的按钮设置为Owner Draw
2>.void CMyBt::OnPaint()
{
static int count=0;
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CString strText;
GetWindowText(strText);
//
CRect rect;
GetClientRect(&rect);
dc.SetBkMode(TRANSPARENT);
dc.Draw3dRect(&rect,RGB(255,255,255),RGB(128,128,128));
//
COLORREF crOldColor;
//
switch(count)
{
case 0:
// Draw the button text using the text color red.
crOldColor = dc.SetTextColor(RGB(255,0,0));
break;
case 1:
crOldColor = dc.SetTextColor(RGB(0,255,0));
break;
case 2:
crOldColor = dc.SetTextColor(RGB(0,0,255));
break;
}
dc.DrawText(strText, strText.GetLength(),rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
dc.SetTextColor(crOldColor);
//
count++;
count %= 3;
// Do not call CButton::OnPaint() for painting messages
}
3>.In class CDlgTest : public CDialog
CMyBt m_btTest;
4>.void CDlgTest::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
m_btTest.Invalidate();
// CDialog::OnTimer(nIDEvent);
}
5>.In BOOL CDlgTest::OnInitDialog()
SetTimer(1,1000,NULL);