MFC 鼠标悬停提示框

运行效果

在MFC窗口中添加一个控件

  1. 工具栏中拖拽List Box到MFC窗口
  2. List Box添加变量 CListBox m_listbox

增加成员变量

CWnd* m_tip_parent_wnd;CToolTipCtrl m_tip;

m_listbox创建提示框

void create_tip_window(CWnd* tip_wnd, CToolTipCtrl* tip){tip->Create(tip_wnd);tip->AddTool(tip_wnd, TTS_ALWAYSTIP);tip->SetTipTextColor(RGB(200, 0, 0);tip->SetDelayTime(500)}// 在 OnInitDialog 函数中调用如下代码:m_tip_parent_wnd = &m_listbox;create_tip_window(m_tip_parent_wnd, &m_tip);

重新 virtual BOOL PreTranslateMessage(MSG* pMsg)

BOOL PreTranslateMessage(MSG* pMsg){if (m_tip.m_hWnd != NULL){m_tip.RelayEvent(pMsg);}return CDialogEx::PreTranslateMessage(pMsg);}

增加鼠标移动消息映射ON_WM_MOUSEMOVE()

BEGIN_MESSAGE_MAP(XXX, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_WM_MOUSEMOVE()END_MESSAGE_MAP()

处理鼠标移动消息afx_msg void OnMouseMove(UINT nFlags, CPoint point);

void OnMouseMove(UINT nFlags, CPoint point){m_tip.UpdateTipText(_T("This is test text!"), m_tip_parent_wnd);CDialogEx::OnMouseMove(nFlags, point);}