如何在C#中通过WindowsAPI找到另一个程序的MessageBox?

时间:2014-06-26 17:35:15

标签: c# winapi mfc user32

我试图在WindowsAPI中使用FindWindow / FindWindowEx找到另一个(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. 目标程序的创建按钮单击 - >在目标程序中出现MessageBox
  2. 我试图找到目标程序的MessageBox
  3. 它的无限循环
  4. 点击我的鼠标确定按钮 - >继续下一步。
  5. 有人帮助我......我想上床睡觉。

1 个答案:

答案 0 :(得分:0)

我自己解决了一个问题。

使用此代码:

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