11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6. Dialog Box<br />

}<br />

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

break;<br />

}<br />

case CTLCOLOR_MSGBOX:<br />

{<br />

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

break;<br />

}<br />

case CTLCOLOR_SCROLLBAR:<br />

{<br />

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

break;<br />

}<br />

default: brush=CDialog::OnCtlColor(pDC, pWnd, nCtlColor);<br />

}<br />

return brush;<br />

Stock Objects<br />

In the above implementation, the background of different controls is painted using different brushes:<br />

the button and static control have a transparent background; the background of the list box and scroll bar is<br />

painted with a gray brush; the background of the message box is painted with a light gray brush. Here, all<br />

the brushes are obtained through calling function ::GetStockObject(…) rather than being created by<br />

ourselves.<br />

In Windows, there are a lot of predefined stock objects that can be used. These objects include<br />

brushes, pens, fonts and palette. The predefined brushes include white brush, black brush, gray brush, light<br />

gray brush, dark gray brush, and null (hollow) brush. Function ::GetStockObject(…) will return a GDI<br />

object handle (a brush handle in our sample). If we attach the returned handle to a GDI object, we must<br />

detach it instead of deleting the object when it is no longer useful.<br />

Text Foreground and Background<br />

Now the edit box, static control and list box all have transparent background. But these controls also<br />

contain text. Since a character also has both foreground and background areas (Figure 6-4), if we don’t set<br />

the text’s drawing mode, its background area may be drawn with an undesirable color (Figure 6-5).<br />

A<br />

Foreground<br />

color<br />

Background color<br />

Figure 6-4. Foreground and background color of a character<br />

153

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

Saved successfully!

Ooh no, something went wrong!