我从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);
}
}
这是结果:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS82akRxYi5wbmcifQ==” alt =“在此处输入图像描述”>
它正在跟踪文档的颜色,但是没有改变它,而是在改变整个窗口的颜色。
您是否尝试过:
void CMainFrame::OnButColor()
{
HBRUSH hBrush=CreateSolidBrush(RGB(0,0,255));
SetClassLong(m_hWndMDIClient, BGCL_HBRBACKGROUND,(LONG)hBrush);
::InvalidateRect(m_hWndMDIClient,0,TRUE);
}