C++ 我想将背景色设置为从CMFCColorButton中拾取的颜色
我从我的C++ 我想将背景色设置为从CMFCColorButton中拾取的颜色,c++,user-interface,visual-c++,mfc,C++,User Interface,Visual C++,Mfc,我从我的CMFCColorButton中选择了颜色,现在我想将其设置为背景(如果它还不是当前颜色) 我似乎不知道怎么做,所以我非常感谢你的帮助和解释 void CMainFrame::OnColor() { // m_TextColors is the ID of the color button I created in the resource editor. CMFCRibbonColorButton* pColorBtn = DYNAMIC_DOWNCAST(CMFCRi
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);
}
}
这就是结果:
它跟踪文档的颜色,但不改变它,它改变了整个窗口的颜色
更新:我尝试了invalidate
功能,结果如下:
它似乎在我的MDI客户端区域顶部添加颜色,而不是像我预期的那样在背景中添加颜色,您尝试过:
void CMainFrame::OnButColor()
{
HBRUSH hBrush=CreateSolidBrush(RGB(0,0,255));
SetClassLong(m_hWndMDIClient, BGCL_HBRBACKGROUND,(LONG)hBrush);
::InvalidateRect(m_hWndMDIClient,0,TRUE);
}
这有用吗?