我希望在控件上有三个鼠标操作:左,右和两个。
我有左右两边,目前我正在使用第三个中间按钮,但我很好奇如何使用左右按钮被按在一起,对于那些用户没有中间按钮的鼠标。这将在自定义控件的OnMouseDown方法中处理。
更新在查看建议的答案之后,我需要澄清我试图做的是在MouseDown事件中实现鼠标单击(实际上是控件的OnMouseDown方法)。因为当点击鼠标左键和右键(每个按钮一个)时,.NET总是会引发两个MouseDown事件,我猜测这样做的唯一方法就是做一些低级别的窗口消息管理或在MouseDown之后实现某种操作的延迟执行。最后,使用鼠标中键更简单。
现在,如果行动发生在MouseUp上,那么Gary或者nos的建议会很好。
任何关于这个问题的进一步见解将不胜感激。谢谢!
总是有“自己动手”的方法:
只需记住按钮按下和释放的状态。在OnMouseDown中,您只需记住按下的按钮,在OnMouseUp中只需检查记住哪些按钮,以及清除按钮的状态。
释放按钮时,您需要一些逻辑来不执行多个操作。就像是
MouseButtons buttonPressed;
..
void OnMouseDown(MouseEventArgs e)
{
buttonPressed |= e.Button;
}
void OnMouseUp(MouseEventArgs e)
{
if(!doneAction) {
if((buttonPressed & MouseButtons.Right == MouseButtons.Right
&& buttonPressed & MouseButtons.Left == MouseButtons.Left)
|| buttonPressed & MouseButtons.Middle== MouseButtons.Middle) {
DoMiddleAction();
doneAction = true;
} else if(check Right button , etc.) {
....
}
}
buttonpressed &= ~e.Button;
if(buttonpressed == None)
doneAction = false;
}
我个人会使用MouseUp
和MouseDown
事件来更清晰地处理它并避免互操作。基本上,这段代码使用静态类来保存两个按钮的状态,并通过检查您是否可以确定两个按钮实际上是否已关闭。
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public static class MouseButtonStatus
{
static bool RightButton;
static bool LeftButton;
public static bool RightButtonPressed
{
get
{
return RightButton;
}
set
{
RightButton = value;
}
}
public static bool LeftButtonPressed
{
get
{
return LeftButton;
}
set
{
LeftButton = value;
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void HandleButtons(bool LeftPressed, bool RightPressed)
{
if(LeftPressed && RightPressed)
{
//BOTH ARE PRESSED
}
else if(LeftPressed)
{
//LEFT IS PRESSED
}
else if(RightPressed)
{
//RIGHT IS PRESSED
}
else
{
//NONE ARE PRESSED
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
MouseButtonStatus.LeftButtonPressed = true;
}
if(e.Button == MouseButtons.Right)
{
MouseButtonStatus.RightButtonPressed = true;
}
HandleButtons(MouseButtonStatus.LeftButtonPressed, MouseButtonStatus.RightButtonPressed);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
MouseButtonStatus.LeftButtonPressed = false;
}
if(e.Button == MouseButtons.Right)
{
MouseButtonStatus.RightButtonPressed = false;
}
HandleButtons(MouseButtonStatus.LeftButtonPressed, MouseButtonStatus.RightButtonPressed);
}
}
}
不确定是否有原生.Net方式来做,但如果你对P / Invoke感到满意,你可以像这样使用GetKeyState或GetAsyncKeyState:
[DllImport("user32.dll")]
public static extern short GetKeyState(int nVirtKey);
if (GetKeyState((int)Keys.LButton) < 0 && GetKeyState((int)Keys.RButton) < 0)
{
// Both buttons are pressed.
}
不是“中间”和“左右在一起”一样吗?至少那是我从某个地方记得的东西,但当我有两个没有滚轮按钮的按钮鼠标时,这就是回来了......
根据我从其他答案中学到的东西,我能够使这个工作。我在这里发布解决方案以防其他人需要它。
我创建了一个组件MouseDownManager,我在MouseDown事件期间调用它。它跟踪刚按下的按钮,确定我们正在等待的按钮,以便有“两个”按钮按下事件,然后启动计时器等待该按钮。如果在指定的时间内按下了正确的按钮,MouseDownManager会引发相应的“两个”按钮事件。否则它会引发相应的单个按钮事件。在表单上,我正在处理MouseDownManager的MouseDown事件以对翻译的鼠标单击执行操作。
我现在可以将这个组件放在表单/控件上,然后我就可以对“两个”点击做出反应。
感谢您帮助解决这个问题。
/// <summary>
/// Manage mouse down event to enable using both buttons at once.
/// I.e. allowing the pressing of the right and left mouse
/// buttons together.
/// </summary>
public partial class MouseDownManager : Component
{
/// <summary>
/// Raised when a mouse down event is triggered by this
/// component.
/// </summary>
public event EventHandler<MouseEventArgs> MouseDown;
protected virtual void OnMouseDown( MouseEventArgs e )
{
if (this.MouseDown != null)
{
this.MouseDown( this, e );
}
}
public MouseDownManager()
{
//-- the timer was dropped on the designer, so it
// is initialized by InitializeComponent.
InitializeComponent();
}
/// <summary>
/// Defines the interval that the timer will wait for the other
/// click, in milliseconds.
/// </summary>
public int BothClickInterval
{
get
{
return this.tmrBothInterval.Interval;
}
set
{
this.tmrBothInterval.Interval = value;
}
}
private MouseButtons _virtualButton = MouseButtons.Middle;
/// <summary>
/// Defines the button that is sent when both buttons are
/// pressed. This can be either a single button (like a middle
/// button) or more than one button (like both the left and
/// right buttons.
/// </summary>
public MouseButtons VirtualButton
{
get
{
return _virtualButton;
}
set
{
_virtualButton = value;
}
}
private MouseEventArgs _originalArgs;
/// <summary>
/// Defines the original mouse event arguments that is associated
/// with the original press.
/// </summary>
private MouseEventArgs OriginalArgs
{
get
{
return _originalArgs;
}
set
{
_originalArgs = value;
}
}
private MouseButtons _waitButton = MouseButtons.None;
/// <summary>
/// Defines the button that we are waiting on, for there to be a
/// both button click.
/// </summary>
private MouseButtons WaitButton
{
get
{
return _waitButton;
}
set
{
_waitButton = value;
}
}
/// <summary>
/// Manage a mouse button being depressed.
/// </summary>
/// <remarks>
/// This will instigate one of two actions. If there is no
/// current wait button, this will set the appropriate wait
/// button (if the button pressed was the left button, then we
/// are waiting on the right button) and start a timer. If the
/// wait button is set, but this isn't that button, then the wait
/// button is updated and the timer restarted. Also, this will
/// trigger the waiting event. If it is the wait button, then
/// the appropriate event is raised for a "both" button press.
/// </remarks>
public void ManageMouseDown( MouseEventArgs mouseArgs )
{
//-- Is the the button we are waiting for?
if (mouseArgs.Button == this.WaitButton)
{
//-- Turn off timer.
this.ClearTimer();
//-- Create new mouse event args for our virtual event.
MouseEventArgs bothArgs = new MouseEventArgs( this.VirtualButton
, mouseArgs.Clicks
, mouseArgs.X
, mouseArgs.Y
, mouseArgs.Delta );
//-- Raise the mouse down event.
this.OnMouseDown( bothArgs );
}
else
{
//-- Clear timer
this.ClearTimer();
//-- If we were waiting for a button, then
// fire the event for the original button.
if (this.WaitButton != MouseButtons.None)
{
this.OnMouseDown( this.OriginalArgs );
}
//-- Cache the original mouse event args.
MouseEventArgs newMouseArgs = new MouseEventArgs( mouseArgs.Button
, mouseArgs.Clicks
, mouseArgs.X
, mouseArgs.Y
, mouseArgs.Delta );
this.OriginalArgs = newMouseArgs;
//-- Reset to wait for the appropriate next button.
switch (mouseArgs.Button)
{
case MouseButtons.Left:
this.WaitButton = MouseButtons.Right;
break;
case MouseButtons.Right:
this.WaitButton = MouseButtons.Left;
break;
default:
this.WaitButton = MouseButtons.None;
break;
}
//-- Start timer
this.tmrBothInterval.Enabled = true;
}
}
/// <summary>
/// Raise the event for the button that was pressed initially
/// and turn off the timer.
/// </summary>
private void tmrBothInterval_Tick( object sender, EventArgs e )
{
//-- Turn off the timer.
this.tmrBothInterval.Enabled = false;
//-- Based on the original button pressed, raise
// the appropriate mouse down event.
this.OnMouseDown( this.OriginalArgs );
//-- Clear timer.
this.ClearTimer();
}
/// <summary>
/// Clear the timer and associated variables so we aren't waiting
/// for the second click.
/// </summary>
private void ClearTimer()
{
//-- Turn off the timer.
this.tmrBothInterval.Enabled = false;
//-- Clear the wait button.
this.WaitButton = MouseButtons.None;
//-- Clear the original args
this.OriginalArgs = null;
}
}
/// <summary>
/// Just the mouse code from the control needing the functionality.
/// </summary>
public class MyControl: Control
{
/// <summary>
/// Handle the mouse down event. This delegates the actual event
/// to the MouseDownManager, so we can capture the situation
/// where both buttons are clicked.
/// </summary>
protected override void OnMouseDown( MouseEventArgs e )
{
this.mdmMain.ManageMouseDown( e );
}
/// <summary>
/// Process the mouse down event.
/// </summary>
private void mdmMain_MouseDown( object sender, MouseEventArgs e )
{
//-- Get the reported button state.
MouseButtons mouseButton = e.Button;
//-- Execute logic based on button pressed, which now can include
// both the left and right together if needed....
}
}