简体   繁体   English

如何检测在图片框上按下的鼠标按钮?

[英]How can I detect a held down mouse button over a PictureBox?

I need to fire an event when the mouse is above a PictureBox with the mouse button already clicked and held down.当鼠标位于已经单击并按住鼠标按钮的图片框上方时,我需要触发一个事件。

Problems:问题:

The MouseDown and MouseEnter event handlers do not work together very well. MouseDown 和 MouseEnter 事件处理程序不能很好地协同工作。

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.例如,一旦单击并按住鼠标按钮,C# 将触发 MouseDown 事件处理程序,但是当光标移动到 PictureBox 上时,MouseEnter 事件不会触发,直到鼠标按钮被释放。

Mouse events鼠标事件

Use the MouseDown event to just detect a down press of a mouse button and set this.Capture to true so that you then get other mouse events, even when the mouse leaves the control (ie you won't get a MouseLeave event because you captured the mouse).使用 MouseDown 事件仅检测鼠标按钮的按下并将 this.Capture 设置为 true 以便您获得其他鼠标事件,即使鼠标离开控件(即您不会获得 MouseLeave 事件,因为您捕获了鼠标)。 Release capture by setting this.Capture to false when MouseUp occurs.当 MouseUp 发生时,通过将 this.Capture 设置为 false 来释放捕获。

Just checking the state of the mouse只是检查鼠标的状态

This may not be relevant, but you can check System.Windows.Control.MousePosition and see if it is in the PictureBox.ClientRectangle , then check the Control.MouseButtons static property for which buttons might be down at any time.这可能不相关,但您可以检查System.Windows.Control.MousePosition并查看它是否在PictureBox.ClientRectangle ,然后检查Control.MouseButtons静态属性,了解哪些按钮可能随时处于关闭状态。

As in:如:

if  (pictureBox.ClientRectangle.Contains(pictureBox.PointToClient(Control.MousePosition)))
{
   if ((Control.MouseButtons & MouseButtons.Left) != 0)
   {
     // Left button is down.
   }
}

When the mouse is pressed down most controls will then Control.Capture the mouse input.当按下鼠标时,大多数控件将Control.Capture鼠标输入。 This means that all MouseMove events are sent to the original control that captured rather than the control the mouse happens to be over.这意味着所有MouseMove事件都被发送到捕获的原始控件而不是鼠标碰巧经过的控件。 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.您需要窥探应用程序中发往其他控件的 Windows 消息。 To do that you need add a message filter ...为此,您需要添加一个消息过滤器...

Application.AddMessageFilter(myFilterClassInstance);

Then you need to implement the IMessageFilter on a suitable class...然后你需要在合适的类上实现 IMessageFilter ......

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.然后你观察鼠标移动事件并检查它们是否在你的图片框上,然后做任何你想做的事情。

Set up a MouseMove event within the PictureBox control:在 PictureBox 控件中设置 MouseMove 事件:

this.myPictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.myPictureBox_MouseMove);

Then, within your MouseMove event handler, check to see if the left mouse button (or whatever) is pressed:然后,在您的 MouseMove 事件处理程序中,检查是否按下了鼠标左键(或其他按钮):

private void myPictureBox_MouseMove(object sender, MouseEventArgs e)
{  
     if (e.Button == MouseButtons.Left)
         // Do what you want to do
}

If you're trying to implement a drag-and-drop operation of some sort, the Drag... events (DragEnter, DragDrop etc.) on the receiving picture box are what you want to use.如果您尝试实现某种类型的拖放操作,那么您需要使用接收图片框上的Drag... 事件(DragEnter、DragDrop 等)。 Basically, you start the drag operation using the DoDragDrop method of the source control, and then any control that you drag over will have its Drag... events raised.基本上,您使用源控件的 DoDragDrop 方法开始拖动操作,然后您拖动的任何控件都将引发其 Drag... 事件。

Search "DoDragDrop" on MSDN to see how to implement this.在 MSDN 上搜索“DoDragDrop”以了解如何实现这一点。

You can use the Preview Events您可以使用预览事件

For example say I want to detect a mousedown event on my button.例如,假设我想检测按钮上的 mousedown 事件。 The MouseDown event is not going to work because as one of the answers here, the mouse capture is sent to the main control, however what you can do is use the mouse preview event. MouseDown 事件将不起作用,因为作为此处的答案之一,鼠标捕获被发送到主控件,但是您可以做的是使用鼠标预览事件。

Here is a code example这是一个代码示例

I want to check when the Left Mouse Button is pressed on my Button, hence I use the PreviewMouseLeftButtonDown我想检查何时在我的按钮上按下鼠标左键,因此我使用 PreviewMouseLeftButtonDown

    private void MyButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        // code here
    }

WPF has preview events for alot of other events, you can read about them here WPF 有很多其他事件的预览事件,你可以在这里阅读它们

Preview Events - It particular talks about Buttons and how the mouse events interacts with it, So I highly recommend you read it 预览事件- 它特别讨论了按钮以及鼠标事件如何与其交互,所以我强烈建议你阅读它

The best way to move a Form based on mouse position and control relative position is similar to what Ian Campbell posted.根据鼠标位置和控件相对位置移动表单的最佳方法类似于 Ian Campbell 发布的内容。

    private void imgMoveWindow_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            Form1.ActiveForm.Left = Control.MousePosition.X - imgMoveWindow.Left - (imgMoveWindow.Size.Width/2);
            Form1.ActiveForm.Top = Control.MousePosition.Y - imgMoveWindow.Top - (imgMoveWindow.Size.Height/2); 
        }

    }

Where imgMoveWindow is a PictureBox Control.其中 imgMoveWindow 是一个 PictureBox 控件。

Bruno Ratnieks布鲁诺·拉特尼克斯

Sniffer Networks嗅探器网络

set a flag or a state on mouse down.在鼠标按下时设置标志或状态。 release it on mouse up.在鼠标向上释放它。 When on mouse over fires for the picture box check your state.当鼠标悬停在图片框上时,请检查您的状态。 Now you can detect when a person is dragging something.现在您可以检测一个人何时拖动某物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM