代码之家  ›  专栏  ›  技术社区  ›  Salamander2007

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

  •  22
  • Salamander2007  · 技术社区  · 16 年前

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

    6 回复  |  直到 16 年前
        1
  •  39
  •   Uwe Keim    8 年前

    你看过吗 White TestStack ?

    样例代码:

    Application application = Application.Launch("foo.exe");
    Window window = application.GetWindow("bar", InitializeOption.NoCache);
    
    Button button = window.Get<Button>("save");
    button.Click();
    

    我不认为会比这更好。这个图书馆是由thoughtworks创建的。

        2
  •  4
  •   Sam Harwell    16 年前

    请参见本页上的“将按键发送到其他应用程序”:

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

        3
  •  2
  •   Matthieu kelly    14 年前

    你可以使用 AutoIT ,这是免费软件,它有一个可以导入到c(dll import)的dll版本。它允许您单击控件,将字符串写入编辑框,在另一个应用程序上进行组合框选择等。

        4
  •  1
  •   abhilash    16 年前

    使用 SendMessage 本机Win32 API。 DllImport 此方法来自user32.dll。您可以使用此API发送两条键盘和鼠标消息

        5
  •  1
  •   Yuriy Faktorovich    16 年前

    您可以使用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); 
    
        6
  •  0
  •   jsls    9 年前

    我也试着这么做:为了使用鼠标,我用了这个课程

    (您需要添加 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);
    
            }
    
        }
    

    在你的表格1(假定名字是表格1)之后,你可以

     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();
    

    } }

    要在指针上显示坐标,我使用计时器和标签:

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

    和我一起工作很好。