如何自动响应msgbox

我正在开发一个C#应用程序来自动运行调用几个VB6 .exe文件的旧版VBScript(vbs)文件。 .exe文件具有我需要“响应”的消息框弹出窗口,以允许VBScript进程无人值守运行。 响应需要是Enter键。 我没有.exe文件的来源,我不确切知道他们做了什么。 我非常感谢任何帮助……

您可能会发现AutoIt很有帮助。

AutoIt v3是一种免费的类似BASIC的脚本语言,用于自动化Windows GUI和通用脚本。 它使用模拟击键,鼠标移动和窗口/控制操作的组合,以便以其他语言(例如VBScript和SendKeys)不可能或不可靠的方式自动执行任务。

您可以仅使用AutoIt编程语言开发一些东西,也可以从自己的应用程序中驱动它。 我的团队正在使用它,取得了很好的成功。

您可以使用wsh SendKeys()函数。 但是,因为您需要确保激活消息框,所以您还需要在SendKeys调用之前立即调用AppActivate() 。

即使这是错误的,但我已经编写了几个脚本,只要您可以预测消息框何时出现,您可以发送[Enter]键来响应它。

您可以在C#中执行此操作而无需使用某些外部实用程序。 诀窍是搜索消息框对话框并单击其确定按钮。 多次执行此操作需要一个Timer,它不断搜索这样的对话框并单击它。 在项目中添加一个新类并粘贴以下代码:

using System; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; class MessageBoxClicker : IDisposable { private Timer mTimer; public MessageBoxClicker() { mTimer = new Timer(); mTimer.Interval = 50; mTimer.Enabled = true; mTimer.Tick += new EventHandler(findDialog); } private void findDialog(object sender, EventArgs e) { // Enumerate windows to find the message box EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow); EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero); GC.KeepAlive(callback); } private bool checkWindow(IntPtr hWnd, IntPtr lp) { // Checks if  is a dialog StringBuilder sb = new StringBuilder(260); GetClassName(hWnd, sb, sb.Capacity); if (sb.ToString() != "#32770") return true; // Got it, send the BN_CLICKED message for the OK button SendMessage(hWnd, WM_COMMAND, (IntPtr)IDC_OK, IntPtr.Zero); // Done return false; } public void Dispose() { mTimer.Enabled = false; } // P/Invoke declarations private const int WM_COMMAND = 0x111; private const int IDC_OK = 2; private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp); [DllImport("user32.dll")] private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp); [DllImport("kernel32.dll")] private static extern int GetCurrentThreadId(); [DllImport("user32.dll")] private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen); [DllImport("user32.dll")] private static extern IntPtr GetDlgItem(IntPtr hWnd, int item); [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); } 

样品用法:

 private void button1_Click(object sender, EventArgs e) { using (new MessageBoxClicker()) { MessageBox.Show("gonzo"); } } 

可能希望查看使用SetWinEventHook PInvoke来检测何时创建对话框。 您可以将挂钩指定为全局或特定进程。 您可以设置WINEVENT_OUTOFCONTEXT标志,以确保您的代码在您挂钩的过程中实际上没有运行。 您正在寻找的事件应该是EVENT_SYSTEM_DIALOGSTART。

一旦你得到了对话框的hwnd(来自事件挂钩),你可以使用带有WM_COMMAND或WM_SYSCOMMAND的SendMesssage来摆脱它。

在过去的两天试图让这个工作后,我终于放弃了,并决定采用另一种方法。 我正在询问正在发送到外部进程的数据,并对导致消息框弹出窗口的条件进行筛选。 感谢所有回复答案的人!

使用sendkey方法,传递键盘键值并继续执行。