VC拖动无标题窗口,同时可以响应WM_LBUTTONUP消息?????

huanglin03 2014-01-27 05:25:29
VC拖动无标题窗口,同时可以响应WM_LBUTTONUP消息?????
如何做到,现在都是移动了,但是一个消息都接收不到了,要求至少能接收到
WM_LBUTTONUP或者是WM_NCLBUTTONUP两个中的一个,如何搞啊
...全文
363 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
d14381381 2015-06-05
  • 打赏
  • 举报
回复
引用 3 楼 schlafenhamster 的回复:
GetAsyncKeyState If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus. 通常 if(GetAsyncKeyState(VK_LBUTTON)) //0x80000000 时 ,是 拖动 否则 是 拖动结束, 即 UP
if(GetAsyncKeyState(VK_LBUTTON)) //0x80000000 这个比较有用,谢谢了!
iRubiker 2015-05-07
  • 打赏
  • 举报
回复
解决拖动无标题窗口时无法响应WM_LBUTTONUP消息的问题,此方法亲测可用 捕获系统消息里处理,鼠标弹起up时会产生WM_CAPTURECHANGED消息
switch(message)
	{
	case WM_LBUTTONDOWN:
		{ 
			//AfxMessageBox(_T("鼠标按下"));
                           ***your code****
		}
		break;
	case WM_LBUTTONDBLCLK:
		{ 
			//AfxMessageBox(_T("鼠标双击"));
                         ***your code****
		}
		break;

	case WM_LBUTTONUP:
		{ 
			//AfxMessageBox(_T("鼠标弹起"));
			  ***your code****
		}
		break;
	case WM_CAPTURECHANGED:
		{ 
			//AfxMessageBox(_T("它失去捕获的鼠标"));
                         ***your code****
		}
		break;
	case WM_MOVING:
		{ 
			//AfxMessageBox(_T("鼠标移动"));
                            ***your code****
		}
		break;
	}
schlafenhamster 2014-01-28
  • 打赏
  • 举报
回复
GetAsyncKeyState If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus. 通常 if(GetAsyncKeyState(VK_LBUTTON)) //0x80000000 时 ,是 拖动 否则 是 拖动结束, 即 UP
huanglin03 2014-01-28
  • 打赏
  • 举报
回复
你没明白意思啊,是这样的:
实现了拖动无标题窗口,但是当停止拖动,鼠标左键放开时,收不到WM_LBUTTONUP消息,WM_NCLBUTTONUP也同样是收不到,这是怎么回事?
schlafenhamster 2014-01-28
  • 打赏
  • 举报
回复
大家探讨, 不必客气了
huanglin03 2014-01-28
  • 打赏
  • 举报
回复
完善后的代码:拖动无标题对话框,且移动时去虚框:
void CMsgTestDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (nFlags == MK_LBUTTON)
{
if (g_bSet == FALSE)
{
//移动之前去掉虚框
g_bSet = TRUE;
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,TRUE,NULL,0);
}
PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
}
CDialog::OnMouseMove(nFlags, point);
}
LRESULT CMsgTestDlg::OnNcHitTest(CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(GetAsyncKeyState(VK_LBUTTON)==0x0000)
{
if (g_bSet == TRUE)
{
//移动之后加上虚框
g_bSet = FALSE;
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,FALSE,NULL,0);
}
}
return CDialog::OnNcHitTest(point);
}

唯一的缺点是仍然收不到WM_LBUTTONUP消息,但解决了我的问题,不用这个消息也可以了,但其它消息仍在,像WM_MOUSEMOVE,WM_MOVING等,对整体的影响算是较小的了,另外,你刚提到的在OnNcHitTest中可能检测到UP,的情况,好像没有,我试了,鼠标按下,弹起,移动,均会调用OnNcHitTest一次,所以一定可以检测到的----在我这种代码模式下,
你说的检测不到的情况是这样的情境下可能会出现:
鼠标在界面上按下,保持按下不弹起,移出对话框界面后再弹起,这样就估计检测不到,我没试过,猜的,但我上面的代码之所以不存在那种情况,是因为我本来就是让窗口和鼠标按下时一起移动的,即不可能存在按下左键,保持按下不动的情况,还可以把鼠标移到整个窗口外!
感谢你的GetAsyncKeyState,老鸟儿,
schlafenhamster 2014-01-28
  • 打赏
  • 举报
回复
听说 OnNcHitTest函数 有时 会 检测 不到 UP
schlafenhamster 2014-01-28
  • 打赏
  • 举报
回复
BOOL CStaticDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class static CPoint ptLast; static BOOL bDown=FALSE; if(pMsg->message == WM_MOUSEMOVE) { CPoint newPt; if(GetAsyncKeyState(VK_LBUTTON)) { bDown=TRUE; CRect rc; m_Static.GetWindowRect(&rc); m_Static.ScreenToClient(&rc); m_Static.MapWindowPoints(this,&rc); newPt=pMsg->pt; ScreenToClient(&newPt); if(rc.PtInRect(newPt)) { rc.OffsetRect(newPt.x-ptLast.x,newPt.y-ptLast.y); ptLast = newPt; m_Static.MoveWindow(&rc); m_Static.RedrawWindow(); } } else { if(bDown) afxDump << "Up\n"; bDown=FALSE; newPt=pMsg->pt; ScreenToClient(&newPt); ptLast = newPt; } } return CDialog::PreTranslateMessage(pMsg); } 红色的是对你有用的。
huanglin03 2014-01-28
  • 打赏
  • 举报
回复
老牛,你好
现在总算找了个还令我满意的解决方法,下面再说,先说你提的检测
我检测到按下时的值是FFFF8000,UP时的返回值确实是0x00000000,我当初检测0是因为看到返回0,函数返回值是SHORT类型,哪里有不对?

这都算了,现在我在程序里响应了OnNcHitTest函数,我发现这个函数不管是鼠标移动还是点击都会被响应,所以我干脆把检测放到这个函数里去就行了,但是有一点,我只之所要检测UP,是因为现在WM_LBUTTONDOWN有,但WM_LBUTTONUP检测不到,我要在DOWN时调用SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,TRUE,NULL,0);就是移动时去掉虚框,然后在UP时再调用一次,恢复WIN7移动时的带虚框的情况,所以不能无条件的一直检测,而是要保证这一对操作一定要成对出现,

就是在DOWN时,调用那个API,去掉异形窗口移动时的虚框,并设一个标志,通知OnNcHitTest开始检测,当OnNcHitTest检测到UP时,调用一次那个API,还原移动时的虚框
现在看来似乎没什么大问题了,因为我不想为这一个小问题就开一个定时器,去刷GetAsyncKeyState,虽然这不算什么开销,但我不想这么干,,,上面的解决思路不晓得牢靠不牢靠,求评估,
schlafenhamster 2014-01-28
  • 打赏
  • 举报
回复
if(GetAsyncKeyState(VK_LBUTTON)==0) 为什么检测==0 0x80000000 最高位 是 ‘按下’
huanglin03 2014-01-28
  • 打赏
  • 举报
回复
void CTestDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(GetAsyncKeyState(VK_LBUTTON)==0)
{
TRACE("up!!!!!!!!!!\n");//-----根本进不来
}
else
{
TRACE("dn!!!!!!!!!!\n");
}
CDialog::OnLButtonDown(nFlags, point);
}
schlafenhamster 2014-01-28
  • 打赏
  • 举报
回复
void CTestDlg::OnLButtonDown(UINT nFlags, CPoint point) { if (point.y < 25 && point.x < 950) { if(GetAsyncKeyState(VK_LBUTTON)) PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y)); else // mouse UP !!!!! } CDialog::OnLButtonDown(nFlags, point); }
huanglin03 2014-01-28
  • 打赏
  • 举报
回复
你说的可能可以,但不是最好的方法,你那要在一个地方不断的主动调用那个函数去检测鼠标左键UP信息,然后做下一步,如果可以找到上面收不到消息的原因,从派发消息的角度解决,是最好不过的了。

为了实现无标题窗口的拖动,只需要加入下面的代码:
void CTestDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
if (point.y < 25 && point.x < 950)
{
PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
}
CDialog::OnLButtonDown(nFlags, point);
}

但这样一下,就收不到WM_LBUTTONUP消息,WM_NCLBUTTONUP消息,不晓得为什么啊
schlafenhamster 2014-01-27
  • 打赏
  • 举报
回复
“拖动无标题窗口”实际上 是: 1. 鼠标在 客户区 2. 鼠标 不在 子 窗口 所以应该 不 影响 点击 当然 不能 同时 拖动 + 加别的功能

15,979

社区成员

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

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