在PreTranslateMessage中处理mouse move的消息,解决Picture Control设置notify为true之后与对话框OnMouseMove冲突的问题

在PreTranslateMessage中处理mouse move的消息,解决Picture Control设置notify为true之后与对话框OnMouseMove冲突的问题

(注Picture Control的notify属性设置为true是为了使该控件响应点击等事件)

MFC中 监听OnMouseMove,当鼠标在Picture Control控件对应的rect区域时,更换Picture Control控件中显示的图片,但是当Picture Control控件的notify属性设置为true时,OnMouseMove中判断鼠标是否在Picture Control控件的rect区域不会响应,即这时候想实现Picture Control控件获取鼠标焦点时展示不同的视觉效果会失败。

最后解决办法是,把判断鼠标是否位于Picture Control控件的rect区域这个过程放到PreTranslateMessage处理。即如下的代码:


BOOL CExampleSixDlg::PreTranslateMessage(MSG *pMsg)
{

	//解决picture control的notify为true时与对话框mouse move时间冲突的问题。
	if (pMsg->message == WM_MOUSEMOVE)
	{

		CRect rect;
		GetDlgItem(IDC_STATIC_PICTURE)->GetWindowRect(&rect);		//获取控件坐标 相对于屏幕左上角

		CPoint  pt;
		GetCursorPos(&pt);		//获取光标在屏幕坐标中的位置 

		if (rect.PtInRect(pt))
		{
			//在control区域内,提示信息,更换图片等 
			m_button.SetWindowText(_T("focus"));
			//GetDlgItem(IDC_PICTURE_TEST)->SetIcon(m_tIcon,TRUE);
			
			//加载png图片
			CStatic *pwd = (CStatic *)GetDlgItem(IDC_STATIC_PICTURE);
			CImage img;
			img.Load(_T("./res/test2.png"));
			HBITMAP hBmp = img.Detach();
			pwd->SetBitmap(hBmp);
			
			//加载图标icon
			pwd = (CStatic *)GetDlgItem(IDC_PICTURE_TEST);
			pwd->SetIcon(m_hIcon);

		}
		else
		{
			//不在control区域内,提示信息,更换图片等 
			m_button.SetWindowText(_T("focus not"));
			//GetDlgItem(IDC_PICTURE_TEST)->SetIcon(m_hIcon,TRUE);
			
			//加载png图片
			CStatic *pwd = (CStatic *)GetDlgItem(IDC_STATIC_PICTURE);
			CImage img;
			img.Load(_T("./res/test1.png"));
			HBITMAP hBmp = img.Detach();
			pwd->SetBitmap(hBmp);

			//加载图标icon
			pwd = (CStatic *)GetDlgItem(IDC_PICTURE_TEST);
			pwd->SetIcon(m_tIcon);

		}   
	}

	return CDialog::PreTranslateMessage(pMsg);
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值