/* * class ClickThreadHelper * { #region Fields, DLL Imports and Constants * * public List<string> Names { get; set; } //Hold the list of points in the queue * public List<Point> Points { get; set; } //Hold the list of points in the queue * public int Iterations { get; set; } //Hold the number of iterations/repeats * public List<string> ClickType { get; set; } //Is each point right click or left click * public List<int> Times { get; set; } //Holds sleep times for after each click * * //Import unmanaged functions from DLL library * [DllImport("user32.dll")] * public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo); * * [DllImport("user32.dll", SetLastError = true)] * public static extern int SendInput(int nInputs, ref INPUT pInputs, int cbSize); * * /// <summary> * /// Structure for SendInput function holding relevant mouse coordinates and information * /// </summary> * public struct INPUT * { * public uint type; * public MOUSEINPUT mi; * * }; * * /// <summary> * /// Structure for SendInput function holding coordinates of the click and other information * /// </summary> * public struct MOUSEINPUT * { * public int dx; * public int dy; * public int mouseData; * public int dwFlags; * public int time; * public IntPtr dwExtraInfo; * }; * * //Constants for use in SendInput and mouse_event * public const int INPUT_MOUSE = 0x0000; * public const int MOUSEEVENTF_LEFTDOWN = 0x0002; * public const int MOUSEEVENTF_LEFTUP = 0x0004; * public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; * public const int MOUSEEVENTF_RIGHTUP = 0x0010; * public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; * public const int MOUSEEVENTF_MIDDLEUP = 0x0040; * #endregion * #region Mouse_Event Methods * * /// <summary> * /// Click the left mouse button at the current cursor position using * /// the imported mouse_event function * /// </summary> * private void ClickLeftMouseButtonMouseEvent() * { * //Send a left click down followed by a left click up to simulate a * //full left click * mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); * mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); * } * * /// <summary> * /// Click the right mouse button at the current cursor position using * /// the imported mouse_event function * /// </summary> * private void ClickRightMouseButtonMouseEvent() * { * //Send a left click down followed by a right click up to simulate a * //full right click * mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); * mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); * } * #endregion * #region SendInput Methods * * /// <summary> * /// Click the left mouse button at the current cursor position using * /// the imported SendInput function * /// </summary> * public void ClickLeftMouseButtonSendInput() * { * //Initialise INPUT object with corresponding values for a left click * INPUT input = new INPUT(); * input.type = INPUT_MOUSE; * input.mi.dx = 0; * input.mi.dy = 0; * input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; * input.mi.dwExtraInfo = IntPtr.Zero; * input.mi.mouseData = 0; * input.mi.time = 0; * * //Send a left click down followed by a left click up to simulate a * //full left click * SendInput(1, ref input, Marshal.SizeOf(input)); * input.mi.dwFlags = MOUSEEVENTF_LEFTUP; * SendInput(1, ref input, Marshal.SizeOf(input)); * * } * * /// <summary> * /// Click the left mouse button at the current cursor position using * /// the imported SendInput function * /// </summary> * public void ClickRightMouseButtonSendInput() * { * //Initialise INPUT object with corresponding values for a right click * INPUT input = new INPUT(); * input.type = INPUT_MOUSE; * input.mi.dx = 0; * input.mi.dy = 0; * input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN; * input.mi.dwExtraInfo = IntPtr.Zero; * input.mi.mouseData = 0; * input.mi.time = 0; * * //Send a right click down followed by a right click up to simulate a * //full right click * SendInput(1, ref input, Marshal.SizeOf(input)); * input.mi.dwFlags = MOUSEEVENTF_RIGHTUP; * SendInput(1, ref input, Marshal.SizeOf(input)); * } * #endregion * #region Methods * * /// <summary> * /// Iterate through all queued clicks, for each deciding which mouse button * /// to press and how long to sleep afterwards * /// * /// This method is assigned to the ClickThread and is the only place where * /// the mouse buttons are pressed * /// </summary> * public void Run() * { * try * { * int i = 1; * * while (i <= Iterations) * { * //Iterate through all queued clicks * for (int j = 0; j <= Points.Count - 1; j++) * { * * * * SetCursorPosition(Points[j]); //Set cursor position before clicking * if (ClickType[j].Equals("R")) * { * ClickRightMouseButtonSendInput(); * } * else * { * ClickLeftMouseButtonSendInput(); * } * Thread.Sleep(Times[j]); * } * i++; * } * } * catch (Exception exc) * { * MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); * } * } * * /// <summary> * /// Set the current position of the cursor to the coordinates held in point * /// </summary> * /// <param name="point">Coordinates to set the cursor to</param> * private void SetCursorPosition(Point point) * { * Cursor.Position = point; * } * #endregion * } */ #endregion #region Timers // Run it with timer private void Startuo(string str) { if (IsValidNumericalInput(NumRepeatsTextBox.Text)) { int iterations = Convert.ToInt32(NumRepeatsTextBox.Text); List <Point> points = new List <Point>(); List <string> clickType = new List <string>(); List <string> names = new List <string>(); List <int> times = new List <int>(); // PositionsListView.Invoke((EventHandler)delegate { test= PositionsListView; }); for (int i = 0; i < this.PositionsListView.Items.Count; i++) { PositionsListView.Invoke(new MethodInvoker(delegate() { if (str == PositionsListView.Items[i].Text) { int x = Convert.ToInt32(PositionsListView.Items[i].SubItems[1].Text); //x coordinate int y = Convert.ToInt32(PositionsListView.Items[i].SubItems[2].Text); //y coordinate clickType.Add(PositionsListView.Items[i].SubItems[3].Text); //click type times.Add(Convert.ToInt32(PositionsListView.Items[i].SubItems[4].Text)); //sleep time names.Add(PositionsListView.Items[i].Text); //sleep time points.Add(new Point(x, y)); } })); } try { //Create a ClickHelper passing Lists of click information Names = names; Points = points; ClickType = clickType; Iterations = iterations; Times = times; // ClickThreadHelper helper = new ClickThreadHelper() {Names = names, Points = points, ClickType = clickType, Iterations = iterations, Times = times }; //Create the thread passing the Run method ClickThread = new Thread(new ThreadStart(Run)); //Start the thread, thus starting the clicks ClickThread.Start(); } catch (Exception exc) { MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Number of repeats is not a valid positive integer", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } listBox2.Invoke(new MethodInvoker(delegate() { listBox2.Items.Add(str + "runned @ " + DateTime.Now.ToString()); })); }
private void PositionsListView_MouseUp(object sender, MouseEventArgs e) { if (e.Button.ToString() != "Left") { return; } ListViewHitTestInfo i = PositionsListView.HitTest(e.X, e.Y); SelectedLSI = i.SubItem; if (SelectedLSI == null) { return; } int border = 0; switch (PositionsListView.BorderStyle) { case BorderStyle.FixedSingle: border = 1; break; case BorderStyle.Fixed3D: border = 2; break; } int CellWidth = SelectedLSI.Bounds.Width; int CellHeight = SelectedLSI.Bounds.Height; int CellLeft = border + PositionsListView.Left + i.SubItem.Bounds.Left; int CellTop = PositionsListView.Top + i.SubItem.Bounds.Top; // First Column if (i.SubItem == i.Item.SubItems[0]) { CellWidth = PositionsListView.Columns[0].Width; } TxtEdit.Location = new Point(CellLeft, CellTop); TxtEdit.Size = new Size(CellWidth, CellHeight); TxtEdit.Visible = true; TxtEdit.BringToFront(); TxtEdit.Text = i.SubItem.Text; TxtEdit.Select(); TxtEdit.SelectAll(); }