project->add to project->component and controls->Visual C++ Components-> ToolTip Support 插入后, 将OnInitDialog中的 m_tooltip.AddTool(GetDlgItem(IDC_<name>), "<text>"); 改为 m_tooltip.AddTool(GetDlgItem(IDOK), "确定");
用CToolTipCtrl类像添加其他的tip不一样么?
假设你有了名为My的对话框的工程.首先建立一个Button,方法:在ResourceView中双击Dialog,添加Button采用默认值:IDC_BUTTON1.实现PreTranslateMessage消息的相应函数. class CMyDlg : public CDialog { public: CToolTipCtrl m_ToolTips; ............... }; BOOL CMyDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CButton *pButton; pButton=(CButton *)GetDlgItem(IDC_BUTTON1); m_ToolTips.Create(this); m_ToolTips.AddTool(pButton,"This is a ToolTips text for this Button"); return TRUE; // return TRUE unless you set the focus to a control } BOOL CMyDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class switch(pMsg->message) { case WM_LBUTTONDOWN: case WM_LBUTTONUP: case WM_MOUSEMOVE: m_ToolTips.RelayEvent(pMsg); } return CDialog::PreTranslateMessage(pMsg); }
首先你的Button应该是由自己做的Button类里派生的在这个类里要处理MOUSEMOVE事件
首先SetCapture(),当捕获到点不在这个Button上时ReleaseCapture()
这两个状态下都向父窗口发送自定义消息(Button的ID,可以加参数TRUE为显示、FALSE为消隐或者用两个不同的消息)父窗口中的消息相应函数中处理显示与消隐(可以用一个Dialog或Button等写入文字)窗口的大小由文字长短和字体决定根据每一个Button的ID标示不同的提示信息。
ToolTip Support
插入后, 将OnInitDialog中的
m_tooltip.AddTool(GetDlgItem(IDC_<name>), "<text>");
改为
m_tooltip.AddTool(GetDlgItem(IDOK), "确定");
class CMyDlg : public CDialog
{
public:
CToolTipCtrl m_ToolTips;
...............
}; BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // TODO: Add extra initialization here CButton *pButton;
pButton=(CButton *)GetDlgItem(IDC_BUTTON1);
m_ToolTips.Create(this);
m_ToolTips.AddTool(pButton,"This is a ToolTips text for this Button");
return TRUE; // return TRUE unless you set the focus to a control
} BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
switch(pMsg->message)
{
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_MOUSEMOVE:
m_ToolTips.RelayEvent(pMsg);
}
return CDialog::PreTranslateMessage(pMsg);
}
在类定义中加入如下函数:
afx_msg BOOL ShowTips(UINT id,NMHDR* PNMHDR,LRESULT* pResult);
在消息映射宏中加入:
ON_NOTIFY_EX(TTN_NEEDTEXT,0,ShowTips)
实现ShowTips函数:
BOOL CTestDlg::ShowTips(UINT id,NMHDR* PNMHDR,LRESULT* pResult)
{
TOOLTIPTEXT* pTTT=(TOOLTIPTEXT*)PNMHDR;
UINT nID=PNMHDR->idFrom;
if(pTTT->uFlags&TTF_IDISHWND)
{
nID=::GetDlgCtrlID((HWND)nID)&::GetMenuItemID((HMENU)nID,2);
//此处我想实现菜单工具提示,但没有完成,“GetMenuItemID”是我自己加的
//好像对静态控件也不能实现
if(nID)
{
pTTT->lpszText=MAKEINTRESOURCE(nID);
pTTT->hinst=AfxGetResourceHandle();
return(TRUE);
}
}
return(FALSE);
}最好在String Table加入提示语句,任何控件都可以显示提示,包括按钮,静态,列表框等......