C#鼠标模拟操作

		static void Drag(int startX, int startY, int endX, int endY)
		{
			int screenWidth = Screen.PrimaryScreen.Bounds.Width;
			int screenHeight = Screen.PrimaryScreen.Bounds.Height;
			var sim = new InputSimulator();
			sim.Mouse.MoveMouseToPositionOnVirtualDesktop(Convert.ToDouble(startX * 65535 / screenWidth), Convert.ToDouble(startY * 65535 / screenHeight));
			sim.Mouse.LeftButtonDown();
			Thread.Sleep(1000);
			sim.Mouse.MoveMouseTo(Convert.ToDouble(endX * 65535 / screenWidth), Convert.ToDouble(endY * 65535 / screenHeight));
			Thread.Sleep(1000);
			sim.Mouse.LeftButtonUp();
			
		}
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace 鼠标操作
{
    class Program
    {

        static void Main(string[] args)
        {
            //硬件鼠标操作
            //MouseHelp.POINT pOINT = new MouseHelp.POINT();
            //MouseHelp.GetCursorPos(out pOINT);
            //MouseHelp.mouse_event(MouseHelp.MOUSEEVENTF_LEFTDOWN, pOINT.X, pOINT.Y, 0, 0);
            // MouseHelp.mouse_event(MouseHelp.MOUSEEVENTF_LEFTUP, pOINT.X, pOINT.Y, 0, 0);
            //鼠标软模拟操作 https://stackoverflow.com/questions/3443325/sendmessage-wm-lbuttondown-up-works-on-buttons-but-not-window
            //https://blog.csdn.net/Ink_cherry/article/details/70210746
            // MouseHelp.SendMessage(Control.hWnd, MouseHelp.WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
            Console.ReadKey();
        }
    }

    public class MouseHelp
    {
        /**
            {鼠标软模拟:好处就是不会真的移动鼠标  开始按钮 坐标 x=386y=387 }  
            sendmessage(hookHwnd,messages.WM_LBUTTONDOWN ,0,$0180017A); {按下鼠标左键}  
            sendmessage(hookHwnd,messages.WM_LBUTTONUP  ,0, $0180017A); {抬起鼠标左键}  
            {硬件模拟:会真的移动鼠标}  
            mouse_event(MOUSEEVENTF_LEFTDOWN,X ,Y ,0,0);  
            mouse_event(MOUSEEVENTF_LEFTUP,X ,Y ,0,0);  
         * **/
        #region 硬件模拟鼠标键值 https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mouse_event
        /// <summary>
        /// dx和dy参数包含标准化的绝对坐标。 如果未设置,则这些参数包含相对数据:自上次报告的位置以来的位置变化。 无论将哪种类型的鼠标或类似鼠标的设备连接到系统,都可以设置或不设置此标志。 有关鼠标相对运动的更多信息,请参见下面的“备注”部分。
        /// </summary>
        public const int MOUSEEVENTF_ABSOLUTE = 0x8000;
        /// <summary>
        /// 左按钮按下。
        /// </summary>
        public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        /// <summary>
        /// 左按钮向上。
        /// </summary>
        public const int MOUSEEVENTF_LEFTUP = 0x0004;
        /// <summary>
        /// 中间按钮按下。
        /// </summary>
        public const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
        /// <summary>
        /// 中间按钮向上。
        /// </summary>
        public const int MOUSEEVENTF_MIDDLEUP = 0x40;
        /// <summary>
        /// 鼠标移动
        /// </summary>
        public const int MOUSEEVENTF_MOVE = 0x1;
        /// <summary>
        /// 右按钮按下。
        /// </summary>
        public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        /// <summary>
        /// 右侧按钮向上。
        /// </summary>
        public const int MOUSEEVENTF_RIGHTUP = 0x0010;
        /// <summary>
        /// 如果鼠标带有滚轮,则滚轮已移动。 移动量在dwData中指定    //滚轮按钮被旋转。
        /// </summary>
        public const int MOUSEEVENTF_WHEEL = 0x800;
        /// <summary>
        /// 按下了X按钮。
        /// </summary>
        public const int MOUSEEVENTF_XDOWN = 0x0080;
        /// <summary>
        /// X按钮被释放。
        /// </summary>
        public const int MOUSEEVENTF_XUP = 0x0100;
        /// <summary>
        /// 滚轮按钮倾斜。
        /// </summary>
        public const int MOUSEEVENTF_HWHEEL = 0x01000;
        #endregion 硬件模拟鼠标键值

        #region 软鼠标键值 https://docs.microsoft.com/zh-CN/windows/win32/inputdev/mouse-input
        /// <summary>
        /// 鼠标移动
        /// </summary>
        public const int WM_MOUSEMOVE = 0x0200;
        /// <summary>
        /// 双击鼠标左键
        /// </summary>
        public const int WM_LBUTTONDBLCLK = 0x0203;
        /// <summary>
        /// 按下鼠标左键
        /// </summary>
        public const int WM_LBUTTONDOWN = 0x0201;
        /// <summary>
        /// 释放鼠标左键
        /// </summary>
        public const int WM_LBUTTONUP = 0x0202;
        /// <summary>
        /// 双击鼠标中键
        /// </summary>
        public const int WM_MBUTTONDBLCLK = 0x0209;
        /// <summary>
        /// 按下鼠标中键
        /// </summary>
        public const int WM_MBUTTONDOWN = 0x0207;
        /// <summary>
        /// 释放鼠标中键
        /// </summary>
        public const int WM_MBUTTONUP = 0x0208;
        /// <summary>
        /// 双击鼠标右键
        /// </summary>
        public const int WM_RBUTTONDBLCLK = 0x0206;
        /// <summary>
        /// 按下鼠标右键
        /// </summary>
        public const int WM_RBUTTONDOWN = 0x0204;
        /// <summary>
        /// 释放鼠标右键
        /// </summary>
        public const int WM_RBUTTONUP = 0x0205;
        /// <summary>
        /// 当用户在窗口的客户区域中双击第一个或第二个X按钮时,将发布该消息。如果未捕获鼠标,则消息将发布到光标下方的窗口中。否则,该消息将发布到捕获鼠标的窗口中。
        /// </summary>
        public const int WM_XBUTTONDBLCLK = 0x020D;
        /// <summary>
        /// 当光标在窗口的工作区中时用户按下第一个或第二个X按钮时发布。如果未捕获鼠标,则消息将发布到光标下方的窗口中。否则,该消息将发布到捕获鼠标的窗口中。
        /// </summary>
        public const int WM_XBUTTONDOWN = 0x020B;
        /// <summary>
        /// 当光标在窗口的客户区域中时,用户释放第一个X或第二个X按钮时发布。如果未捕获鼠标,则消息将发布到光标下方的窗口中。否则,该消息将发布到捕获鼠标的窗口中。
        /// </summary>
        public const int WM_XBUTTONUP = 0x020C;
        /// <summary>
        /// 旋转鼠标滚轮时发送到焦点窗口。该DefWindowProc函数功能将消息传播到窗口的父。不应在内部转发消息,因为DefWindowProc会将消息沿父链传播,直到找到处理该消息的窗口为止。
        /// </summary>
        public const int WM_MOUSEWHEEL = 0x020A;

        #endregion 软鼠标键值
        #region 其他数据操作键值--剪切板、粘贴板  https://docs.microsoft.com/en-us/windows/win32/dataxchg/wm-paste
        public const int WM_COPYDATA = 0x004A;
        public const int WM_PASTE = 0x0302;
        public const int WM_CHAR = 0x102;
        public const int WM_SETTEXT = 0x0C;
        public const int WM_KEYDOWN = 0x0100;
        public const int WM_KEYUP = 0x0101;
        public const int WM_CLOSE = 0x0010;

        public const int VK_END = 0x23;
        public const int VK_BACK = 0x08;
        public const int VK_RETURN = 0x0D;
        public const int VK_Delete = 0x2E;
        #endregion 其他数据操作键值

        public const int SM_CXSCREEN = 0;
        public const int SM_CYSCREEN = 1;

        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
        public static extern int SetCursorPos(int x, int y);

        [DllImport("user32.dll", EntryPoint = "mouse_event")]
        public static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo);

        [DllImport("user32.dll", EntryPoint = "keybd_event")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        [DllImport("user32.dll", EntryPoint = "SendMessageW")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string strclassName, string strWindowText);

        [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
        public static extern long GetPrivateProfileString(string strSection, string strKey, string strDef, StringBuilder sbBuffer, int iSize, string strFilePath);
        /// <summary>
        /// 获取屏幕分辩率
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        static extern int GetSystemMetrics(int nIndex);
        /// <summary>
        /// 获取鼠标当前位置
        /// </summary>
        /// <param name="lpPoint"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out POINT lpPoint);

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;
            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //int x = this.Location.X+18;
            //int y = this.Location.Y+18;
            //int at = MouseHelp.SendMessage(this.Handle, MouseHelp.WM_LBUTTONDOWN, IntPtr.Zero, new IntPtr(MAKELPARAM(x, y)));
            //for (int i = 0; i < 1000; i++)
            //{
            //    x++; y++;
            //    int t = MouseHelp.SendMessage(this.Handle, MouseHelp.WM_MOUSEMOVE, IntPtr.Zero, new IntPtr(MAKELPARAM(x, y)));
            //}
            //at = MouseHelp.SendMessage(this.Handle, MouseHelp.WM_LBUTTONUP, IntPtr.Zero, new IntPtr(MAKELPARAM(x, y)));

            var notepad = Process.GetProcessesByName("Notepad2_x64").FirstOrDefault();
            if (notepad != null)
            {
                //var edit = FindWindowEx(notepad.MainWindowHandle, IntPtr.Zero, "Edit", null);
                //PerformRightClick(edit, new Point(20, 20));
                PerformRightClick(notepad.MainWindowHandle, new Point(20, 20));
            }

            IntPtr maindHwnd = FindWindow(null, "QQ"); //获得QQ登陆框的句柄   
            if (maindHwnd != IntPtr.Zero)
            {
                IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null, "QQ");   //获得按钮的句柄
                PerformRightClick(maindHwnd, new Point(20, 20));     //移动
            }
            else
            {
                MessageBox.Show("没有找到窗口");
            }

        }
        private int MakeLParam(int p, int p_2)
        {
            return ((p_2 << 16) | (p & 0xFFFF));
        }

        [DllImport("user32.dll")]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,string lpszClass, string lpszWindow);
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        void PerformRightClick(IntPtr hwnd, Point point)
        {
            var pointPtr = MakeLParam(point.X, point.Y);
            int a = MouseHelp.SendMessage(hwnd, MouseHelp.WM_MOUSEMOVE, IntPtr.Zero, new IntPtr( pointPtr));
            a = MouseHelp.SendMessage(hwnd, MouseHelp.WM_RBUTTONDOWN, IntPtr.Zero, new IntPtr(pointPtr));
            a = MouseHelp.SendMessage(hwnd, MouseHelp.WM_RBUTTONUP, IntPtr.Zero, new IntPtr(pointPtr));
        }

        
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C#提供了一个`System.Windows.Forms`命名空间,其中包含了许多与用户界面相关的类及方法,可以用来模拟键盘鼠标操作。 下面是一些常用的模拟键盘鼠标操作的方法: 1. 模拟按键操作 可以使用`SendKeys`类中的`Send`方法来模拟按键操作。例如,要模拟按下“a”键,可以使用以下代码: ``` SendKeys.Send("a"); ``` 2. 模拟组合键操作 如果要模拟组合键操作,例如Ctrl+C复制操作,可以将组合键的按键值用“+”连接起来,如下所示: ``` SendKeys.Send("^c"); ``` 其中,“^”表示Ctrl键,“+”表示Shift键,“%”表示Alt键,“{F1}”表示F1键,等等。 3. 模拟鼠标点击操作 可以使用`System.Windows.Forms.Cursor`类中的`Position`属性来获取当前鼠标的位置,然后使用`System.Windows.Forms.Mouse`类中的`LeftClick`或`RightClick`方法来模拟鼠标左键或右键点击操作。例如,要模拟在屏幕上(100,100)的位置进行左键点击操作,可以使用以下代码: ``` Cursor.Position = new Point(100, 100); Mouse.LeftClick(); ``` 4. 模拟鼠标移动操作 可以使用`System.Windows.Forms.Cursor`类中的`Position`属性来设置鼠标的位置,从而模拟鼠标移动操作。例如,要将鼠标移到屏幕上(200,200)的位置,可以使用以下代码: ``` Cursor.Position = new Point(200, 200); ``` 需要注意的是,模拟键盘鼠标操作可能会对系统产生影响,因此在使用时要谨慎,并且尽量避免在用户不知情的情况下进行操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值