0

I found a video and a source code on which I am working; to get the coordinates by clicking outside the form. I have tried to tie it to events: By clicking (right / left) or pressing the "F11" key; But it doesn't work properly.

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;

namespace iMouse
{
    public partial class Events : Form
    {
        public bool TrackerSt;
        public FormMouseCR()
        {
            InitializeComponent();
        }


        public void ButtonGetCoord_Click(object sender, EventArgs e)
        {
            Visible = false;
            TrackerSt = true;
            int x = Cursor.Position.X;
            int y = Cursor.Position.Y;
            int size = 10; // Arbitrary size

            System.Drawing.Graphics graphics = CreateGraphics();
            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(x - (size / 2), y - (size / 2), size, size);
            graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);

        }

        private void FormMouseCR_MouseClick(object sender, MouseEventArgs e)
        {
            if(TrackerSt){
                label1.Text = "X = " + e.X + " ; Y = " + e.Y;
                TrackerSt = false;
            }
        }

        private void FormMouseCR_MouseMove(object sender, MouseEventArgs e)
        {
            if(TrackerSt){
                label1.Text = "X = " + e.X + " ; Y = " + e.Y;
            }
        }

    }
}

I also think about changing the default cursor icon with that of a target and that it is centered, similar to a straight shooter sight, but I haven't found anything similar, only that i have in my code.

but this not work properly. iam dont know that i am missing?

  • Have you tried simplifying your code, like not utilizing `TrackerSt`? – Sunny Patel Jan 10 '20 at 17:36
  • @SunnyPatel Not really; I am supposed to want the value to be captured only once when the user clicks on the button that starts the capture of the coordinates. –  Jan 10 '20 at 17:40
  • My point is to start simple and get your base case working before you start making it more complicated by only capturing at certain times. – Sunny Patel Jan 10 '20 at 17:43
  • @SunnyPatel Can you try an example and discuss it? –  Jan 10 '20 at 17:45
  • Check [this](https://stackoverflow.com/questions/1316681/getting-mouse-position-in-c-sharp) post, the `GetCursorPos` answers. –  Jan 10 '20 at 18:46

1 Answers1

0

i have solved it with this update code: i have change the order of some execution and change task with threat.

using System;
using System.Threading;
using System.Windows.Forms;

namespace iMouse
{
    public partial class Events : Form
    {
        public Thread TrackerThread;
        public Mutex Checking = new Mutex(false);
        public AutoResetEvent Are = new AutoResetEvent(false);
        public Events()
        {
            InitializeComponent();
        }
        private void Events_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
        }
        public void Button3_Click(object sender, EventArgs e)
        {
            Visible = false;
            MouseTracker();
        }
        private void MouseTracker()
        {
            if (Checking.WaitOne(10))
            {
                var ctx = new SynchronizationContext();
                Are.Reset();
                TrackerThread = new Thread(() =>{
                       while (true)
                       {
                           if (Are.WaitOne(1))
                           {
                               break;
                           }
                           if (MouseButtons == MouseButtons.Left)
                           {
                               ctx.Send(CLickFromOutside, null);
                               break;
                           }
                       }
                   }
                );
                TrackerThread.Start();
                Checking.ReleaseMutex();
            }
        }

        private void CLickFromOutside(object state)
        {
            Are.Set();
            int X = MousePosition.X;
            int Y = MousePosition.Y;
            TextBox2.Text = X.ToString();
            TextBox3.Text = Y.ToString();
            Visible = true;
        }
    }
}