Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.62 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Implementing double click event using timer
  2. #region Timer Mouse Double Click event
  3.  
  4.     timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
  5.  
  6.     //Here, the timer for Timer click event will start when mouse hovers over an area
  7.     private void form_MouseHover(object sender, System.EventArgs e)
  8.     {
  9.         timer.Start();
  10.     }
  11.  
  12.     private void form_MouseLeave(object sender, System.EventArgs e)
  13.     {
  14.         timer.Stop();
  15.     }
  16.  
  17.     void timer_Elapsed(object sender, ElapsedEventArgs e)
  18.     {
  19.         timer.Stop();
  20.         DoubleClickEvent();
  21.     }
  22.  
  23.     //This method allows the user to click a file/folder by hovering/keeping still the mouse for specified time
  24.     void DoubleClickEvent()
  25.     {
  26.  
  27.         DoClickMouse(0x2);      // Left mouse button down
  28.         DoClickMouse(0x4);      // Left mouse button up
  29.     }
  30.  
  31.     static void DoClickMouse(int mouseButton)
  32.     {
  33.         var input = new INPUT()
  34.         {
  35.             dwType = 0, // Mouse input
  36.             mi = new MOUSEINPUT() { dwFlags = mouseButton }
  37.         };
  38.  
  39.         if (SendInput(1, input, Marshal.SizeOf(input)) == 0)
  40.         {
  41.             throw new Exception();
  42.         }
  43.     }
  44.     [StructLayout(LayoutKind.Sequential)]
  45.     struct MOUSEINPUT
  46.     {
  47.         int dx;
  48.         int dy;
  49.         int mouseData;
  50.         public int dwFlags;
  51.         int time;
  52.         IntPtr dwExtraInfo;
  53.     }
  54.     struct INPUT
  55.     {
  56.         public uint dwType;
  57.         public MOUSEINPUT mi;
  58.     }
  59.     [DllImport("user32.dll", SetLastError = true)]
  60.     static extern uint SendInput(uint cInputs, INPUT input, int size);
  61.  
  62.     #endregion
  63.        
  64. public partial class Form1 : Form
  65. {
  66.     public Form1()
  67.     {
  68.         InitializeComponent();
  69.     }
  70.  
  71.     private void Form1_Load(object sender, EventArgs e)
  72.     {
  73.  
  74.         this.MouseHover += new EventHandler(MouseHoverEvent);
  75.         this.MouseLeave +=new EventHandler(MouseLeaveEvent);
  76.         timer1.Tick += new EventHandler(timer1_Tick);
  77.  
  78.         foreach (Control item in this.Controls)
  79.         {
  80.             item.MouseHover += new EventHandler(MouseHoverEvent);
  81.             item.MouseLeave += new EventHandler(MouseLeaveEvent);
  82.         }
  83.  
  84.     }
  85.  
  86.     void timer1_Tick(object sender, EventArgs e)
  87.     {
  88.         timer1.Stop();
  89.         DoubleClickEvent();
  90.     }
  91.  
  92.     void MouseLeaveEvent(object sender, EventArgs e)
  93.     {
  94.         timer1.Stop();
  95.     }
  96.  
  97.     void MouseHoverEvent(object sender, EventArgs e)
  98.     {
  99.         timer1.Start();
  100.     }
  101. }
  102.        
  103. void DoubleClickEvent()
  104. {
  105.     DoClickMouse(0x2);      // Left mouse button down
  106.     DoClickMouse(0x4);      // Left mouse button up        
  107.     DoClickMouse(0x2);      // Left mouse button down
  108.     DoClickMouse(0x4);      // Left mouse button up
  109. }