MFC MAPX 如何实现 把鼠标移动到图元上后 冒出一个气泡显示图元信息

吴瀚 2017-12-11 05:19:13
我查了一些资料,有用CPPToolTip ,但是都是介绍给工具栏添加的。
如图 ,把鼠标移到那个绿色的线上 然后能显示线的信息
...全文
227 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2017-12-16
  • 打赏
  • 举报
回复
1 CToolTipCtrl m_TT; 2 初始化

void CPieHasTTDlg::InitTT(UINT radius,CPoint center)
{
	m_PieRadius=radius;
	m_PieCenter=center;
//
	m_TT.Create(this);
// pie
	CRect rc1(m_PieCenter.x-m_PieRadius,m_PieCenter.y-m_PieRadius,
			  m_PieCenter.x+m_PieRadius,m_PieCenter.y+m_PieRadius);
	
	m_TT.AddTool(this,LPSTR_TEXTCALLBACK,&rc1,1);
// horz line 
	CRect rc2(0,398,255*3,402);
	m_TT.AddTool(this,LPSTR_TEXTCALLBACK,&rc2,2);
// oblique line 斜线
	CRect rc3(0,0,160,160);
	m_TT.AddTool(this,LPSTR_TEXTCALLBACK,&rc3,3);
//
	m_TT.Activate(TRUE);
}
3 hit test

void CPieHasTTDlg::MyToolHitTest()
{
static int nLastID=0;
	int nCurrentID=-1;
	CPoint mouse;
	GetCursorPos(&mouse);
	ScreenToClient(&mouse);
// horz line test
	CRect rc(0,398,255*3,402);
	if(rc.PtInRect(mouse))
	{
		nCurrentID = mouse.x/3;
		if(nLastID != nCurrentID)
		{
			//afxDump << nCurrentID << "::" << nLastID << "\n";
			nLastID = nCurrentID;
			m_TT.Update();
		}
		return;
	}
// oblique line test
	CRect rc1(10,10,160,160);
	if(rc1.PtInRect(mouse))
	{
		nCurrentID = mouse.x;
		if(nLastID != nCurrentID)
		{
			//afxDump << nCurrentID << "::" << nLastID << "\n";
			nLastID = nCurrentID;
			m_TT.Update();
		}
		return;
	}

// pie test
	double dx=mouse.x-m_PieCenter.x;
	double dy=mouse.y-m_PieCenter.y;
	if(sqrt(dx*dx+dy*dy)<=m_PieRadius)
	{

		double angle=atan(fabs(dy)/fabs(dx))*DEG;// < 0 y axis up
		if		(dy<0 && dx>0) angle=atan(fabs(dy)/fabs(dx))*DEG;
		else if (dy<0 && dx<0) angle=90+atan(fabs(dx)/fabs(dy))*DEG;
		else if (dy>0 && dx<0) angle=180+atan(fabs(dy)/fabs(dx))*DEG;
		else				   angle=270+atan(fabs(dx)/fabs(dy))*DEG;
//			
		nCurrentID = int(angle/30.0);
		if(nLastID != nCurrentID)
		{
//			afxDump << nCurrentID << "::" << nLastID << "\n";
			nLastID = nCurrentID;
			m_TT.Update();
		}
	}
}

4 hit test 调用

void CPieHasTTDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	MyToolHitTest();
	CDialog::OnMouseMove(nFlags, point);
}
5 event map afx_msg BOOL OnToolTipText(UINT /*id*/, NMHDR * pNMHDR, LRESULT * pResult); // ON_NOTIFY_EX(TTN_NEEDTEXT,0,OnToolTipText) 6 tip text

//
BOOL CPieHasTTDlg::OnToolTipText(UINT /*id*/, NMHDR * pNMHDR, LRESULT * pResult)
{
	TOOLTIPTEXT* pTTT = (TOOLTIPTEXT*)pNMHDR; 
	UINT nID = pNMHDR->idFrom;
	char TipNow[80];
	CPoint mouse;
	GetCursorPos(&mouse);
	ScreenToClient(&mouse);
// idFrom is actually the HWND of the 1000=IDC_TREE
	if(nID==1)
	{// pie
		double dx=mouse.x-m_PieCenter.x;
		double dy=mouse.y-m_PieCenter.y;

		if(sqrt(dx*dx+dy*dy)<=m_PieRadius)
		{
			double angle=atan(fabs(dy)/fabs(dx))*DEG;// < 0 y axis up
			if		(dy<0 && dx>0) angle=atan(fabs(dy)/fabs(dx))*DEG;
			else if (dy<0 && dx<0) angle=90+atan(fabs(dx)/fabs(dy))*DEG;
			else if (dy>0 && dx<0) angle=180+atan(fabs(dy)/fabs(dx))*DEG;
			else				   angle=270+atan(fabs(dx)/fabs(dy))*DEG;
//			
			sprintf(TipNow,"Inside Pie: %d",int(angle/30.0));
			//afxDump << TipNow << "\n";
			pTTT->lpszText = TipNow;
		}
		return TRUE;// ! for win7
	}
	else if(nID==2)
	{// horz line
		sprintf(TipNow,"mouseX: %d",mouse.x/3);
//		afxDump << TipNow << "\n";
		pTTT->lpszText = TipNow;
		return TRUE;// ! for win7
	}
	else if(nID==3)
	{// obligue line
		CDC *pDC = GetDC();
		COLORREF color=pDC->GetPixel(mouse.x,mouse.y);
		ReleaseDC(pDC);
		if(color==RGB(0,0,255))// blue
		{
			sprintf(TipNow,"X: %d,Y: %d",mouse.x,mouse.y);
	//		afxDump << TipNow << "\n";
			pTTT->lpszText = TipNow;
			return TRUE;// ! for win7
		}
	}
//	
	return FALSE;// we didn't handle the message,
}
7 event relay

BOOL CPieHasTTDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	if((IsWindow(m_hWnd))  && (pMsg->message==WM_MOUSEMOVE))
		m_TT.RelayEvent(pMsg);	
	return CDialog::PreTranslateMessage(pMsg);
}
你只要 oblique line 部分, 斜线颜色 请 改为绿 (现在是 蓝)
schlafenhamster 2017-12-15
  • 打赏
  • 举报
回复
需要响应 OnToolTipText(UINT /*id*/, NMHDR * pNMHDR, LRESULT * pResult)
吴瀚 2017-12-14
  • 打赏
  • 举报
回复
你好,我试了一下 还是没有显示呢
boylafong 2017-12-12
  • 打赏
  • 举报
回复
CToolTipCtrl m_tipBtnCtr; m_tipBtnCtr.Create(this, TTS_ALWAYSTIP); m_tipBtnCtr.AddTool(this); 在mousemove中判断,一到你要的区域绿线上,就添加所需显示的文本内容 strTemp = _T("绿线上"); m_tipBtnCtr.UpdateTipText(strTemp, this); 一离开指定位置,就把文本消除 strTemp = _T(""); m_tipBtnCtr.UpdateTipText(strTemp, this); 3.BOOL CDlg::PreTranslateMessage(MSG* pMsg) { if ( NULL != m_tipBtnCtr) { m_tipBtnCtr.RelayEvent(pMsg); } return CDialog::PreTranslateMessage(pMsg); }

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧