我已经尝试搜索,但是没有什么能真正满足我的需求。
我不希望explorer.exe被终止或重新启动。我只想关闭所有打开的资源管理器窗口。
[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();
}
编辑:
所以我想唯一有趣的是回调将由Windows为每个枚举的窗口(在hwnd中所述窗口的句柄)调用
GetWindowThreadProcessId
为我们提供给定窗口句柄的processid
GetProcessById
然后为我们提供一个流程对象,以读取诸如流程名称之类的内容
RealGetWindowClass
为我们提供给定窗口句柄的注册类名称
最后,我们可以查看当前窗口的进程是否为资源管理器,以及窗口类是否为“ CabinetWClass”,这是普通文件资源管理器窗口的窗口类。
最后但并非最不重要的一点是,如果我们的检查通过,请发送一条WM_CLOSE
消息,请窗口自行关闭...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句