如何找到另一个程序's MessageBox的WindowsAPI在c#中

本文关键字:MessageBox WindowsAPI 何找 另一个 程序 | 更新日期: 2023-09-27 18:10:37

我试图在WindowsAPI中使用FindWindow/findwindowwex找到另一个(MFC)程序的MessageBox。我的代码是无限循环的。我怎样才能找到它?

我使用这个代码:

        case 17: // target Program's Create Button
            // Click
            SendMessage(hWnd, WM.LBUTTONDOWN, 0, null);
            SendMessage(hWnd, WM.LBUTTONUP, 0, null);
            // -> Show MessageBox in Display
            // my program is stop. What's the happen ?
            /******** here is Problem Code. (infinite loop) ********/
            int main = FindWindow(null, "Create Connecter"); // Parent
            int finish = 0;
            do {
                finish = FindWindowEx(main, 0, null, "Encode"); // MessageBox
                textBox1.AppendText("Wating....");
                System.Threading.Thread.Sleep(100);
            } while (finish == 0);
            textBox1.AppendText("Find MessageBox !!");
            /********************************************************/
            // OK Button Click in MessageBox
            hWnd = FindWindowEx(finish, 0, "Button", "OK");
            SendMessage(hWnd, WM.LBUTTONDOWN, 0, null);
            SendMessage(hWnd, WM.LBUTTONUP, 0, null);

:

  1. 目标程序的创建按钮点击->出现在目标程序的消息框
  2. 我正试图找到目标程序的MessageBox
  3. 它是无限循环
  4. 用鼠标点击OK按钮->继续下一步。

谁来帮帮我……我想睡觉了

如何找到另一个程序's MessageBox的WindowsAPI在c#中

我自己解决了一个问题。

使用此代码:

    case 17: // target Program's Create Button
        // Click : SendMessage -> PostMessage
        PostMessage(hWnd, WM.LBUTTONDOWN, 0, null);
        PostMessage(hWnd, WM.LBUTTONUP, 0, null);
        // used SendMessage : my program is stop
        // useing PostMessage : no problem 
        /******** here is Problem Code. (infinite loop) ********/
        int main = FindWindow(null, "Create Connecter"); // Parent
        int finish = 0;
        do {
            finish = FindWindowEx(main, 0, null, "Encode"); // MessageBox
            textBox1.AppendText("Wating....");
            System.Threading.Thread.Sleep(100);
        } while (finish == 0 || main == finish);
        textBox1.AppendText("Find MessageBox !!");
        /********************************************************/
        // OK Button Click in MessageBox
        hWnd = FindWindowEx(finish, 0, "Button", "OK");
        SendMessage(hWnd, WM.LBUTTONDOWN, 0, null);
        SendMessage(hWnd, WM.LBUTTONUP, 0, null);