Jak najprościej wykonać Left/Right mouse Click? Program odliczałby określony czas na timerze a następnie klikał prawym bądź lewym przyciskiem myszy w obecnej pozycji kursora.
0
0
- Wiem widziałem, nie działa. W następnym nie widzę akcji odpowiadającej za wciśnięcie myszy.
- Działa. Dzięki!
0
Ok. Program zlicza kliknięcie ale tylko w okienku programu. Nie działa to poza oknem programu ani na elementach takich jak textBox.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace Strawberry_Macro
{
public partial class Form1 : Form
{
//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
ListViewItem lv, lv2;
int a, b, lc, rc, counter = 0;
int lvalue = 0;
int rvalue = 0;
public Form1()
{
InitializeComponent();
}
//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
private void Tick(object sender, EventArgs e)
{
lv = new ListViewItem(Cursor.Position.X.ToString());
lv.SubItems.Add(Cursor.Position.Y.ToString());
listView1.Items.Add(lv);
//lv2 = new ListViewItem(Cursor.Position.X.ToString());
//lv2.SubItems.Add(Cursor.Position.Y.ToString());
//listView2.Items.Add(lv2);
this.MouseClick += mouseClick;
listView2.Items.Add(""+lc);
lc = 0;
rc = 0;
b++;
}
private void Tock(object sender, EventArgs e)
{
if (a != b)
{
Cursor.Position = new Point(int.Parse(listView1.Items[a].SubItems[0].Text), int.Parse(listView1.Items[a].SubItems[1].Text));
if (listView2.Items[a].Text == "1")
{
counter++;
LeftMouseClick(Cursor.Position.X, Cursor.Position.Y);
txtCounter.Text = counter.ToString();
}
a++;
}
}
private void btnRecord_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
timer1.Stop();
timer2.Stop();
}
private void btnPlay_Click(object sender, EventArgs e)
{
timer2.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
a = 0;
b = 0;
lc = 0;
rc = 0;
}
private void mouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
lc = 1;
//lvalue++;
txtCounter.Text = lvalue.ToString();
}
if (e.Button == MouseButtons.Right)
{
rc = 1;
//rvalue++;
txtCounter.Text = rvalue.ToString();
}
}
private void btnLClick_Click(object sender, EventArgs e)
{
for (int i = 0; i < listView2.Items.Count; i++)
{
if (listView2.Items[i].Text == "1")
{
MessageBox.Show("Było");
}
}
//Thread.Sleep(2000);
//LeftMouseClick(Cursor.Position.X, Cursor.Position.Y);
}
}
}
0
Tutaj masz przykład który zlicza globalnie kliknięcia:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
MouseHook.Start();
MouseHook.MouseAction += new EventHandler(Event);
}
private void Event(object sender, EventArgs e) { Console.WriteLine("Left mouse click!"); }
}
public static class MouseHook
{
public static event EventHandler MouseAction = delegate { };
public static void Start()
{
_hookID = SetHook(_proc);
}
public static void stop()
{
UnhookWindowsHookEx(_hookID);
}
private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static IntPtr SetHook(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
MouseAction(null, new EventArgs());
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private const int WH_MOUSE_LL = 14;
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
http://stackoverflow.com/questions/11607133/global-mouse-event-handler
0
Działa. Jeszcze tylko muszę rozpracować prawe kliknięcia.