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

The ID of the target control (whose tool tip text is being retrieved) can be obtained from member hdr.<br />

From this ID we can obtain the resource string that is prepared for the tool tip. There are three ways to<br />

provide a tool tip string: 1) Prepare our own buffer that contains the tool tip text and assign the buffer’s<br />

address to member lpszText. 2) Copy the tool tip text directly to member szText. 3) Stores the tool tip text<br />

in a string resource, assign its ID to member lpszText. In the last case, we need to assign member hinst<br />

the instance handle of the application, which can be obtained from function AfxGetResourceHandle().<br />

Member uflsgs indicates if the control is a window or not.<br />

Recall when we create tool bars and dialog bars in the first chapter, tool tips were all implemented in a<br />

very simple way: we provide a string resource whose ID is exactly the same with the control ID, and<br />

everything else will be handled automatically. When handling message TOOLTIPTEXT for dialog box, we<br />

can also let the tool tip be implemented in a similar way. In order to do this, we can assign the resource ID<br />

of the control to member lpszText and the application instance handle to member hinst. If there exists a<br />

string resource whose ID is the same with the control ID, that string will be used to implement the tool tip.<br />

Otherwise, nothing will be displayed because the string can not be found.<br />

Sample<br />

Sample 6.7\DB demonstrates how to implement tool tips for the controls contained in a dialog box. It<br />

is based on sample 6.6\DB, with tool tips enabled for the following three controls: ID_EDIT, ID_BUTTON_A<br />

and ID_BUTTON_B (Although sample 6.6\DB is a form view based application, the tool tip implementation is<br />

the same with that of a dialog box).<br />

Three string resources are added to the application, whose IDs are IDC_EDIT, IDC_BUTTON_A and<br />

IDC_BUTTON_B. They will be used to implement tool tips for the corresponding edit box and buttons. In<br />

function CDBView::OnInitialUpdate(), the tool tips are enabled as follows<br />

……<br />

void CDBView::OnInitialUpdate()<br />

{<br />

}<br />

EnableToolTips(TRUE);<br />

Message handler of TTN_NEEDTEXT must be added manually. First we need to declare a member<br />

function OnToolTipNotify() in class CDBView:<br />

……<br />

……<br />

class CDBView : public CFormView<br />

{<br />

protected:<br />

};<br />

//}}AFX_MSG<br />

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

DECLARE_MESSAGE_MAP()<br />

Then, message mapping macros should be added to the implementation file:<br />

……<br />

BEGIN_MESSAGE_MAP(CDBView, CFormView)<br />

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

END_MESSAGE_MAP()<br />

Here, TTN_NEEDTEXT is sent through message WM_NOTIFY. Macro ON_NOTIFY_EX allows more than one<br />

object to process the specified message. If we use this macro, our message handler must return TRUE if the<br />

message is processed. If we do not process the message, we must return FALSE so that other objects can<br />

continue to process this message. Please note that in the above message mapping, the second parameter<br />

should always be 0.<br />

Member function CDBView::OnToolTipNotify(…) is implemented as follows:<br />

BOOL CDBView::OnToolTipNotify(UINT id, NMHDR *pNMHDR, LRESULT *pResult)<br />

{<br />

158

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

Saved successfully!

Ooh no, something went wrong!