0

可能重复:
使用 c# 从另一个程序关闭消息框

我的电脑上有另一个程序,每隔一段时间就会显示一个消息框。弹出这些对话框时是否可以使用 .NET 来关闭它们?

4

1 回答 1

1

使用 FindWindow 和 SendMessage winapi 函数,如这里http://www.codeproject.com/Articles/22257/Find-and-Close-the-Window-using-Win-API

您必须创建一个关闭该窗口的连续循环,例如 while (true)。我使用了 Timer,因为它更有效。这是我的代码:

[DllImport ("user32.dll")] public static extern IntPtr FindWindow  (String sClassName, String sAppName);
[DllImport ("user32.dll")] public static extern int    SendMessage (IntPtr hWnd, uint Msg, int wParam, int lParam);
    Timer t;
    public Form1 ()
    {
        InitializeComponent ();
        t=new Timer ();
        t.Interval=100;
        t.Tick+=delegate
        {
            IntPtr w=FindWindow (null, "Message box title");
            if (w!=null) SendMessage (w, 0x0112, 0xF060, 0);
        };
        t.Start ();
    }

其中 WM_SYSCOMMAND=0x0112 public const int SC_CLOSE = 0xF060;

如果您不知道窗口的类名(如上),请使用 null 和消息框的标题作为参数。当然,这意味着消息框有一个标题。

于 2012-09-09T18:07:26.417 回答