C# 如何检测在图片框上按下的鼠标按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/169590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I detect a held down mouse button over a PictureBox?
提问by Justin Tanner
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 事件不会触发,直到鼠标按钮被释放。
采纳答案by Phil Wright
When the mouse is pressed down most controls will then Control.Capturethe mouse input. This means that all MouseMoveevents 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.
当按下鼠标时,大多数控件将Control.Capture鼠标输入。这意味着所有MouseMove事件都被发送到捕获的原始控件而不是鼠标碰巧经过的控件。这一直持续到鼠标丢失捕获,这通常发生在鼠标向上。
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 ...
如果您真的需要知道鼠标何时在您的控件上,即使另一个控件已捕获鼠标输入,那么您只有一种方法。您需要窥探应用程序中发往其他控件的 Windows 消息。为此,您需要添加一个消息过滤器...
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.
然后你观察鼠标移动事件并检查它们是否在你的图片框上,然后做任何你想做的事情。
回答by Hyman B Nimble
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.
在鼠标按下时设置标志或状态。在鼠标向上释放它。当鼠标悬停在图片框上时,请检查您的状态。现在您可以检测到一个人何时在拖动某物。
回答by Jeff Yates
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 (i.e. you won't get a MouseLeave event because you captured the mouse). Release capture by setting this.Capture to false when MouseUp occurs.
使用 MouseDown 事件仅检测鼠标按钮的按下并将 this.Capture 设置为 true 以便您获得其他鼠标事件,即使鼠标离开控件(即您不会获得 MouseLeave 事件,因为您捕获了鼠标)。当 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.
}
}
回答by MusiGenesis
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. 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.
如果您尝试实现某种类型的拖放操作,那么您需要使用接收图片框上的Drag... 事件(DragEnter、DragDrop 等)。基本上,您使用源控件的 DoDragDrop 方法开始拖动操作,然后您拖动的任何控件都将引发其 Drag... 事件。
Search "DoDragDrop" on MSDN to see how to implement this.
在 MSDN 上搜索“DoDragDrop”以了解如何实现这一点。
回答by Ian Campbell
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
}
回答by Bruno Ratnieks
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
嗅探器网络