c# - 使用 C# 从另一个程序关闭消息框

标签 c#

这是我的问题:我们的产品有一个自动构建过程。在编译其中一个 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/

相关文章:

c# - 如何使用消息批处理大小不大于等于 240 的重命名消息创建最后一个文件

c# - Asp.Net/C# - 如何获取嵌套在 Repeater 中的 Label 控件的文本?

c# - 如何在 C# 中将小时从 h 转换为 hh

c# - 如何在图形上创建镜像形状

c# - 在 Windows Phone 8.1 C# 中创建警报或提醒?

c# - 我如何告诉 Dapper 在使用 "WHERE"的 "IN"子句中使用 varchar 作为参数列表?

c# - 如何从Windows服务确定当前的Windows用户?

c# - 使用 FileSystemWatcher 监视文件创建并在删除前复制它

c# - 抑制文件中的抑制错误给出错误 : The Selected Message Could not be suppressed

c# - 如何制作包含主要项目的列表