在MFC中检测鼠标单击 [英] Detecting a single mouse click in MFC

查看:557
本文介绍了在MFC中检测鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MFC中,鼠标双击事件触发以下消息序列

In MFC a double-mouse click event triggers the following sequence of messages

  • WM_LBUTTONDOWN
  • WM_LBUTTONUP
  • WM_LBUTTONDBCLK
  • WM_LBUTTONUP

因此,响应 WM_LBUTTONDBCLK 消息,您可以检测到双击.但是,如果我只想检测单击,该如何区分呢?

So responding to the WM_LBUTTONDBCLK message allows you to detect a double-click. But if I just want to detect a single-click how to I distinguish it?

但是仅查看 WM_LBUTTONUP 消息是不够的,因为它可能是单击或双击的第一次.

But just looking at the WM_LBUTTONUP message isn't enough as it could be a single-click or it could be the first click of a double-click.

如何仅单击即可成功识别?

How can I successfully identify just a single-click?

推荐答案

(请允许我将这些事件称为Mouse Up和Mouse Down.我的MFC有点生锈.而且有一个名为.NET的东西正在搞乱我的最近的术语;-)

(Please allow me to call these events Mouse Up and Mouse Down. My MFC is a little rusty. And there's this stuff called .NET who's been messing up my terminology lately ;-)

短篇小说:您不只是想了解鼠标单击.您还需要更多.

Short story: You don't simply want to know about Mouse Click. You need more.

长话:

尽管这是违反直觉的,但似乎只需要单击鼠标就很少见了.大多数情况下,您将需要在Mouse Down上执行一些处理,并在Mouse Up上进行一些进一步的处理.诀窍是仅仅跟踪Mouse Up消息是不够的:Mouse Down可能未在窗口中发生.那您认为这是有效的点击吗?特别是考虑到没有发生Mouse Down处理(例如选择一个项目).

Although this is counter-intuitive, it appears that simply wanting a mouse-click is fairly uncommon. Most often, you'll want to perform some processing on Mouse Down and do some further processing on Mouse Up. The trick is that simply tracking Mouse Up messages is not enough: Mouse Down may not have happened in your window. Do you consider it a valid click then? Especially considering that the Mouse Down processing (such as selecting an item) did not occur.

进一步进行推理,在处理鼠标按下"后,您不应该依赖于收到鼠标按下":用户可能已移动鼠标并将按钮释放到其他位置(请考虑拖放),在这种情况下,您可以不会收到MouseUp事件...除非您在MouseDown上捕获了鼠标,以确保即使鼠标离开了窗口,鼠标事件也直到Mouse Up为止.

Going further up the reasoning, you should not rely on receiving a Mouse Up after you processed Mouse Down: User may have moved the mouse and released the button somewhere else (think drag'n'drop), in which case, you don't receive the MouseUp event... unless you capture the mouse on MouseDown to make sure you get mouse event up to Mouse Up even if the mouse left your window.

总而言之,您最终将跟踪Mouse Down,捕获鼠标,并且在收到Mouse Up时,只需检查您是否拥有捕获即可.如果不是,则双击鼠标(没有第二个鼠标按下),或者在其他地方发生鼠标按下,因此您很可能不在乎此鼠标向上.

All in all, you end up tracking Mouse Down, capture the mouse and when you receive Mouse Up, just check if you own the capture. If not, the mouse was either double-clicked (no 2nd mouse down) or Mouse Down happened somewhere else hence you most likely don't care about this Mouse Up.

结论:之所以没有MouseClick消息仅仅是因为您不会走得太远:无论如何,您需要处理更多消息并实施更多机制.

In conclusion: There's no MouseClick message simply because you wouldn't go very far with it: You need to handle more messages and implement more mechanics anyway.

哦!并且,如果您处理的是已经处理所有这些项目和选择内容的现有控件(例如列表视图),那么它很可能会提供类似的自定义通知,例如项目激活"或项目选择已更改".

Oh! And if your dealing with an existing control which already handles all this items and selection stuff, such as a listview, chances are it provides with a similar custom notification such as Item Activate or Item Selection Changed.

这篇关于在MFC中检测鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆