为控件添加Tip提示功能CToolTipCtrl

为控件增加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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小米的修行之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值