Con questo controlli il mouse per tutto il sistema operativo, non solo per il programma che crei in c#.
Cosa ottieni come input dal mouse sul serial monitor? Perche' io utilizzando un pad ( 4 frecce per le 4 direzioni) ottenevo delle stringhe tipo RIGHT, LEFT,UP,DOWN ma di cosa esce da un mouse non ne ho proprio idea.
Il click e doppio click mi hanno fatto dannare ma alla fine ho risolto così:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Linq;
namespace SimpleSerial
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, int dwData, IntPtr dwExtraInfo);
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
private const UInt32 MOUSEEVENTF_RIGHTDOWN = 0x00000008;
private const UInt32 MOUSEEVENTF_RIGHTUP = 0x00000010;
private const UInt32 MIDDLEDOWN = 0x00000020;
private const UInt32 MIDDLEUP = 0x00000040;
private const UInt32 MOUSEEVENTF_WHEEL = 0x0800;
internal static void SendDoubleClickL()
{
SendClickL();
SendClickL();
}
internal static void SendClickL()
{
SendDownL();
SendUpL();
}
internal static void SendClickR()
{
SendDownR();
SendUpR();
}
internal static void WheelUp()
{
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, +7, new System.IntPtr());
}
internal static void WheelDown()
{
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -7, new System.IntPtr());
}
internal static void SendUpL()
{
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}
internal static void SendDownL()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
}
internal static void SendDownR()
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, new System.IntPtr());
}
internal static void SendUpR()
{
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, new System.IntPtr());
}
}
}
if (tua condizione si avvera)
{
SendClickL();
}
if (condizione si avvera)
{
SendClickR();
}
if (condizione si avvera)
{
WheelUp();
laststring = "R1";
}
if (condizione si avvera)
{
WheelDown();
laststring = "R2";
}
if (condizione si avvera)
{
Special();
}
if (condizione si avvera)
{
SendDoubleClickL();
laststring = "Square";
}
}
Qui ho incluso anche Rotella in su, Rotella in Giu, Click destro, CLick sinistro e Doppio Click sinistro.
Spero ti sia utile, Saluti 