这是我的问题:我们的产品有一个自动构建过程。在编译其中一个 VB6 项目期间,弹出一个消息框,要求用户单击“确定”才能继续。作为一个自动化过程,这是一件坏事,因为它最终可能会在那里坐上几个小时不动,直到有人点击确定。我们研究了 VB6 代码以尝试抑制消息框,但现在似乎没有人知道如何做。因此,作为临时修复,我正在开发一个将在后台运行的程序,当消息框弹出时,将其关闭。到目前为止,我能够检测到消息何时弹出,但我似乎无法找到正确关闭它的功能。该程序是用 C# 编写的,我正在使用 user32.dll 中的 FindWindow 函数来获取指向窗口的指针。到目前为止,我已经尝试过 closeWindow、endDialog 和 postMessage 来尝试关闭它,但它们似乎都不起作用。 closeWindow 只是将它最小化,endDialog 出现错误的内存异常,而 postMessage 什么都不做。有谁知道会处理此问题的任何其他功能,或任何其他消除此消息的方法?提前致谢。
这是我目前的代码:
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);
}
更多信息
关于c# - 使用 C# 从另一个程序关闭消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11729281/