首页 文章

如何使用C#杀死Windows中的警报窗口?

提问于
浏览
3

我在C#中使用System.Diagnostics.Process命名空间来启动系统进程,有时这个新创建的进程无法正常启动,在这些情况下,Windows会向我显示一个警报窗口,提供有关失败进程的信息 . 我需要一种以编程方式关闭(终止)此警报窗口的方法 . 我尝试了以下代码,但它不起作用,因为警报窗口不会出现在Process.GetProcesses()列表中 .

foreach (Process procR in Process.GetProcesses())
{
    if (procR.MainWindowTitle.StartsWith("alert window text"))
    {
        procR.Kill();
        continue;
    } 
}

我将不胜感激任何帮助 . 谢谢!

UPDATE :只是想让你知道这个例子对我有用 . 非常感谢你 . 下面有一些代码可以帮助别人 . 代码在Visual Studio 2008中进行了测试,您仍然需要一个winform和一个按钮来使其工作 .

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
/* More info about Window Classes at http://msdn.microsoft.com/en-us/library/ms633574(VS.85).aspx */

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        const uint WM_CLOSE = 0x10;

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);


        public Form1()
        {
            InitializeComponent();
        }

        /* This event will silently kill any alert dialog box */
        private void button2_Click(object sender, EventArgs e)
        {
            string dialogBoxText = "Rename File"; /* Windows would give you this alert when you try to set to files to the same name */
            IntPtr hwnd = FindWindow("#32770", dialogBoxText);
            SendMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
        }

    }
}

2 回答

  • 0

    您可以尝试使用PInvoke通过参数调用FindWindow()API,例如名称和/或窗口类,然后PInvoke SendMessage(窗口,WM_CLOSE,0,0)API来关闭它 .

  • 2

    正确,因为警报窗口(正确称为消息框)不是应用程序的主窗口 .

    我想你'd have to examine the process' windows使用EnumThreadWindowsGetWindowText .

相关问题