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

pWndCtrl=GetWindow(GW_CHILD);<br />

while(pWndCtrl)<br />

{<br />

pWndCtrl->GetWindowRect(rect);<br />

ScreenToClient(rect);<br />

rect.OffsetRect(ptOffset);<br />

pWndCtrl->MoveWindow(rect, FALSE);<br />

pWndCtrl=pWndCtrl->GetNextWindow();<br />

}<br />

GetWindowRect(rect);<br />

rect.right+=rectOld.Width()-rectNew.Width();<br />

rect.bottom+=rectOld.Height()-rectNew.Height();<br />

MoveWindow(rect);<br />

RepositionBars<br />

(<br />

AFX_IDW_CONTROLBAR_FIRST,<br />

AFX_IDW_CONTROLBAR_LAST,<br />

0<br />

);<br />

}<br />

return TRUE;<br />

First function CWnd::GetClientRect() is called and the dimension of client window is stored in<br />

rectOld. After the control bars are created, we call function CWnd::RepositionBars(…) and use flag<br />

CWnd::reposQuery to obtain the new layout dimension with the consideration of two control bars. The new<br />

layout dimension is stored in variable rectNew. The offset is calculated by deducting rectOld from<br />

rectNew. To access all the controls in the dialog box, we first call CWnd::GetWindow(…) using GW_CHILD<br />

flag to obtain the first child window of the dialog box, then call CWnd::GetNextWindow() repeatedly to find<br />

all the other child windows. Each child window is moved according to the offset dimension. Finally the<br />

dialog box is resized by calling function CWnd::RepositionBars(…) using the default parameters.<br />

Tool Tip and Flyby Implementation<br />

We need to add message handler for TTN_NEEDTEXT notification in order to implement tool tips. Also,<br />

we need to handle WM_SETMESSAGESTRING and WM_POPMESSAGESTRING in order to implement flybys on the<br />

status bar.<br />

In the sample, three new message handlers are added to class CDBDlg:<br />

……<br />

class CDBDlg : public CDialog<br />

{<br />

};<br />

//}}AFX_MSG<br />

afx_msg BOOL OnToolTipText(UINT, NMHDR *, LRESULT *);<br />

afx_msg LRESULT OnSetMessageString(WPARAM, LPARAM=0L);<br />

afx_msg LRESULT OnPopMessageString(WPARAM wParam, LPARAM lParam);<br />

DECLARE_DYNAMIC(CDBDlg)<br />

DECLARE_MESSAGE_MAP()<br />

Function CDBDlg::OnTooltipText(…) will be used to handle TTN_NEEDTEXT message, CDBDlg::<br />

OnSetMessageString(…) and CDBDlg::OnPopMessageString(…) will be used to handle<br />

WM_SETMESSAGESTRING and WM_POPMESSAGESTRING respectively.<br />

Message mapping macros are added to the implementation file as follows:<br />

……<br />

BEGIN_MESSAGE_MAP(CDBDlg, CDialog)<br />

ON_WM_QUERYDRAGICON()<br />

//}}AFX_MSG_MAP<br />

ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipText)<br />

ON_MESSAGE(WM_SETMESSAGESTRING, OnSetMessageString)<br />

ON_MESSAGE(WM_POPMESSAGESTRING, OnPopMessageString)<br />

END_MESSAGE_MAP()<br />

163

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

Saved successfully!

Ooh no, something went wrong!