我想用timer消息从一数据库读取数据,根据读取出来的数据(0或1)来不停的变换button按钮的颜色
如 读取的是0改成红色  1为绿色....
如何才能实现我的功能呢?   重绘? 位图按钮???  麻烦各位高人指点一下小弟..... 谢谢了先...

解决方案 »

  1.   

    建议你用个按钮类吧,可以借鉴很多现成的类,比如CXPBUTTON等,会方便操作些
      

  2.   

    你先理解一下重绘
    然后通过OnTimer改变一下按钮自身的绘制参数就可以了
    你要详细解释这里有个例子
    http://blog.csdn.net/xianglitian/archive/2007/08/06/1728242.aspx
      

  3.   

    百度搜索 自绘BUTTON 有很多 
      

  4.   

    Button的颜色之类的更改,只能选择重新绘制了。。
    网上有很多代码的,楼主不妨找找看。。
      

  5.   

    从CButton类派生出一个自己的类CColorButton,重载DrowItem函数,添加成员m_curColor,在OnLButtonDown中设置m_curColor的值,在DrawItem中根据m_curColor的值绘制不同颜色的按钮。
      

  6.   

    怎么处理 BUTTON按钮 的事件 才能利用timer来改变按钮的颜色呢?
      

  7.   

    添加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
      

  8.   

    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);