【发布时间】:2012-02-06 12:47:51
【问题描述】:
正如我在this 问题中提到的,我正在尝试在我的应用程序中实现一项功能,将光标放在某个点上一段时间(比如 3-5 秒)会触发双击事件。根据该线程中提供的答案,我写了以下内容。此代码未按预期工作。有人可以帮忙吗?
#region Timer Mouse Double Click event
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
//Here, the timer for Timer click event will start when mouse hovers over an area
private void form_MouseHover(object sender, System.EventArgs e)
{
timer.Start();
}
private void form_MouseLeave(object sender, System.EventArgs e)
{
timer.Stop();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
timer.Stop();
DoubleClickEvent();
}
//This method allows the user to click a file/folder by hovering/keeping still the mouse for specified time
void DoubleClickEvent()
{
DoClickMouse(0x2); // Left mouse button down
DoClickMouse(0x4); // Left mouse button up
}
static void DoClickMouse(int mouseButton)
{
var input = new INPUT()
{
dwType = 0, // Mouse input
mi = new MOUSEINPUT() { dwFlags = mouseButton }
};
if (SendInput(1, input, Marshal.SizeOf(input)) == 0)
{
throw new Exception();
}
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
int dx;
int dy;
int mouseData;
public int dwFlags;
int time;
IntPtr dwExtraInfo;
}
struct INPUT
{
public uint dwType;
public MOUSEINPUT mi;
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint cInputs, INPUT input, int size);
#endregion
【问题讨论】:
-
我建议你使用Reactive Extensions 去那个。
-
双击事件根本没有发生!
-
@ykombinator:你为什么不使用
MouseEnter事件? -
@Tigran 将此代码视为一些 RSI 自动点击器应用程序,因此使用了 SendInput API 方法,以便所有正在运行的程序都可以使用该功能。
标签: c# .net winforms mouseevent emgucv