【发布时间】:2011-03-11 10:32:32
【问题描述】:
我有一个用于 c# 的 .dll 库,它喜欢在启动时弹出一个“欢迎”屏幕。 此屏幕显示为任务管理器中的应用程序。
有没有办法自动检测这个应用程序/表单正在启动并关闭它?
谢谢! :)
【问题讨论】:
标签: c#
我有一个用于 c# 的 .dll 库,它喜欢在启动时弹出一个“欢迎”屏幕。 此屏幕显示为任务管理器中的应用程序。
有没有办法自动检测这个应用程序/表单正在启动并关闭它?
谢谢! :)
【问题讨论】:
标签: c#
如果它在您的进程中运行并打开一个表单(而不是对话框),您可以使用类似的方法来关闭您自己的程序集未打开的所有表单。
foreach (Form form in Application.OpenForms)
if (form.GetType().Assembly != typeof(Program).Assembly)
form.Close();
什么是您自己的程序集由类 Program 定义,您也可以使用 Assembly.GetExecutingAssembly 或 Assembly.GetCallingAssembly,但我不确定如果您在 Visual Studio 中运行应用程序(因为它可能会返回 VS 程序集)。
【讨论】:
这里是一个简单的控制台应用程序,它将监视和关闭指定的窗口
class Program
{
static void Main(string[] args)
{
while(true)
{
FindAndKill("Welcome");
Thread.Sleep(1000);
}
}
private static void FindAndKill(string caption)
{
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
StringBuilder windowText = new StringBuilder(256);
GetWindowText(pFoundWindow, windowText, windowText.Capacity);
if (windowText.ToString() == caption)
{
p.CloseMainWindow();
Console.WriteLine("Excellent kill !!!");
}
}
}
[DllImport("user32.dll", EntryPoint = "GetWindowText",ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd,StringBuilder lpWindowText, int nMaxCount);
}
【讨论】: