By default the color is gray, I want to change it.I use OnEraseBkgnd
in my MainFarm.h
,this works, it changes color,but when somewhere further in the code mfc changes it to gray again.
BOOL CMainFrame::OnEraseBkgnd(CDC* pDC)
{
CBrush backBrush(RGB(0, 0, 0));
CBrush* pPrevBrush = pDC->SelectObject(&backBrush);
CRect rect;
pDC->GetClipBox(&rect);
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(),
PATCOPY);
pDC->SelectObject(backBrush);
return TRUE;
}
CodePudding user response:
A MDI application doesn't just have frame windows and child windows. It also has a client window. The client window handles most of managing child windows.
But it also draws the client area of the frame window. This is what's drawing the grey background after you draw your background when you handle OnEraseBkgnd
in the frame window.
Assuming your frame is derived from CMDIFrameWndEx
, you should have a OnEraseMDIClientBackground
virtual function that you can override to do the drawing you want.
If you're modifying older code, it's possible it uses an old enough version of MFC that's not present. If so, you need to create a window class and do the correct drawing in its onEraseBkgnd
, create an instance of that in your frame window class, and in the frame window's onCreate
, you subclass the MDI child window:
class MyBkgndErase : public CWnd {
public:
BOOL OnEraseBkgnd(CDC* pDC) {
// drawing code here
}
};
class MyFrameWnd : public CMDIFrameWnd {
MyBkgndErase eraser;
int OnCreate(LPCREATESTRUCT lpCreateStruct) {
// there's probably existing code you'll want to preserve here
eraser.SubclassWindow(m_hWndMDIClient);
return 0;
}
};
Or, if you can switch to a newer version of MFC, you can probably just change your frame window's parent class from CMDIFrameWnd
to CMDIFrameWndEx
, and use OnEraseMDIClientBackground
(which is undoubtedly easier).