Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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;


            }
        }
    }
}
Posted
Updated 1-Jan-14 10:52am
v2

1 solution

You are going to have to use P/Invoke to set the input from the mouse.

One way is: P/Invoke User32 SendInput[^] and P/Invoke User32 INPUT Structure[^]. The first link has an example on how to use it.
 
Share this answer
 
Comments
Member 10497973 1-Jan-14 17:00pm    
thank you ron .. but i need to insert a function that click my mouse in the if loop.some solution ?
Ron Beyer 1-Jan-14 17:02pm    
Yes, the links I gave have functions you can call that simulate a mouse, please take some time to read through them, you need to implement the P/Invoke method and pass in an INPUT structure with the clicks. There isn't something as simple as "ClickMouse(LeftButton)", its a little more complicated.
Member 10497973 1-Jan-14 17:04pm    
i didnt understand where should i write in my code ..
i am not so good in c# and i have to present my project tomorrow :S

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900