show tooltip on control

http://www.borngeek.com/coding/dialog/control_tooltips.html

Adding Tooltips to Dialog Controls

Back to the Coding Article Index

Enabling Tooltips

Enabling tooltips on dialog controls can be extremely useful, especially when a large number of controls are used. In this scenario, they could be used to describe what a particular control does, as well as any corresponding shortcut keys. Adding this feature is a relatively simple procedure. First we need to enable the tooltips by calling the following line of code in our OnInitDialog() method:

EnableToolTips(TRUE);

This function simply enables the tooltip control in our dialog window. We now need to add the following line of code to the message map in our dialog class. Note that the text OnToolTip in the code below is the name of the method that will be associated with this message. Feel free to change the name of this method if you like. For the purposes of this article, we will stick with using OnToolTip as the method name.

ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTip)

Handling the Tooltips

Next, add the OnToolTip() method declaration to the dialog class header file:

afx_msg BOOL OnToolTip(UINT id, NMHDR* pTTTStruct, LRESULT* pResult);

After we have declared this method, we need to add its code to the dialog class CPP file. The definition for this method is shown below:

BOOL CYourDlg::OnToolTip(UINT id, NMHDR* pNMHDR, LRESULT* pResult)
{
TOOLTIPTEXT *pTTT = (TOOLTIPTEXT*)pNMHDR;
UINT nID = pNMHDR->idFrom;
if(pTTT->uFlags & TTF_IDISHWND)
{
// idFrom is actually the HWND of the tool
nID = ::GetDlgCtrlID((HWND)nID);
if(nID)
{
pTTT->lpszText = MAKEINTRESOURCE(nID);
pTTT->hinst = AfxGetResourceHandle();
return(TRUE);
}
}
return(FALSE);
}

Now that our code is in place, we need to add the tooltip strings to the string table resource. In the resource editor, open the string table (insert a new one if your project doesn't already have a string table). Now add one string for each control that you want to show a tooltip. Each string's value should be set to the text that you want to appear in the tooltip. Most importantly, each string's ID should be set to the same ID of the corresponding control.

posted on 2006-11-10 06:44  cy163  阅读(...)  评论(...编辑  收藏

导航