Changing the Default Background Color in an SDI Application | CodeGuru

Changing the Default Background Color in an SDI Application

Environment: SDI MFC projects SDI projects created using MFC come with a default white background color. This sample code implements any color the programmer desires after the creation of an SDI framework using the MFC wizard for project creation. Ctrl+W pops up the MFC classwizard property sheet. Select the Message Maps tab. From the drop-down […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 6, 2003
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: SDI MFC projects

SDI projects created using MFC come with a default white background color. This sample code implements any color the programmer desires after the creation of an SDI framework using the MFC wizard for project creation.

  1. Ctrl+W pops up the MFC classwizard property sheet.
  2. Select the Message Maps tab.
  3. From the drop-down list box under the Class Name static control, select the CxxxView option (xxx = Your project’s name; for example, CNnoyeView).
  4. Make sure CxxxView is also selected in the Object ID’s list box.
  5. Select the WM_ERASEBKGND option in the Messages list box.
  6. Click the Add Function button. The Class Wizard adds the “OnEraseBkgnd” member function.
  7. Click the Edit Code button. Add the following code before the return CView::OnEraseBkgnd(pDC) statement.
CBrush brNew(RGB(0,0,255));  //Creates a blue brush
CBrush* pOldBrush = (CBrush*)pDC->SelectObject(&brNew);
CRect rc;
pDC->GetClipBox(rc); // Gets the co-ordinates of the client
                     // area to repaint.
pDC->PatBlt(0,0,rc.Width(),rc.Height(),PATCOPY);
                     // Repaints client area with current brush.
pDC->SelectObject(pOldBrush);
return TRUE;    // Prevents the execution of return
                // CView::OnEraseBkgnd(pDC) statement

You can choose any desired color background by adjusting the values of Red, Green, and Blue in RGB(0,0,255); for example, RGB(0,0,0)=Black. You can also use bitmaps instead of brushes as the background color with simple adjustments. Guess that’s it for now, folks! Have to get back to work.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.