11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6. Dialog Box<br />

Sample<br />

Sampel 6.5\DB demonstrates background customization. It is a standard dialog-based application<br />

generated from Application Wizard. In the sample, instead of using a uniform color, the dialog box paints<br />

its background with a bitmap image (Figure 6-3).<br />

Figure 6-3. Customize the background of a dialog box<br />

Because WM_ERASEBKGND is not listed as a dialog box message, first we need to customize the filter<br />

settings for this application. We can do this by invoking Class Wizard, clicking “Class Info” tab then<br />

changing the default setting in combo box “Message Filter” from “Dialog” to “Window”. By going back to<br />

“Message maps” page now, we can find WM_ERASEBKGND in “Message” window, and add a message handler<br />

for it. The function name should be CDBDlg::OnEraseBkgnd(…).<br />

In the sample, a bitmap resource IDB_BITMAP_DLGBGD is added to the application, which will be used to<br />

draw the background of the dialog box. In function CDBDlg::OnEraseBkgnd(…), this bitmap is painted<br />

repeatedly until all dialog box area is covered by it:<br />

BOOL CDBDlg::OnEraseBkgnd(CDC *pDC)<br />

{<br />

CBitmap bmp;<br />

CBitmap *ptrBmpOld;<br />

CDC dcMemory;<br />

BITMAP bm;<br />

CRect rect;<br />

int i, j;<br />

int nHor, nVer;<br />

bmp.LoadBitmap(IDB_BITMAP_DLGBGD);<br />

bmp.GetBitmap(&bm);<br />

GetClientRect(rect);<br />

nHor=rect.Width()/bm.bmWidth+1;<br />

nVer=rect.Height()/bm.bmHeight+1;<br />

dcMemory.CreateCompatibleDC(pDC);<br />

ptrBmpOld=dcMemory.SelectObject(&bmp);<br />

for(i=0; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!