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

wndA.GetWindowRect(rect);<br />

wndB.ScreenToClient(rect);<br />

Sample<br />

When the user resizes a window, a WM_SIZE message will be sent to that window. We can handle this<br />

message to resize and move the controls contained in the dialog template.<br />

Sample 6.6\DB demonstrates how to resize the common controls contained in the form view<br />

dynamically. It is a standard SDI application generated from the Application Wizard. When generating the<br />

application, CFormView is selected as the base class of the view in the last step. After the application is<br />

generated, the following controls are added to the dialog template: an edit box, a static group control, two<br />

buttons. The IDs of these controls are IDC_EDIT, IDC_STATIC_GRP, IDC_BUTTON_A and IDC_BUTTON_B<br />

respectively.<br />

If we compile and execute the application at this point, the application will behave awkwardly because<br />

if we resize the window, the sizes/positions of the controls will not change, this may make the window not<br />

well balanced (Figure 6-6).<br />

Figure 6-6. The form view is not well balanced<br />

We need to remember the original sizes and positions of the embedded controls and base their new<br />

sizes and positions on them. In the sample, four CRect type variables are declared in class CDBView for this<br />

purpose:<br />

……<br />

……<br />

class CDBView : public CFormView<br />

{<br />

protected:<br />

CRect m_rectA;<br />

CRect m_rectB;<br />

CRect m_rectEdit;<br />

CRect m_rectStaticGrp;<br />

BOOL m_bSizeAvailable;<br />

}<br />

Also, a Boolean type variable m_bSizeAvailable is added to indicate if the original positions and sizes<br />

of the controls have been recorded.<br />

There is no OnInitDialog() member function for class CFormView. The similar one is CView::<br />

OnInitialUpdate(). This function is called when the view is first created and is about to be displayed. We<br />

can record the positions and sizes of the controls in this function.<br />

Variable m_bSizeAvailable is initialized to FALSE in the constructor of class CDBView:<br />

CDBView::CDBView()<br />

: CFormView(CDBView::IDD)<br />

{<br />

//{{AFX_DATA_INIT(CDBView)<br />

155

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

Saved successfully!

Ooh no, something went wrong!