MFC - 在控件上设置背景图像

时间:2013-06-26 13:36:11

标签: mfc controls paint

我为标题的模糊性道歉,但我不完全确定我想如何解决手头的问题。基本上,我有两组按钮。当用户从其中一个组中选择一个按钮时,我想设置一个指示符,表明该组已被选中。因此,要么围绕它们的矩形(蓝色),要么在组后面着色背景(或背景图像)。最终用户表示围绕它们的矩形会很好,所以这是优先的。我已经尝试在组框上覆盖onCtlColor,但我不知道如何将着色限制为仅边框。我知道它绝对需要成为z-order中最远的任何建议吗?

示例代码(我知道它会影响整个框)

pDC->SetBkColor(GetSysColor(RGB(100,149,237)));
CRect rect;
testGb.GetClientRect(rect);
CBrush brushBlue(RGB(0, 0, 255));
CBrush* pOldBrush = pDC->SelectObject(&brushBlue);

pDC->Rectangle(rect);
pDC->SelectObject(pOldBrush);

2 个答案:

答案 0 :(得分:0)

我只能找到一种方法来改变组合框文字的背景颜色:

编辑您可以在“组”框中绘制所需的任何内容!

a)声明一个CBrush成员变量m_br并用它创建一个彩色画笔

b)覆盖ID为IDC_GROUPBOX的组框的WindowProc: <击>

<击>
LRESULT CTestMFCDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{   if (message == WM_CTLCOLORSTATIC)
        if (::GetDlgCtrlID((HWND) lParam) == IDC_GROUPBOX)
            return (LRESULT) m_br.GetSafeHandle();

    return CDialogEx::WindowProc(message, wParam, lParam);
}

<击>

LRESULT CTestMFCDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{   if (message == WM_CTLCOLORSTATIC)
        if (::GetDlgCtrlID((HWND) lParam) == IDC_GROUPBOX)
        {   HWND hWnd = (HWND) lParam;
            HDC hDC = (HDC) wParam;
            RECT rc;
            ::GetClientRect(hWnd, &rc);
            HBRUSH hOldBrush = (HBRUSH) ::SelectObject(hDC, m_br);
            ::Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
            ::SelectObject(hDC, hOldBrush);
    //      return (LRESULT) m_br.GetSafeHandle();
        }

    return CDialogEx::WindowProc(message, wParam, lParam);
}

答案 1 :(得分:0)

更好的代码(至少对我而言):

    void MyClass::OnPaint()
{
CPaintDC dc(this); // device context for painting
COLORREF highlightFillColor;
CPen nPen, *pOldPen = NULL;
CBrush  nBrush, *pOldBrush = NULL;
CRect rect;
GetWindowRect(rect);

ScreenToClient(rect);
BmsMemDC memDc(&dc, &rect);
memDc.SetBkMode(TRANSPARENT);

//dc.Re
highlightFillColor =  RGB(0x99,0xB4,0xFF);
nPen.CreatePen( PS_SOLID, 4, highlightFillColor);
nBrush.CreateSolidBrush( GetSysColor(COLOR_3DFACE ));

pOldPen = memDc.SelectObject(&nPen);
pOldBrush = memDc.SelectObject(&nBrush);

if(leftGroupSelected)
{        
    rect.SetRect(rect.left + 4, rect.top+30,  rect.left + 126, rect.bottom - 5);
    memDc.FillRect(&rect,&nBrush);
    memDc.RoundRect(rect.left, rect.top, rect.right, rect.bottom, 8, 8);
}
if (rightGroupSelected)
{
    rect.SetRect(rect.left + 134, rect.top+30,  rect.left + 256, rect.bottom - 5);
    memDc.FillRect(&rect,&nBrush);
    memDc.RoundRect(rect.left, rect.top, rect.right, rect.bottom, 8, 8);
}

}