遵循你的想法,看看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; }
我是在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 );
看看这个例子:
http://codeguru.earthweb.com/buttonctrl/ClrButton.html
不能用OnPaint(),应该用什么?
SetBkColor方法
我感觉是先画了颜色,然后又刷新了button.
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;
}
//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 );
}