using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; namespace MouseSerial { public partial class Form1 : Form { SerialPort sp; Point startPoint; int oldX = 0, oldY = 0; const int ep = 30; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { sp = new SerialPort("COM8",9600); sp.Open(); sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); startPoint = new Point(Cursor.Position.X, Cursor.Position.Y); } private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { SerialPort sp1 = (SerialPort)sender; string indata = sp1.ReadLine(); if (indata.StartsWith("||") && indata.EndsWith("||\r")) { indata = indata.TrimStart('|'); indata = indata.TrimEnd('\r'); indata = indata.TrimEnd('|'); string[] d = indata.Split(';'); double x = Convert.ToDouble(d[0]); double y = Convert.ToDouble(d[1]); if (x <= oldX + ep && x >= oldX - ep && y <= oldY + ep && y >= oldY - ep) { oldX = (int)x; oldY = (int)y; //x = ((0.1428 * x) - 59.976) * (-1); //y = (0.1428 * y) - 53.976; x = ((0.2857 * x) - 119.994) * (-1); y = ((0.2857 * y) - 110.994) ; startPoint.X = (int)x + startPoint.X; startPoint.Y = (int)y + startPoint.Y; Cursor.Position = new Point(startPoint.X, startPoint.Y); BeginInvoke((MethodInvoker)delegate { listBox1.Items.Add(x + " ; " + y); }); } else { if (y >= oldX + ep) { BeginInvoke((MethodInvoker)delegate { label1.Text = "Left Click"; }); } else if (y <= oldX - ep) { BeginInvoke((MethodInvoker)delegate { label1.Text = "Right Click"; }); } } if (oldX == 0) oldX = (int)x; if (oldY == 0) oldY = (int)y; } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)