在非Dialog窗口中动态创建CheckBox, RadioButton 时, 总有默认的灰色背景, 想了许多方法都未能去除,到最后我只好自己处理WM_PAINT消息了, 郁闷!
template<class BASE_CLASS>
class CTransparentButton : public BASE_CLASS
{
public:
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
CPaintDC dc(this);
CString iText;
CRect boxRc, textRc;
int wh = 14;
boxRc.SetRectEmpty();
GetClientRect(&textRc);
textRc.bottom = 20;
boxRc.top = ((int)textRc.Height()/2)-7;
textRc.left = boxRc.right = boxRc.bottom = boxRc.top+wh;
textRc.left += 2;
switch(GetButtonStyle())
{
case BS_AUTOCHECKBOX:
{
if(GetCheck())
dc.DrawFrameControl(&boxRc,DFC_BUTTON,DFCS_CHECKED);
else
dc.DrawFrameControl(&boxRc,DFC_BUTTON,DFCS_BUTTONCHECK);
}
break;
case BS_AUTORADIOBUTTON:
{
if(GetCheck())
{
dc.DrawFrameControl(&boxRc,DFC_BUTTON,DFCS_BUTTONRADIO);
boxRc.DeflateRect(6,5);
CBrush bkBrush(RGB(0,0,0));
dc.SelectObject(&bkBrush);
dc.RoundRect(&boxRc,CPoint(boxRc.Width()/2,boxRc.Height()/2));
}
else
dc.DrawFrameControl(&boxRc,DFC_BUTTON,DFCS_BUTTONRADIO);
}
break;
}
GetWindowText(iText);
dc.SelectStockObject(DEFAULT_GUI_FONT);
dc.DrawText(iText,&textRc,DT_VCENTER|DT_SINGLELINE);
}
break;
}
return BASE_CLASS::WindowProc(message, wParam, lParam);
}
};
使用方法:
protected:
CTransparentButton<CButton> m_checkbox;
CTransparentButton<CButton> m_radiobtn1;
CTransparentButton<CButton> m_radiobtn2;
//-----------------------------------------------------------------
CCustomWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
//创建控件
m_checkbox.Create(_T("CheckBox"),WS_CHILD|WS_VISIBLE|
BS_AUTOCHECKBOX,
CRect(0,20,80,40), this, IDC_CHECKBOX1);
m_radiobtn1.Create(_T("RadioButton1"),WS_CHILD|WS_VISIBLE|
BS_AUTORADIOBUTTON ,
CRect(0,50,80,70), this, IDC_RADIOBTN1);
m_radiobtn2.Create(_T("RadioButton2"),WS_CHILD|WS_VISIBLE|
BS_AUTORADIOBUTTON ,
CRect(0,80,80,100), this, IDC_RADIOBTN2);
//....................
}