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 1. Tool Bar and Dialog Bar<br />

We can also use Class Wizard to add new class. In order to do this, after invoking the Class Wizard,<br />

we can click button labeled “Add Class…” then select “New…” from the popup menu. This will invoke a<br />

dialog box that lets us add a new class to the project. We can type in the new class name, select the header<br />

and implementation file names, and designate base class name. Unfortunately, CDialogBar is not in the list<br />

of base classes. A workaround is that we can select CDialog as the base class, after the class is generated,<br />

delete the unnecessary functions, and change all CDialog keywords to CDialogBar in both header and<br />

implementation files.<br />

Resizing Edit Control<br />

The edit control should be resized whenever the size of its parent window changes. In order to do this,<br />

we can trap WM_SIZE message, which is sent to a window when its size is about to change. We need to<br />

declare an afx_msg type member function as the message handler, and implement the message mapping<br />

using ON_WM_SIZE macro. The message handler of WM_SIZE should have the following format:<br />

afx_msg void OnSize(UINT nType, int cx, int cy);<br />

Here nType indicates how the window’s size will be changed (is it maximized, minimized…), cx and<br />

cy indicate the new window size.<br />

It is not very difficult to add message mapping macro, we can either add it manually, or ask Class<br />

Wizard to do it for us:<br />

BEGIN_MESSAGE_MAP(MCDialogBar, CDialogBar)<br />

//{{AFX_MSG_MAP(MCDialogBar)<br />

ON_WM_SIZE()<br />

//}}AFX_MSG_MAP<br />

END_MESSAGE_MAP()<br />

Please note that we do not need to specify function name when using macro ON_WM_SIZE. Instead, we<br />

must use OnSize to name the message handler of WM_SIZE.<br />

To change a window’s size, we can call function CWnd::MoveWindow(…):<br />

void CWnd::MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint=TRUE);<br />

We need to provide new position and size in order to call this function. Because the function is a<br />

member of CWnd, we need to first obtain a pointer to the edit window then use it to call this function.<br />

For controls contained in a dialog box, their window pointers can be obtained by calling function<br />

CWnd::GetDlgItem(…). This function requires a valid control ID:<br />

CWnd *CWnd::GetDlgItem(int nID);<br />

The function returns a CWnd type pointer. With this pointer, we can call any member functions of CWnd<br />

to retrieve its information or change the properties of the control.<br />

Because we want to set the edit control’s size according to the parent window’s size (dialog bar), we<br />

need to find a way of retrieving a window’s dimension. This can be implemented by calling another<br />

member function of CWnd:<br />

void CWnd::GetClientRect(LPRECT lpRect);<br />

It is very easy to use this function. We can just declare a CRect type variable, and pass its pointer to the<br />

above function when calling it. After this, the position and size of the window will be stored in the variable.<br />

The following shows how message WM_SIZE is handled in the sample:<br />

void MCDialogBar::OnSize(UINT nType, int cx, int cy)<br />

{<br />

CWnd *ptrWnd;<br />

CRect rectWnd;<br />

CDialogBar::OnSize(nType, cx, cy);<br />

26

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

Saved successfully!

Ooh no, something went wrong!