I have a simple MFC application which draws an ellipse when user left clicks on it. I want to draw a colored ellipse instead of one with a white background. Here are my OnEraseBkgnd() and OnLButtonDown() handlers
HBRUSH NewBrush;
BOOL CMainWindow::OnEraseBkgnd(CDC *pDC){
NewBrush = CreateSolidBrush(RGB(0, 0, 250));
auto res = SelectObject(HDC(*pDC),NewBrush);
if(res==NULL||ERROR(res))
{
MessageBox("SelectObject error");
}
return CFrameWnd::OnEraseBkgnd(pDC); //I have tried return 0, and return 1 as well.
}
void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point){
CClientDC dc(this);
dc.Ellipse(0,0,100,50);
}
I expect a blue ellipse to be drawn, but only a white ellipse gets drawn.
If I do select object in OnLButtonDown() the ellipse is drawn in blue.
void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point){
CClientDC dc(this);
NewBrush = CreateSolidBrush(RGB(0, 0, 250));
SelectObject(HDC(dc),NewBrush);
dc.Ellipse(0,0,100,50);
}
What I don't understand is how that the effect of SelectObject() should be the same, no matter where I do it, as long as I am drawing the ellipse after doing SelectObject(). Since the brush gets attached to the device context, which is a property of my application window. Hence once set should retain its value while the application is running.