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 />

dcMemory.SelectObject(ptrBmpOld);<br />

}<br />

return TRUE;<br />

First function CBitmap::LoadBitmap(…) is called to load the bitmap resource, then its dimension is<br />

retrieved by calling function CBitmap::GetBitmap(…). Next, function CWnd::GetClientRect(…) is called<br />

to obtain the size of the client area of the dialog box. Then we calculate the number of loops required to<br />

repeat drawing in both horizontal and vertical directions in order to cover all the client area. The results are<br />

stored in two local variables nHor and nVer. Then, a memory DC is created, and the bitmap image is<br />

selected into this DC. Next, function CDC::BitBlt(…) is called enough times to paint the bitmap to<br />

different locations of the dialog box. Finally a TRUE value is returned to prevent the background from<br />

being updated by the default implementation.<br />

Changing the Background of Common Controls<br />

If the dialog box includes some other common controls such as edit box, list box, check box or radio<br />

button, we will see the undesirable effect: the background of these controls is still painted with the default<br />

color, and this makes the appearance of the dialog box not harmonic.<br />

To change the background color of the common controls, we need to handle message WM_CTLCOLOR.<br />

The message handler can be added through using Class Wizard, and the default member function looks like<br />

the following:<br />

HBRUSH CDBDlg::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)<br />

{<br />

return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);<br />

}<br />

This function has three parameters. The first parameter is a pointer to the device context of the target<br />

window; the second is a pointer to the common control contained in the dialog box whose background is to<br />

be customized; the third parameter specifies the control type, which could be CTLCOLOR_BTN,<br />

CTLCOLOR_EDIT, CTLCOLOR_LISTBOX..., indicating that the control is a button, an edit box, a list box, and so<br />

on.<br />

We can return a brush handle that can be used to paint the background of the control. We can also let<br />

the control to have a transparent background, in this case we must return a NULL brush handle.<br />

In order to demonstrate how to customize the background of the common controls, in the sample, an<br />

edit box, a check box, two radio buttons, a static text, a scroll bar, a list box and a simple combo box are<br />

added to the application. Also, WM_CTLCOLOR message handler is added and the corresponding function<br />

CDBDlg::OnCtlColor(…) is implemented as follows:<br />

HBRUSH CDBDlg::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)<br />

{<br />

HBRUSH brush;<br />

switch(nCtlColor)<br />

{<br />

case CTLCOLOR_BTN:<br />

case CTLCOLOR_STATIC:<br />

{<br />

pDC->SetBkMode(TRANSPARENT);<br />

brush=(HBRUSH)::GetStockObject(NULL_BRUSH);<br />

break;<br />

}<br />

case CTLCOLOR_EDIT:<br />

{<br />

pDC->SetBkMode(TRANSPARENT);<br />

brush=(HBRUSH)::GetStockObject(LTGRAY_BRUSH);<br />

break;<br />

}<br />

case CTLCOLOR_LISTBOX:<br />

{<br />

pDC->SetBkMode(TRANSPARENT);<br />

152

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

Saved successfully!

Ooh no, something went wrong!