我怎样才能检测过一个PictureBox一个按住鼠标按钮? [英] How can I detect a held down mouse button over a PictureBox?

查看:114
本文介绍了我怎样才能检测过一个PictureBox一个按住鼠标按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要当鼠标是已经点击并按住鼠标按钮图片框上方触发一个事件

I need to fire an event when the mouse is above a PictureBox with the mouse button already clicked and held down.

问题:

鼠标按下和MouseEnter事件处理程序不在一起工作得很好。

The MouseDown and MouseEnter event handlers do not work together very well.

例如一次鼠标点击按钮,并按住,C#会火MouseDown事件处理程序,但是当光标移动在图片框MouseEnter事件不火,直到鼠标按钮realeased。

For instance once a mouse button is clicked and held down, C# will fire the MouseDown event handler, but when the cursor moves over the PictureBox the MouseEnter event does not fire, until the mouse button is realeased.

推荐答案

当鼠标按下大多数控件也就那么的 Control.Capture 的鼠标输入。这意味着所有的的MouseMove 的事件发送到拍摄的,而不是鼠标恰好是在控制原有的控制。这个过程持续到鼠标失去捕获这通常发生在鼠标。

When the mouse is pressed down most controls will then Control.Capture the mouse input. This means that all MouseMove events are sent to the original control that captured rather than the control the mouse happens to be over. This continues until the mouse loses capture which typically happens on the mouse up.

如果您真的需要知道,当鼠标悬停即使另一个控件已捕获鼠标输入,那么你只有真的有一个办法你的控制。你需要窥探往您的应用程序内其他控件的窗口消息。要做到这一点你需要添加一个消息过滤器...

If you really need to know when the mouse is over your control even when another control has captured mouse input then you only really have one way. You need to snoop the windows messages destined for other controls inside your application. To do that you need add a message filter ...

Application.AddMessageFilter(myFilterClassInstance);



然后,你需要实现合适的类...

Then you need to implement the IMessageFilter on a suitable class...

public class MyFilterClass : IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_MOUSEMOVE)
            // Check if mouse is over my picture box!

        return false;
    }
}



然后你看的鼠标移动事件,并检查他们超过你的图片框,做什么是你想做的事情。

Then you watch for mouse move events and check if they are over your picture box and do whatever it is you want to do.

这篇关于我怎样才能检测过一个PictureBox一个按住鼠标按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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