[C#在屏幕上获取鼠标坐标

问题描述 投票:0回答:1

我找到了正在工作的视频和源代码;单击在表单外部获取坐标。我试图将其与事件关联:通过单击(向右/向左)或按“ F11”键;但这不能正常工作。

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;
            }
        }

    }
}

我还考虑将默认光标图标更改为目标,并且将其居中,类似于直射射击瞄准器,但我没有发现任何类似的东西,只有我的代码中有。

但是这不能正常工作。我不知道我想念吗?

c# mouseevent coordinates mouse
1个回答
0
投票

我已经使用此更新代码解决了它:我已经更改了某些执行的顺序,并更改了具有威胁的任务。

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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.