使用 C# 控制另一个应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1134993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:03:25  来源:igfitidea点击:

Control another application using C#

c#winforms

提问by Salamander2007

I need to control other application by simulating mouse movement and keyboard input. How do I accomplish this in C#? Is it even possible?

我需要通过模拟鼠标移动和键盘输入来控制其他应用程序。我如何在 C# 中实现这一点?甚至有可能吗?

采纳答案by SolutionYogi

Have you looked at WhiteTestStack?

你看过WhiteTestStack吗?

Sample code:

示例代码:

Application application = Application.Launch("foo.exe");
Window window = application.GetWindow("bar", InitializeOption.NoCache);

Button button = window.Get<Button>("save");
button.Click();

I don't think it can get better than that. The library is created by ThoughtWorks.

我认为没有比这更好的了。该库由 ThoughtWorks 创建。

回答by Sam Harwell

See "To send a keystroke to a different application" on this page:

请参阅此页面上的“将击键发送到不同的应用程序”:

http://msdn.microsoft.com/en-us/library/ms171548.aspx

http://msdn.microsoft.com/en-us/library/ms171548.aspx

回答by abhilash

Use the SendMessageNative Win32 API. DllImportthis method from the User32.dll. You can use this API to send both keyboard & mouse messages

使用SendMessage 本机 Win32 API。DllImport这个方法从 User32.dll 中导入。您可以使用此 API 发送键盘和鼠标消息

回答by Yuriy Faktorovich

You can use p/invoke, I stole the following code for mouse clicks in random spots on a button with a known handle:

您可以使用 p/invoke,我窃取了以下代码,用于在具有已知句柄的按钮上的随机位置进行鼠标单击:

    [Flags]
    public enum MouseEventFlags
    {
        LEFTDOWN = 0x00000002,
        LEFTUP = 0x00000004,
        MIDDLEDOWN = 0x00000020,
        MIDDLEUP = 0x00000040,
        MOVE = 0x00000001,
        ABSOLUTE = 0x00008000,
        RIGHTDOWN = 0x00000008,
        RIGHTUP = 0x00000010
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct Rectangle
    {
        public int X;
        public int Y;
        public int Width;
        public int Height;
    }

    private static void Click(IntPtr Handle)
    {
        lock (typeof(MouseAction))
        {
            Rectangle buttonDesign;

            GetWindowRect(Handle, out buttonDesign);
            Random r = new Random();

            int curX = 10 + buttonDesign.X + r.Next(100 - 20);
            int curY = 10 + buttonDesign.Y + r.Next(60 - 20);

            SetCursorPos(curX, curY);
            //Mouse Right Down and Mouse Right Up
            mouse_event((uint)MouseEventFlags.LEFTDOWN, curX, curY, 0, 0);
            mouse_event((uint)MouseEventFlags.LEFTUP, curX, curY, 0, 0);  
        }
    }

    [DllImport("user32.dll")]
    static extern bool SetCursorPos(int X, int Y);

    [DllImport("user32.dll")]
    private static extern void mouse_event(
        long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

    [DllImport("user32.dll")]
    static extern bool GetWindowRect(IntPtr hWnd, out Rectangle rect); 

回答by xavier

you can use AutoIT, it's freeware, and it has a dll version that you can import into C# (DllImport). It allows you to click on controls, write strings to edit boxes, make combobox selections etc on another application from C#.

您可以使用AutoIT,它是免费软件,并且有一个 dll 版本,您可以将其导入 C# (DllImport)。它允许您在 C# 中的另一个应用程序上单击控件、将字符串写入编辑框、进行组合框选择等。

回答by jsls

I tried to do the same: to use the mouse i used this class

我尝试做同样的事情:为了使用鼠标,我使用了这个类

(you need to add using System.Runtime.InteropServices;)

(你需要添加using System.Runtime.InteropServices;

public class MouseClick1    //public is important here**
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);


        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;


        public int CoordX { get; set; }    
        public int CoordY { get; set; }

                public void Click1()
        {

            Cursor.Position = new Point(CoordX, CoordY);

            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

        }

    }

and after on your form1 (presuming the name is form1) you can

在你的form1之后(假设名称是form1)你可以

 public partial class form1 : Form
    {

 MouseClick1 button1 = new MouseClick1();
 MouseClick1 button2 = new MouseClick1();

[...]

[...]

    public void Simulate_button1and2_Click(object sender, EventArgs e)
    {

       button1.CoordX = 1652;   //random coordinates
       button1.CoordY = 225;
       button2.CoordX = 1650;
       button2.CoordY = 250;

       button1.Click1();
       button1.Click2();

} }

} }

To have the coordinates on your pointer, I use a timer and a label:

为了在指针上显示坐标,我使用了一个计时器和一个标签:

 private void timer1_Tick(object sender, EventArgs e)
        {
            Coord.Text = Cursor.Position.X.ToString() + " : " + Cursor.Position.Y.ToString();  //Premet d'avtheitroad les coord en direct

        }

Work fine with me.

和我一起工作很好。