MFC 鼠标悬停提示框

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);
}
相关推荐
寻寻觅觅☆7 小时前
东华OJ-基础题-106-大整数相加(C++)
开发语言·c++·算法
fpcc7 小时前
并行编程实战——CUDA编程的Parallel Task类型
c++·cuda
ceclar1238 小时前
C++使用format
开发语言·c++·算法
lanhuazui109 小时前
C++ 中什么时候用::(作用域解析运算符)
c++
charlee449 小时前
从零实现一个生产级 RAG 语义搜索系统:C++ + ONNX + FAISS 实战
c++·faiss·onnx·rag·语义搜索
老约家的可汗9 小时前
初识C++
开发语言·c++
crescent_悦9 小时前
C++:Product of Polynomials
开发语言·c++
小坏坏的大世界10 小时前
CMakeList.txt模板与 Visual Studio IDE 操作对比表
c++·visual studio
乐观勇敢坚强的老彭10 小时前
c++寒假营day03
java·开发语言·c++
愚者游世10 小时前
brace-or-equal initializers(花括号或等号初始化器)各版本异同
开发语言·c++·程序人生·面试·visual studio