使用c#关闭来自其他程序的消息框

这是我的问题:我们的产品有自动构建过程。 在编译其中一个VB6项目期间,会弹出一个消息框,要求用户在继续之前单击“确定”。 作为一个自动化过程,这是一件坏事,因为它可能最终会在那里停留数小时而不会移动,直到有人点击确定。 我们已经研究了VB6代码来尝试抑制消息框,但似乎没有人能够弄清楚现在如何。 因此,作为临时修复程序,我正在处理将在后台运行的程序,当弹出消息框时,将其关闭。 到目前为止,我能够检测到消息何时弹出,但我似乎无法找到正确关闭它的function。 该程序是用C#编写的,我使用user32.dll中的FindWindow函数来获取指向窗口的指针。 到目前为止,我已经尝试了closeWindow,endDialog和postMessage来尝试关闭它,但它们似乎都没有用。 closeWindow只是最小化它,endDialog会出现一个错误的内存exception,而postMessage什么都不做。 有没有人知道任何其他function会处理这个,或任何其他方式去除这个消息? 提前致谢。

这是我目前的代码:

class Program { [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); static void Main(string[] args) { IntPtr window = FindWindow(null, "Location Browser Error"); while(window != IntPtr.Zero) { Console.WriteLine("Window found, closing..."); //use some function to close the window window = IntPtr.Zero; } } } 

你必须找到窗口,这是第一步。 您可以使用SendMessage发送SC_CLOSE消息。

样品

 [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; IntPtr window = FindWindow(null, "Location Browser Error"); if (window != IntPtr.Zero) { Console.WriteLine("Window found, closing..."); SendMessage((int) window, WM_SYSCOMMAND, SC_CLOSE, 0); } 

更多信息

  • 使用WIN API查找并关闭窗口

当您找到消息框时,尝试使用BN_CLICKED类型和OK按钮的ID发送WM_NOTIFY