【问题标题】:I want to set the background color as the color picked from my CMFCColorButton我想将背景颜色设置为从我的 CMFCColorButton 中选择的颜色
【发布时间】:2020-03-20 18:00:10
【问题描述】:

我从CMFCColorButton 中挑选了颜色,现在我想将其设置为背景(如果它还不是当前颜色)。

我似乎无法弄清楚如何,所以非常感谢您的帮助和解释。

void CMainFrame::OnColor()
{
    // m_TextColors is the ID of the color button I created in the resource editor.
    CMFCRibbonColorButton* pColorBtn = DYNAMIC_DOWNCAST(CMFCRibbonColorButton, m_wndRibbonBar.FindByID(m_TextColors));
    COLORREF color = pColorBtn->GetColor();
    CWnd* pwndParent = this->GetParent();
    CRect rcClient;
    pwndParent->GetClientRect(&rcClient);
    if (color != GetSysColor(COLOR_BACKGROUND)) {
        CBrush brush;
        CClientDC dc(this);
        brush.CreateSolidBrush(color);
        dc.FillRect(rcClient, &brush);
    }
    else {
        MessageBox(_T("Same Color."), MB_OK);
    }
}

我做了一些改动:

void CMainFrame::OnColor()
{
    // m_TextColors is the ID of the color button I created in the resource editor.
    CMFCRibbonColorButton* pColorBtn = DYNAMIC_DOWNCAST(CMFCRibbonColorButton, m_wndRibbonBar.FindByID(m_TextColors));
    COLORREF color = pColorBtn->GetColor();
    CBrush brush;
    brush.CreateSolidBrush(color);
    CRect rc;
    GetClientRect(&rc);
    GetWindowRect(&rc);
    CClientDC dc(this);
    dc.SelectObject(&rc);
    if (color != GetSysColor(COLOR_WINDOW)) {
        dc.FillRect(rc, &brush);
    } else {
        MessageBox(_T("Same Color."), MB_OK);
    }
}

结果如下:

它正在跟踪文档的颜色但不改变它,它正在改变整个窗口的颜色。

更新:我尝试了invalidateRect 函数,结果如下:

它似乎是在我的 MDI 客户区顶部添加颜色,而不是像我想要的那样在背景中添加颜色,

【问题讨论】:

标签: c++ user-interface visual-c++ mfc


【解决方案1】:

你试过了吗:

void CMainFrame::OnButColor() 
{
     HBRUSH hBrush=CreateSolidBrush(RGB(0,0,255));
     SetClassLong(m_hWndMDIClient, BGCL_HBRBACKGROUND,(LONG)hBrush);
     ::InvalidateRect(m_hWndMDIClient,0,TRUE);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-13
    • 2020-01-29
    • 2011-07-01
    • 1970-01-01
    • 2017-08-31
    • 2014-02-10
    • 2017-04-05
    • 1970-01-01
    相关资源
    最近更新 更多