Click to See Complete Forum and Search --> : [RESOLVED] Modify the default Background of the Client View


prabhakar157
June 14th, 2006, 02:59 AM
Is it possible to modify the default Background color of the Client which is white to a custom color?

I tried to change the background color to black using FillRect method of CDC in the following way..........

A simple code to test...........


void CXYZ::OnDraw(CDC* pDC)
{
CXYZ* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

CRect r;
a) CBrush blackBrush(RGB(0,0,0));
b) //pDC->SetBkMode(TRANSPARENT);
c) GetClientRect(&r);
d) pDC->FillRect(&r,&blackBrush);
CBrush redBrush(RGB(210,0,0));
CRect rect(100,100,400,400);
pDC->BeginPath();
pDC->Rectangle(&rect);
pDC->EndPath();

rgn.CreateFromPath(pDC);
pDC->FillRgn(&rgn,&redBrush);

rgn.OffsetRgn(movePoint.x-100,movePoint.y-100);
pDC->FillRgn(&rgn,&redBrush);
rgn.DeleteObject();


// TODO: add draw code for native data here
}

movePoint rgn object are declared in CXYZ.h file

The value of movePoint is assigned the value of Mouse point which is handled in the MouseMove Event.


void CXYZ::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
movePoint = point;
InvalidateRgn(&rgn,0);
CView::OnMouseMove(nFlags, point);
}



I used InvalidateRgn() instead of Invalidate() coz i dont want to redraw the Background to avoid flickering.

I know that the problem is with the lines a,c and d.. as i'm redrawing the entire rectangle every time by calling OnDraw from the mousemove event.
As a result there is a flickering in the Rectangle region.

But when the lines a,c and d are commented (the background color of the client is set to its default color WHITE) there is not a pinch of flickering on the screen as the background is not erased.

So is it possible for me to change the color of the client view during runtime just like that of paint, so that i can fill the background of the view with my desired color and also be able to move different shapes on the same background at RUNTIME ?

philkr
June 14th, 2006, 05:14 AM
There is a handler named OnCtlColor() or similar, which you should use to change colors.

ovidiucucu
June 14th, 2006, 09:32 AM
Depends on which type of window you want to modify the background color of the client area.
I have observed you are using MFC, so here are few MFC examples:

For a CView-derived you can register a window class with desired background brush, and pass that class name in the overriden PreCreateWindow:
class CMyView : public CView
{
// ...
// Attributes
protected:
CBrush m_brush;
// ...
};

BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
m_brush.CreateSolidBrush(RGB(0,0,255)); // blue
cs.lpszClass = AfxRegisterWndClass(CS_VREDRAW|CS_HREDRAW|CS_DBLCLKS,
AfxGetApp()->LoadStandardCursor(IDC_ARROW),
m_brush);
return CView::PreCreateWindow(cs);
}

For a dialog or a form view you can handle WM_CTLCOLOR
class CMyView : public CFormView
{
// ...
// Attributes
protected:
CBrush m_brush;
// ...
};

CMyView::CMyView()
: CFormView(CMyView::IDD)
{
m_brush.CreateSolidBrush(RGB(0,0,255));
// ...
}
HBRUSH CMyView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

if(CTLCOLOR_DLG == nCtlColor)
{
hbr = m_brush;
}
return hbr;
}
For the MDI frame client area see THIS FAQ (http://www.codeguru.com/forum/showthread.php?t=319786),

ovidiucucu
June 14th, 2006, 10:05 AM
... and more examples:

in an edit view:
class CMyView : public CEditView
{
// ...
// Attributes
public:
CBrush m_brush;
// ...
};

CMyView::CMyView()
{
m_brush.CreateSolidBrush(RGB(0,0,255));
}
HBRUSH CMyView::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetTextColor(RGB(255,255,255));
pDC->SetBkColor(RGB(0,0,255));
return m_brush;
}
yet another one, handling WM_ERASEBKGND
class CMyView : public CView
{
// ...
// Attributes
protected:
CBrush m_brush;
// ...
// Implementation
protected:
void SetBkColor(COLORREF crBack);
// ...
};

BOOL CMyView::OnEraseBkgnd(CDC* pDC)
{
CBrush* pbrushOld = pDC->SelectObject(&m_brush);
CRect rect;
pDC->GetClipBox(&rect);
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
pDC->SelectObject(pbrushOld);

return TRUE;
}
void CMyView::SetBkColor(COLORREF crBack)
{
if(NULL != m_brush.GetSafeHandle())
{
m_brush.DeleteObject();
}
m_brush.CreateSolidBrush(crBack);
}