【发布时间】: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