1

使用 c# 从另一个应用程序单击按钮

我需要以编程方式处理另一个 Windows 应用程序。

下面是我的代码:

       public const uint WM_LBUTTONDOWN = 0x0201;
        public const uint WM_LBUTTONUP = 0x0202;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        /// <summary>
        /// The FindWindow API
        /// </summary>
        /// <param name="lpClassName">the class name for the window to search for</param>
        /// <param name="lpWindowName">the name of the window to search for</param>
        /// <returns></returns>
        [DllImport("User32.dll")]
        public static extern Int32 FindWindow(String lpClassName, String lpWindowName);

        /// <summary>
        /// The SendMessage API
        /// </summary>
        /// <param name="hWnd">handle to the required window</param>
        /// <param name="msg">the system/Custom message to send</param>
        /// <param name="wParam">first message parameter</param>
        /// <param name="lParam">second message parameter</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(int hWnd, uint msg, int wParam, IntPtr lParam);

        /// <summary>
        /// The FindWindowEx API
        /// </summary>
        /// <param name="parentHandle">a handle to the parent window </param>
        /// <param name="childAfter">a handle to the child window to start search after</param>
        /// <param name="className">the class name for the window to search for</param>
        /// <param name="windowTitle">the name of the window to search for</param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);



        private void button1_Click(object sender, EventArgs e)
        {
            int hwnd = 0;
            IntPtr hwndChild = IntPtr.Zero;

             hwnd = FindWindow(null, "Form22");
            if (hwnd != 0)
            {
// AnotherButton => text button another programm
                hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "AnotherButton");

                 SendMessage((int) hwndChild, WM_LBUTTONDOWN, 0, IntPtr.Zero);
            }

        }

另一个 dotnet 应用程序:

  private void button1_KeyDown(object sender, KeyEventArgs e)
    {
        Text = "another Program";
    }

但无论如何都不会发生。如果有人知道如何解决它,请帮助。

4

0 回答 0