如何获取另一个进程MessageBox的消息

本文关键字:进程 MessageBox 消息 另一个 何获取 获取 | 更新日期: 2023-09-27 17:50:21

另一个应用程序有时显示标题为"Test"的MessageBox和MessageBox体内的一些消息,我怎么能从另一个应用程序(写在c++或c#什么)得到这个消息,然后关闭它?c#代码:

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd,uint Msg,int wParam,int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
for (int i = 0; i < 40; i++) // just test cycle
{
     IntPtr window = FindWindow(null, "Test");
     if (window != IntPtr.Zero) // ok we found it!
     {
          // code that reads MessageBox message?
          .....
          // work is done, closing
          SendMessage((int)window, WM_SYSCOMMAND, SC_CLOSE, 0);
          break;
     }
     System.Threading.Thread.Sleep(50);
}
你能帮我解决这个问题吗?我该怎么做呢?

如何获取另一个进程MessageBox的消息

看看这个帖子。

选择1:

你可以使用像spy++这样的API来访问其他windows控件。

选择2:

你应该使用Windows api,如EnumWindow或FindWindow,然后使用来查找目标窗口中的控件,比如你正在寻找的文本框,然后是API SetWindowText

看看这里的一些想法:为什么EnumChildWindows跳过孩子吗?也可以在Stack Overflow和中搜索这些api名称你会发现很多例子……

选择3:

我找到了这个api,它可以让你完成你的任务:http://www.programmersheaven.com/download/56171/download.aspx