LordPankake

Clicker

May 27th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Diagnostics;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using System.Runtime.InteropServices;
  11. using System.Xml.Serialization;
  12. using System.IO;
  13. using System.Xml;
  14. using System.Collections;
  15.  
  16. namespace AutoClicker
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  21.         public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
  22.  
  23.         [DllImport("user32")]
  24.         public static extern int SetCursorPos(int x, int y);
  25.  
  26.         #region Fields
  27.         private const int MOUSEEVENTF_MOVE = 0x0001; /* mouse move */
  28.         private const int MOUSEEVENTF_LEFTDOWN = 0x0002; /* left button down */
  29.         private const int MOUSEEVENTF_LEFTUP = 0x0004; /* left button up */
  30.         private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; /* right button down */
  31.         private const int MOUSEEVENTF_RIGHTUP = 0x0010; /* right button up */
  32.         private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; /* middle button down */
  33.         private const int MOUSEEVENTF_MIDDLEUP = 0x0040; /* middle button up */
  34.         private const int MOUSEEVENTF_XDOWN = 0x0080; /* x button down */
  35.         private const int MOUSEEVENTF_XUP = 0x0100; /* x button down */
  36.         private const int MOUSEEVENTF_WHEEL = 0x0800; /* wheel button rolled */
  37.         private const int MOUSEEVENTF_VIRTUALDESK = 0x4000; /* map to entire virtual desktop */
  38.         private const int MOUSEEVENTF_ABSOLUTE = 0x8000; /* absolute move */
  39.  
  40.        
  41.         #endregion
  42.         public Form1()
  43.         {
  44.             InitializeComponent();
  45.         }
  46.  
  47.         private static bool shouldClick = false;
  48.         public static void toggleClick()
  49.         {
  50.             shouldClick = !shouldClick;
  51.         }
  52.         private void timer1_Tick(object sender, EventArgs e)
  53.         {
  54.             if (shouldClick)
  55.             {
  56.                 int X = Cursor.Position.X;
  57.                 int Y = Cursor.Position.Y;
  58.                 mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
  59.             }
  60.            
  61.         }
  62.  
  63.         private void trackBar1_Scroll(object sender, EventArgs e)
  64.         {
  65.             timer1.Interval = trackBar1.Value;
  66.         }
  67.  
  68.         private void Form1_Load(object sender, EventArgs e)
  69.         {
  70.             InterceptMouse.RunHook();
  71.         }
  72.  
  73.     }
  74.     class InterceptMouse
  75.     {
  76.         private static LowLevelMouseProc _proc = HookCallback;
  77.         private static IntPtr _hookID = IntPtr.Zero;
  78.         public static void RunHook()
  79.         {
  80.             _hookID = SetHook(_proc);
  81.            
  82.         }
  83.  
  84.         public static void Unhook()
  85.         {
  86.             UnhookWindowsHookEx(_hookID);
  87.         }
  88.         private static IntPtr SetHook(LowLevelMouseProc proc)
  89.         {
  90.             using (Process curProcess = Process.GetCurrentProcess())
  91.             using (ProcessModule curModule = curProcess.MainModule)
  92.             {
  93.                 return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
  94.             }
  95.         }
  96.         private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
  97.         private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  98.         {
  99.             if (nCode >= 0 && MouseMessages.WM_RBUTTONDOWN == (MouseMessages)wParam)
  100.             {
  101.                 Form1.toggleClick();
  102.             }
  103.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  104.         }
  105.         private const int WH_MOUSE_LL = 14;
  106.         private enum MouseMessages
  107.         {
  108.             WM_LBUTTONDOWN = 0x0201, WM_LBUTTONUP = 0x0202, WM_MOUSEMOVE = 0x0200, WM_MOUSEWHEEL = 0x020A, WM_RBUTTONDOWN = 0x0204, WM_RBUTTONUP = 0x0205
  109.         }
  110.         [StructLayout(LayoutKind.Sequential)]
  111.         private struct POINT
  112.         {
  113.             public int x;
  114.             public int y;
  115.         }
  116.         [StructLayout(LayoutKind.Sequential)]
  117.         private struct MSLLHOOKSTRUCT
  118.         {
  119.             public POINT pt;
  120.             public uint mouseData;
  121.             public uint flags;
  122.             public uint time;
  123.             public IntPtr dwExtraInfo;
  124.         }
  125.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  126.         private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
  127.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  128.         [
  129.             return: MarshalAs(UnmanagedType.Bool)
  130.         ]
  131.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  132.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  133.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  134.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  135.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment