为控件增加Tip提示功能,首要的工作是创建一个CToolTipCtrl控件,这是提示信息的载体,因为提示信息是要显示在此控件上的。
MFC上说的很清楚,创建CToolTipCtrl控件要分为两步:You construct aCToolTipCtrl in two steps. First, call the constructor to construct theCToolTipCtrl object, and then call Create to create the tool tip control and attach it to theCToolTipCtrl object.
第一步:声明一个CToolTipCtrl对象: CToolTipCtrl m_pToolTipCtrl;
第二步:CToolTipCtrl控件的创建:if( !m_pToolTipCtrl.Create(this) ) return FALSE;这里采用的是Create()函数。一下是MFC中Create()函数的介绍:
1
Create函数的介绍:
Creates a tool tip control and attaches it to a CToolTipCtrl object.
virtual BOOL Create(
CWnd* pParentWnd,
DWORD dwStyle = 0
);
Parameters:
pParentWnd
Specifies the tool tip control's parent window, usually a CDialog. It must not be NULL.
dwStyle
Specifies the tool tip control's style. See the Remarks section for more information.
Return Value
Nonzero if the CToolTipCtrl object is successfully created; otherwise 0.
其次,添加需要增加提示功能的控件,并启用目标窗口的ToolTip 属性:
m_ToolTipCtrl.AddTool(GetDlgItem(IDC_EDIT), LPSTR_TEXTCALLBACK);
m_ToolTipCtrl.AddTool(GetDlgItem(IDC_BUTTON), LPSTR_TEXTCALLBACK);
EnableToolTips(TRUE);
AddTool()函数的解释如下:
1
2
3
Registers a tool with the tool tip control.
BOOL AddTool(
CWnd* pWnd,
LPCTSTR lpszText = LPSTR_TEXTCALLBACK,
LPCRECT lpRectTool = NULL,
UINT_PTR nIDTool = 0
);
Parameters
pWnd
Pointer to the window that contains the tool.
nIDText
ID of the string resource that contains the text for the tool.
lpRectTool
Pointer to a RECT structure containing coordinates of the tool's bounding rectangle. The coordinates are relative to the upper-left corner of the client area of the window identified by pWnd.
nIDTool
ID of the tool.
lpszText
Pointer to the text for the tool. If this parameter contains the value LPSTR_TEXTCALLBACK, TTN_NEEDTEXT notification messages go to the parent of the window that pWnd points to.
Collapse imageReturn Value
Nonzero if successful; otherwise 0.
最后,增加消息的映射:
1、消息函数的定义:afx_msg BOOL ToolTipFxn(UINT id, NMHDR *pNMHDR, LRESULT *pResult);
2、添加映射:ON_NOTIFY_EX(TTN_NEEDTEXT, 0, &CDlgTestDlg::ToolTipFxn)
3、函数实现:
函数实现:
BOOL CTest9Dlg::ToolTipFxn( UINT id, NMHDR * pTTTStruct, LRESULT * pResult )
{
UNREFERENCED_PARAMETER(id);
TOOLTIPTEXT *pTTT=(TOOLTIPTEXT *)pTTTStruct;
UINT_PTR nID=pTTTStruct->idFrom;
if (pTTT->uFlags && TTF_IDISHWND)
{
//nID = ::GetDlgCtrlID((HWND)nID);
switch (nID)
{
case IDC_EDIT:
StrText.Empty();
GetDlgItemText(nID,StrText);
pTTT->lpszText=(LPTSTR)(LPCTSTR)StrText;
pTTT->hinst=AfxGetResourceHandle();
break;
case IDC_BUTTON:
str=_T("按钮测试");
wcscpy(pTTT->lpszText,str);
break;
}
}
*pResult = 0;
return TRUE;
}