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

        }

別のドットネット アプリ:

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

しかし、いずれにせよ発生しません。どなたか解決方法をご存じの方がいらっしゃればお願いします。

4

0 に答える 0