0

我为标题的模糊性道歉,但我不完全确定我想如何解决手头的问题。基本上,我有 2 组按钮。当用户从其中一个组中选择一个按钮时,我想设置一个指示该组已被选中。因此,要么是围绕它们的矩形(蓝色),要么是为组后面的背景(或背景图像)着色。最终用户表示,围绕它们的矩形会很好,因此会优先考虑。我曾尝试覆盖组框上的 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);
4

2 回答 2

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);
}

}

于 2013-06-26T15:55:22.477 回答
0

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

编辑您可以在组框上绘制任何您想要的东西!

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

b) 为 ID = 的组框覆盖 WindowProc IDC_GROUPBOX

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);
}
于 2013-06-26T15:46:03.057 回答