gpt4 book ai didi

c# - 如何使用 C# 在 Windows 中关闭警告窗口?

转载 作者:可可西里 更新时间:2023-11-01 09:31:45 25 4
gpt4 key购买 nike

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

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

对于这方面的任何帮助,我将不胜感激。谢谢!

更新:只是想让你知道这个例子对我有用。非常感谢你。下面有一些代码可以帮助其他人。该代码已使用 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);        }    }}

最佳答案

您可以尝试使用 PInvoke 通过名称和/或窗口类等参数调用 FindWindow() API,然后 PInvoke SendMessage(window, WM_CLOSE, 0, 0) API 将其关闭。

关于c# - 如何使用 C# 在 Windows 中关闭警告窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3098629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com