添加重载虚函数、添加tooltip对象
// CDemo01Dlg 对话框
class CDemo01Dlg : public CDialogEx
{
// 构造
public:
CDemo01Dlg(CWnd* pParent = nullptr); // 标准构造函数
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_DEMO01_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
// 添加下面的函数、对象
public:
afx_msg void OnMouseMove(UINT nFlags, CPoint point);// WM_MOVEMOUSE消息
virtual BOOL PreTranslateMessage(MSG* pMsg); //处理键盘和鼠标消息
void InitToolTip(); //初始化tooltip
CToolTipCtrl m_toolTipCtrl; //对象
};
tooltip提示框初始化
//提示框初始化
void CDemo01Dlg::InitToolTip()
{
if (m_toolTipCtrl.m_hWnd == NULL)
{
m_toolTipCtrl.Create(this);
m_toolTipCtrl.Activate(true);
m_toolTipCtrl.AddTool(this, TTS_ALWAYSTIP);
m_toolTipCtrl.SetMaxTipWidth(500);//设定显示宽度,超长内容自动换行
m_toolTipCtrl.SetTipBkColor(RGB(210, 210, 255));//设置背景色
m_toolTipCtrl.SetTipTextColor(RGB(0, 106, 53));//设置文本色
//m_toolTipCtrl.SetDelayTime(TTDT_INITIAL, 10); //鼠标指向后延迟10ms再显示提示
//m_toolTipCtrl.SetDelayTime(TTDT_AUTOPOP, 3000); //鼠标显示提示保持3s
//m_toolTipCtrl.SetDelayTime(TTDT_RESHOW, 1000);;//鼠标从当前控件移动到另一个控件后,等待1秒后显示提示;
}
}
在OnInitDialog()函数中初始化tooltip
BOOL CDemo01Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
//提示框
InitToolTip();
return TRUE;
}
处理键盘和鼠标消息
BOOL CDemo01Dlg::PreTranslateMessage(MSG* pMsg)
{
m_toolTipCtrl.RelayEvent(pMsg);
return CDialogEx::PreTranslateMessage(pMsg);
}
获取且tooltip更新文本
void CDemo01Dlg::OnMouseMove(UINT nFlags, CPoint point)
{
TCHAR szBuffer[255] = {0};
_stprintf_s(szBuffer, _countof(szBuffer), _T("鼠标位置: x = %d, y = %d"), point.x, point.y);
SetWindowText(szBuffer); //显示到对话框标题
CString strPos;
strPos.Format(_T("[%d, %d] "), point.x, point.y);
m_toolTipCtrl.UpdateTipText(strPos, this);//随鼠标移动更新文本
CDialogEx::OnMouseMove(nFlags, point);
}