关闭打开的资源管理器窗口而不终止 explorer.exe [英] Close open Explorer windows without terminating explorer.exe

查看:42
本文介绍了关闭打开的资源管理器窗口而不终止 explorer.exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过搜索,但没有什么能真正满足我的需求.

I've tried searching but nothing really matches my demand.

我不希望 explorer.exe 被终止或重新启动.我只想关闭所有打开的资源管理器窗口.

I don't want explorer.exe to be terminated or restarted. I just want any open explorer windows to close.

推荐答案

    [DllImport("user32.dll")]
    private static extern bool EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lParam);
    [DllImport("user32.dll")]
    private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);
    [DllImport("user32.dll")]
    private static extern uint RealGetWindowClass(IntPtr hwnd, StringBuilder pszType, uint cchType);
    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    static uint WM_CLOSE = 0x10;

    private delegate bool EnumWindowsDelegate(IntPtr hwnd, IntPtr lParam);

    private static bool EnumWindowsCallback(IntPtr hwnd, IntPtr lParam)
    {
        IntPtr pid = new IntPtr();
        GetWindowThreadProcessId(hwnd, out pid);
        var wndProcess = System.Diagnostics.Process.GetProcessById(pid.ToInt32());
        var wndClass = new StringBuilder(255);
        RealGetWindowClass(hwnd, wndClass, 255);
        if (wndProcess.ProcessName == "explorer" && wndClass.ToString() == "CabinetWClass")
        {
            //hello file explorer window...

            SendMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); // ... bye file explorer window
        }
        return (true);
    }

    static void Main()
    {
        EnumWindowsDelegate childProc = new EnumWindowsDelegate(EnumWindowsCallback);

        EnumWindows(childProc, IntPtr.Zero);

        Console.ReadKey();
    }


所以我想唯一有趣的事情是回调,它将被窗口为每个枚举窗口调用(hwnd 中所述窗口的句柄)

edit:
so i guess the only interesting thing is the callback which will be called by windows for each enumerated window (handle of said window in hwnd)

GetWindowThreadProcessId 为我们提供给定窗口句柄的 processid

GetWindowThreadProcessId provides us with the processid for a given window handle

GetProcessById 然后为我们提供一个进程对象来读取进程名称之类的内容

GetProcessById then provides us with a process object to read things like the process name from

RealGetWindowClass 为我们提供给定窗口句柄的注册类名

RealGetWindowClass provides us with the registered class name for a given window handle

最后我们可以看看当前窗口的进程是否为资源管理器,窗口类是否为CabinetWClass",即普通文件资源管理器窗口的窗口类

finally we can look to see if the process for the current window is the explorer and if the window class is "CabinetWClass", which is the window class for the normal file explorer window

最后但并非最不重要的一点,如果我们的检查没问题,发送一个 WM_CLOSE 消息,请窗口自行关闭...

last but not least, if our check is ok, send a WM_CLOSE message to kindly ask the window to close itself...

这篇关于关闭打开的资源管理器窗口而不终止 explorer.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆