【问题标题】:Find and close multiple Windows查找并关闭多个窗口
【发布时间】:2013-12-09 15:41:32
【问题描述】:

我下面的代码找到一个以特定“窗口名称”开头的窗口,然后将其关闭。但是,有多个同名窗口同时打开。我需要同时关闭所有这些。我该怎么做?

foreach (Process proc in Process.GetProcesses())
{
    string toClose = null;

    if (proc.MainWindowTitle.StartsWith("untitled"))
    {
        toClose = proc.MainWindowTitle;

        int hwnd = 0;

        //Get a handle for the Application main window
        hwnd = FindWindow(null, toClose);

        //send WM_CLOSE system message
        if (hwnd != 0)
            SendMessage(hwnd, WM_CLOSE, 0, IntPtr.Zero);        
    }
}

【问题讨论】:

  • 您当前的代码有什么问题?你在寻找什么样的“高效”?
  • 基本上当前代码只会关闭一个名为“untitled”的窗口,而其他同名窗口会保持打开状态,需要一次全部关闭
  • 您正在运行的表单是无标题的吗?它是关闭的第一个表单吗?
  • 不太明白,不过我的应用不是叫无标题的,其他第三方应用都是。
  • 你的意思是其他窗口属于同一个进程还是不同进程?

标签: c# window


【解决方案1】:

您可以使用返回 Process 类型数组的 Process.GetProcessesByName 方法。例如,如果您打开了 2 个未命名的实例,它会返回一个包含 2 个进程的数组。

查看更多信息: http://msdn.microsoft.com/en-us/library/System.Diagnostics.Process.GetProcessesByName.aspx

希望这会有所帮助:)

编辑:

枚举窗口并按名称查找窗口。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class Program
    {
        protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        protected static extern int GetWindowTextLength(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lp1, string lp2);

        [DllImport("user32.dll")]
        protected static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

        [DllImport("user32.dll")]
        protected static extern bool IsWindowVisible(IntPtr hWnd);
        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);

        private static void Main(string[] args)
        {
            EnumWindows(EnumTheWindows, IntPtr.Zero);

            Console.ReadLine();
        }

        static uint WM_CLOSE = 0x10;

        protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam)
        {
            int size = GetWindowTextLength(hWnd);
            if (size++ > 0 && IsWindowVisible(hWnd))
            {
                StringBuilder sb = new StringBuilder(size);
                GetWindowText(hWnd, sb, size);
                if (sb.ToString().StartsWith("Untitled"))
                    SendMessage(hWnd, WM_CLOSE, 0, 0);
            }
            return true;
        }
    }
}

【讨论】:

  • 对不起,我的错一开始应该更清楚地解释它,这是在正确的轨道上,但窗口并没有准确地说“无标题”,它就像“无标题 - abc”,“无标题 - abg”等
  • 我还需要通过窗口标题搜索它。当前代码工作正常,但一次只关闭一个窗口。我需要把它变成一个数组,这应该可以解决问题,但我该怎么做呢?
  • 我认为 EnumWindows 只给你顶层,没有子窗口。
  • 您可以尝试按进程名称搜索进程,然后枚举窗口,枚举完后关闭以“Untitled”开头的窗口。
猜你喜欢
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多