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

int ::GetSystemMetrics(int nIndex);<br />

Possible Value of nIndex<br />

Meaning<br />

SM_CXBORDER, SM_CYBORDER Width and height of the window border<br />

SM_CXCURSOR, SM_CYCURSOR Width and height of the cursor<br />

SM_CXDLGFRAME, SM_CYDLGFRAME Width and height of the non-resizable dialog box frame<br />

SM_CXFULLSCREEN,<br />

Width and height of the desktop screen<br />

SM_CYFULLSCREEN<br />

SM_CXICON, SM_CYICON Width and height of the icon (usually 32×32)<br />

SM_CXSMICON, SM_CYSMICON Width and height of the small icon (usually 16×16)<br />

SM_CYCAPTION<br />

Height of the caption area<br />

In the sample, the maximized size of the dialog is set to 1/4 of the desk top screen size. When the<br />

application is first maximized, it will be positioned at top-left corner (0, 0).<br />

The dialog box’s initial size is set in function CDialog::OnInitDialog():<br />

……<br />

BOOL CDBDlg::OnInitDialog()<br />

{<br />

RECT rect;<br />

rect.left=0;<br />

rect.top=0;<br />

rect.right=MIN_X_SIZE;<br />

rect.bottom=MIN_Y_SIZE;<br />

MapDialogRect(&rect);<br />

rect.right+=50;<br />

rect.bottom+=50;<br />

MoveWindow(&rect);<br />

CenterWindow();<br />

}<br />

return TRUE;<br />

The dialog box’s initial size is a little bigger than its minimum tracking size.<br />

The above sizes are not unique to dialog boxes. In fact, any window has the above sizes, and can be<br />

customized with the same method.<br />

6.5 Customizing Dialog Box Background<br />

Background Drawing<br />

Generally all dialog boxes have a gray background. Sometimes it is more desirable to change dialog<br />

box’s background to a custom color, or, we may want to paint the background using a repeated pattern. To<br />

customize a dialog box’s background, we need to handle message WM_ERASEBKGND, and draw the custom<br />

background after receiving this message. All classes that are derived from CWnd will inherit function<br />

CWnd::OnEraseBkgnd(…), which has the following format:<br />

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

{<br />

}<br />

Here, pointer pDC can be used to draw anything on the target window. For example, we can create<br />

solid brush and paint the background with a custom color, or we can create pattern brush, and paint the<br />

background with certain pattern. Of course, bitmap can also be used here: we can draw our own bitmap<br />

repeatedly until all of the dialog box area is covered by the bitmap patterns.<br />

150

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

Saved successfully!

Ooh no, something went wrong!