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;
using System.Runtime.InteropServices;
using System.Threading;

namespace 模擬鼠標啊啊
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
         [DllImport("user32.dll")]
        static extern bool SetCursorPos(int x, int y);
        [DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
        [Flags]
        enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }
       
    private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
          
           ;         
           Point screenPoint = Control.MousePosition;//獲取光標相對屏幕的距離
                        textBox1.Text = screenPoint.X.ToString();//獲得鼠標x軸
                        textBox2.Text = screenPoint.Y.ToString();//獲得鼠標y軸

        }

        public void xxx(int x,int y) {  SetCursorPos(y, x);
            mouse_event(MouseEventFlag.LeftDown, 10, 10, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, 10, 10, 0, UIntPtr.Zero);
        }


        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            int x =Convert.ToInt32( textBox2.Text);
            int y = Convert.ToInt32(textBox1.Text);
          
            this.WindowState = FormWindowState.Minimized;
            xxx(x, y);
        }
      
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章