MFC设置控件背景透明

来源:互联网 发布:网络技术培训 编辑:程序博客网 时间:2024/06/06 17:56
添加消息响应WM_CTLCOLOR,
Static代码如下:
HBRUSH CTest1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
CFont m_font;   //声明变量
m_font.CreatePointFont(600,"华文行楷"); //设置字体大小和类型
if(pWnd->GetDlgCtrlID()==IDC_STATIC)//可以用CTLCOLOR_STATIC表示静态控件
{
   pDC->SelectObject(&m_font);       //设置字体 
   pDC->SetTextColor(RGB(0,0,255)); //设置字体颜色
   pDC->SetBkMode(TRANSPARENT);      //属性设置为透明
   return (HBRUSH)::GetStockObject(NULL_BRUSH); //不返回画刷
}
// TODO: Return a different brush if the default is not desired
return hbr;

}


Radio和Check代码如下
HBRUSH CTest1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);




    if (pWnd->GetDlgCtrlID() == IDC_RADIO_REALNAME  ||
        pWnd->GetDlgCtrlID() == IDC_RADIO_ANONYMOUS ||
        pWnd->GetDlgCtrlID() == IDC_CHECK_SELFSELECT)
    {
        pDC->SetBkMode(TRANSPARENT);


        CRect rc;
        pWnd->GetWindowRect(&rc);
        ScreenToClient(&rc);


        CDC* dc = GetDC();
        pDC->BitBlt(0,0,rc.Width(),rc.Height(),dc,rc.left,rc.top,SRCCOPY);    //把父窗口背景先画到按钮上
        ReleaseDC(dc);


        hbr = (HBRUSH) ::GetStockObject(NULL_BRUSH);
    }
}

0 0
原创粉丝点击