- Implementing double click event using timer
- #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
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- this.MouseHover += new EventHandler(MouseHoverEvent);
- this.MouseLeave +=new EventHandler(MouseLeaveEvent);
- timer1.Tick += new EventHandler(timer1_Tick);
- foreach (Control item in this.Controls)
- {
- item.MouseHover += new EventHandler(MouseHoverEvent);
- item.MouseLeave += new EventHandler(MouseLeaveEvent);
- }
- }
- void timer1_Tick(object sender, EventArgs e)
- {
- timer1.Stop();
- DoubleClickEvent();
- }
- void MouseLeaveEvent(object sender, EventArgs e)
- {
- timer1.Stop();
- }
- void MouseHoverEvent(object sender, EventArgs e)
- {
- timer1.Start();
- }
- }
- void DoubleClickEvent()
- {
- DoClickMouse(0x2); // Left mouse button down
- DoClickMouse(0x4); // Left mouse button up
- DoClickMouse(0x2); // Left mouse button down
- DoClickMouse(0x4); // Left mouse button up
- }