在dialog上动态创建一些按钮,在OnPaint()函数里给这些按钮涂色,为什么彩色一闪而过,留下的还是灰色的按钮?

解决方案 »

  1.   

    你不应该用OnPaint,应该重载一个按钮,然后自绘她,在DrawItem(...)里,并记住这个按钮的type要设为自绘型
    看看这个例子:
    http://codeguru.earthweb.com/buttonctrl/ClrButton.html
      

  2.   

    我用CDC涂蓝色。
    不能用OnPaint(),应该用什么?
      

  3.   

    要想永远改变颜色,不能在OnPaint中,建议你使用CDC类的
    SetBkColor方法
      

  4.   

    我是在OnPaint()里面重载自绘的,会有问题吗?
    我感觉是先画了颜色,然后又刷新了button.
      

  5.   

    遵循你的想法,看看OnCtlColor这个函数,看看这个例子:
    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;
    }
      

  6.   

    给按钮改颜色最好不是在Dialog里改。派生按钮类,对按钮的OnPaint,OnEraseBkgnd进行修改,或者,自己画。我有一个自己搞得的Button类,需要的话,发消息给我。
      

  7.   

    我是在OnPaint()里调用这样一个函数,输入的Button(IDC)有些是静态的,有些是动态创建的。结果有时会涂几个,有时一个都不涂。如果用别的窗口覆盖一下再移开,会发现其实是都涂过的,只是一闪而过。请问有没有比较方便有效的办法解决,因为我是在别人的程序上做修改,不想改动太多。谢谢!//rc : a button's IDC
    //errsort[8]: will be shown on the button
    void CSripTest::PaintErrIcon(int rc,char errsort[8])
    { CButton *ItemBtn; CDC *charDC;         
             charDC = CWnd::GetDC();
    ItemBtn = (CButton*) GetDlgItem( rc );  
    RECT Rect;
    ItemBtn->GetWindowRect(&Rect);
    RECT RectClient;
             //get the coordination of the father dialog.
    GetWindowRect(&RectClient);
    Rect.bottom -= RectClient.top;   
    Rect.top    -= RectClient.top;
    Rect.left   -= RectClient.left;
    Rect.right  -= RectClient.left; Rect.bottom -= 8;     //adjust to the button
    Rect.right -= 8; CString rStr;
    int rStrLen;
    ItemBtn->GetWindowText( rStr ) ;
    rStrLen = rStr.GetLength();     //brush used for ellipse fill color
    CBrush brush;            //privious brush that was seleted into dc
             CBrush* pBrushOld;       
    if(!strcmp(errsort,"bad"))
                          //use RED to fill
    brush.CreateSolidBrush(RGB(255,0,0));
    else
                        //use blue to fill
    brush.CreateSolidBrush(RGB(0,0,255));  
     //select brush into charDC,used to floodfill.
              pBrushOld = charDC->SelectObject(&brush); 
     charDC->FillRect( &Rect, &brush );
              
    //deselect brush from charDC so we can delete the brush
         charDC->SelectObject(pBrushOld);  
               brush.DeleteObject();   charDC->SetTextColor(RGB(255,255,255));    
    charDC->SetBkMode(TRANSPARENT);
    charDC->TextOut( (Rect.left+Rect.right-rStrLen*8)/2,(Rect.top+Rect.bottom-15)/2,rStr);           ReleaseDC( charDC );

     
    }