关闭打开的资源管理器窗口而不终止explorer.exe我不想终止或重新启动explorer.exe。我只想关闭所有打开的资源管理器窗口。[DllImport("user32.dll")]privatestaticexternboolEnumWindows(EnumWindowsDelegatelpEnumFunc,IntPtrlParam);[DllImport("user32.dll")]privatestaticexternIntPtrGetWindowThreadProcessId(IntPtrhWnd,outIntPtrlpdwProcessId);[DllImport("user32.dll")]privatestaticexternuintRealGetWindowClass(IntPtrhwnd,StringBuilderpszType,uintcchType);[DllImport("user32.dll")]staticexternIntPtrSendMessage(IntPtrhWnd,UInt32Msg,IntPtrwParam,IntPtrlParam);静态uintWM_CLOSE=0x10;私人委托boolEnumWindowsDelegate(IntPtrhwnd,IntPtrlParam);privatestaticboolEnumWindowsCallback(IntPtrhwnd,IntPtrlParam){IntPtrpid=newIntPtr();GetWindowThreadProcessId(hwnd,outpid);varwndProcess=System.Diagnostics.Process.GetProcessById(pid.ToInt32());varwndClass=newStringBuilder(255);RealGetWindowClass(hwnd,wndClass,255);if(wndProcess.ProcessName=="explorer"&&wndClass.ToString()=="CabinetWClass"){//你好文件资源管理器窗口...SendMessage(hwnd,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);//...再见文件资源管理器窗口}return(true);}staticvoidMain(){EnumWindowsDelegatechildProc=newEnumWindowsDelegate(EnumWindowsCallback);EnumWindows(childProc,IntPtr.Zero);控制台.ReadKey();编辑:所以我想唯一有趣的是窗口将为每个枚举窗口(所述窗口句柄的hwnd)调用的回调GetWindowThreadProcessId给我们给定窗口句柄的processid然后,GetProcessById给我们一个进程对象读取进程名等RealGetWindowClass给我们给定窗口句柄的注册类名最后我们可以查看当前窗口的进程是不是资源管理器,是窗口类“CabinetWClass”,这是一个普通文件资源管理器窗口的窗口类最后但并非最不重要的一点是,如果我们的检查正常,则发送WM_CLOSE消息,请求关闭窗口...以下替代方法使用Shell对象的COMAPI来检索和识别文件资源管理器窗口。它需要添加一个COM引用:Shell.Windows方法返回的对象是一个IEnumerable。集合中的每个对象都是SHDocVw.InternetExplorer的一个实例。如果Document对象是Shell32.ShellFolderView,则资源管理器是文件资源管理器。以上就是C#学习教程:在不终止explorer.exe共享的所有内容的情况下关闭和打开资源管理器窗口。如果对大家有用,需要进一步了解C#学习教程,希望大家多加关注—privatestaticvoidCloseExplorerWindows(){Shell32.Shellshell=newShell32.Shell();//参考:Shell.Windows方法//https://msdn.microsoft.com/en-us/library/windows/desktop/bb774107(v=vs.85).aspxSystem.Collections.IEnumerablewindows=shell.Windows()作为System.Collections.IEnumerable;if(windows!=null){//ref:ShellWindows对象//https://msdn.microsoft.com/en-us/library/windows/desktop/bb773974(v=vs.85).aspxforeach(SHDocVw.Windows中的InternetExplorer窗口){objectdoc=window.Document;if(doc!=null&&docisShell32.ShellFolderView){window.Quit();//关闭窗口}}}}publicstaticvoidCloseExplorerWindows()=>EnumWindows(newEnumWindowsProc(EnumTheWindows),IntPtr.Zero);私人委托boolEnumWindowsProc(IntPtrhWnd,IntPtrlParam);[DllImport("user32.dll",CharSet=CharSet.Unicode)]privatestaticexternintGetWindowText(IntPtrhWnd,StringBuilderstrText,intmaxCount);[DllImport("user32.dll",CharSet=CharSet.Unicode)]privatestaticexternintGetWindowTextLength(IntPtrhWnd);[DllImport("user32.dll")]privatestaticexternboolEnumWindows(EnumWindowsProcenumProc,IntPtrlParam);[DllImport("user32.dll")]privatestaticexternboolIsWindowVisible(IntPtrhWnd);[DllImport("user32.dll",SetLastError=true)]staticexternuintGetWindowThreadProcessId(IntPtrhWnd,outuintlpdwProcessId);[DllImport("user32.dll",CharSet=CharSet.Auto)]staticexternIntPtrSendMessage(IntPtrhWnd,UInt32Msg,IntPtrwParam,IntPtrlParam);静态uintWM_CLOSE=0x10;privatestaticboolEnumTheWindows(IntPtrhWnd,IntPtrlParam){intsize=GetWindowTextLength(hWnd);if(size++>0&&IsWindowVisible(hWnd)){varsb=newStringBuilder(size);GetWindowText(hWnd,sb,size);varthreadID=GetWindowThreadProcessId(hWnd,outvarprocessID);变量s=系统.Diagnostics.Process.GetProcessById((int)processID).ProcessName;if(s=="explorer"&&sb.ToString()!="ProgramManager")SendMessage(hWnd,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);}返回真;}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
