当前位置:   article > 正文

在没有源程序的情况时,如何通过控制鼠标按钮控制电脑exe程序?

在没有源程序的情况时,如何通过控制鼠标按钮控制电脑exe程序?

有时候想控制第三方软件,但是没有源程序,可以控制鼠标键盘自动操作软件达到我们想要的目的

首先建一个功能类包含窗口控制,鼠标控制和输入控制等


```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace 进程程序名
{
    public class Win32Api
    {

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

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

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        public static IntPtr FindWindowByCaption(string caption)
        {
            return FindWindow(null, caption);
        }
        public static IntPtr FindWindowByClassName(string className)
        {
            return FindWindow(className, null);
        }
    }

    public enum SW
    {
        HIDE = 0,
        SHOW_NORMAL = 1,
        SHOW_MINIMIZED = 2,
        MAXIMIZE = 3,
        SHOW_MAXIMIZED = 3,
        SHOW_NO_ACTIVE = 4,
        SHOW = 5,
        MINIMIZE = 6,
        SHOW_MIN_NO_ACTIVE = 7,
        SHOW_NA = 8,
        RESTORE = 9,
        SHOW_DEFAULT = 10,
        FORCE_MINIMIZE = 11
    }
    public enum WMessages : int
    {
        WM_KEYDOWN = 0x100,
        WM_KEYUP = 0x101,
        WM_CHAR = 0x102,
        WM_LBUTTONDOWN = 0x201, //Left mousebutton down
        WM_LBUTTONUP = 0x202,   //Left mousebutton up
        WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
        WM_RBUTTONDOWN = 0x204, //Right mousebutton down
        WM_RBUTTONUP = 0x205,   //Right mousebutton up
        WM_RBUTTONDBLCLK = 0x206, //Right mousebutton do
        WM_CUT = 0x300,
        WM_COPY = 0x301,
        WM_PASTE = 0x302,
        WM_CLEAR = 0x303
    }


    public enum Functions : int
    {
        KEYEVENTF_KEYDOWN = 0x0000, // New definition
        KEYEVENTF_EXTENDEDKEY = 0x0001, //Key down flag
        KEYEVENTF_KEYUP = 0x0002, //Key up flag
        VK_LCONTROL = 0xA2, //Left Control key code
        A = 0x41, //A key code
        C = 0x43, //C key code
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
再写一个实际应用,根据实际软件大小和位置设置相应的控制点位

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 进程程序名
{
    public class 进程程序名
    {
        public Process 进程程序名
        {
            get => Process.GetProcessesByName("进程程序名").FirstOrDefault(i => i.MainWindowHandle != IntPtr.Zero); 
        }

        public void SendMsg(string msg)
        {
            if(SunnyLink==null)
            {
                Console.WriteLine("请打开SunnyLink窗口,之后重试!");
                return;
            }
            if(string.IsNullOrEmpty(msg))
            {
                Console.WriteLine("发送的字符为空!");
                return;
            }
            var x = 357;
            var y = 540;
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            Clipboard.SetText(msg);
            Task.Delay(1).Wait();
            //复制
            Win32Api.keybd_event((byte)Functions.VK_LCONTROL, 0, (int)Functions.KEYEVENTF_KEYDOWN, 0);
            Task.Delay(100).Wait();
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (int)WMessages.WM_KEYDOWN, (int)System.Windows.Forms.Keys.V, 0);
            Task.Delay(1).Wait();
            Win32Api.keybd_event((byte)Functions.VK_LCONTROL, 0, (int)Functions.KEYEVENTF_KEYUP, 0);
            //发送消息
            Task.Delay(500).Wait();
            x = 920; y = 570;
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            x = 940; y = 610;
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            Task.Delay(100).Wait();
        }

        public void FindUser(string msg)
        {
            if (SunnyLink == null)
            {
                Console.WriteLine("请打开目标程序窗口,之后重试!");
                return;
            }

            var x = 98;
            var y = 26;
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            Clipboard.SetText(msg);
            Task.Delay(100).Wait();
            //复制
            Win32Api.keybd_event((byte)Functions.VK_LCONTROL, 0, (int)Functions.KEYEVENTF_KEYDOWN, 0);
            Task.Delay(100).Wait();
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (int)WMessages.WM_KEYDOWN, (int)System.Windows.Forms.Keys.V, 0);
            Task.Delay(1).Wait();
            Win32Api.keybd_event((byte)Functions.VK_LCONTROL, 0, (int)Functions.KEYEVENTF_KEYUP, 0);
            Task.Delay(2000).Wait();
            //选择用户
            x = 321; y = 159;
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            Task.Delay(100).Wait();
        }

        public void sendMsg(Image image)
        {
            if (SunnyLink == null)
            {
                Console.WriteLine("请打开目标程序窗口,之后重试!");
                return;
            }

            var x = 357;
            var y = 540;
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            Clipboard.SetImage(image); 
            Task.Delay(10).Wait();
            //复制
            Win32Api.keybd_event((byte)Functions.VK_LCONTROL, 0, (int)Functions.KEYEVENTF_KEYDOWN, 0);
            Task.Delay(100).Wait();
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (int)WMessages.WM_KEYDOWN, (int)System.Windows.Forms.Keys.V, 0);
            Task.Delay(1).Wait();
            Win32Api.keybd_event((byte)Functions.VK_LCONTROL, 0, (int)Functions.KEYEVENTF_KEYUP, 0);
            //发送消息
            Task.Delay(100).Wait();
            x = 920; y = 570;
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            x = 940; y = 610;
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, 0, x + (y << 16));
            Win32Api.PostMessage(SunnyLink.MainWindowHandle, (uint)WMessages.WM_LBUTTONUP, 0, x + (y << 16));
            Task.Delay(100).Wait();
        }
    }
}



运行测试
 class Program
 {
     [STAThreadAttribute]
     static void Main(string[] args)
     {
         SunnyLinker sunnyLinker = new SunnyLinker();
         //找到用户
         进程程序名.FindUser("1234567");
         //发送文本
         for (int i = 0; i < 50; i++)
         {
             进程程序名.SendMsg($"[雪花][雪花][雪花]");
             进程程序名.SendMsg($"你好{i}");
         }
         //发送图片
         Image img = Image.FromFile("img.jpeg");
         进程程序名.sendMsg(img);
     }
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/922176
推荐阅读
相关标签
  

闽ICP备14008679号