c# – 如何刷新/重新加载桌面

前端之家收集整理的这篇文章主要介绍了c# – 如何刷新/重新加载桌面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 WPF C#项目,我正在实现 Windows文件夹选项的设置.其中之一是“单击以打开项目”(而不是双击).当我更改注册表项时,我需要刷新我找到解决方案的Windows资源管理器.但桌面不刷新,甚至手动刷新它不会应用更改.
我已经使用了IActiveDesktop :: ApplyChanges方法,但没有用(或者我犯了一个错误).我也使用了这段代码片段,但它仍然没有应用我所做的更改:
  1. SHChangeNotify(0x8000000,0x1000,IntPtr.Zero,IntPtr.Zero);

这里是我用来刷新Win Explorer的完整代码片段(女巫来自这个网站):

  1. [System.Runtime.InteropServices.DllImport("Shell32.dll")]
  2. private static extern int SHChangeNotify(int eventId,int flags,IntPtr item1,IntPtr item2);
  3.  
  4. public static void RefreshWindowsExplorer()
  5. {
  6. // Refresh the desktop
  7. SHChangeNotify(0x8000000,IntPtr.Zero);
  8.  
  9. // Refresh any open explorer windows
  10. // based on https://stackoverflow.com/questions/2488727/refresh-windows-explorer-in-win7
  11. Guid CLSID_ShellApplication = new Guid("13709620-C279-11CE-A49E-444553540000");
  12. Type shellApplicationType = Type.GetTypeFromCLSID(CLSID_ShellApplication,true);
  13.  
  14. object shellApplication = Activator.CreateInstance(shellApplicationType);
  15. object windows = shellApplicationType.InvokeMember("Windows",System.Reflection.BindingFlags.InvokeMethod,null,shellApplication,new object[] { });
  16.  
  17. Type windowsType = windows.GetType();
  18. object count = windowsType.InvokeMember("Count",System.Reflection.BindingFlags.GetProperty,windows,null);
  19. for (int i = 0; i < (int)count; i++)
  20. {
  21. object item = windowsType.InvokeMember("Item",new object[] { i });
  22. Type itemType = item.GetType();
  23.  
  24. // Only refresh Windows Explorer,without checking for the name this could refresh open IE windows
  25. string itemName = (string)itemType.InvokeMember("Name",item,null);
  26. if (itemName == "Windows Explorer")
  27. {
  28. itemType.InvokeMember("Refresh",null);
  29. }
  30. }
  31. }

这适用于Windows资源管理器,但不适用于桌面(这是奇怪的,因为桌面也依赖于资源管理器).
那么我应该如何重新加载桌面以使我的更改生效?

解决方法

感谢您的所有回复评论.我终于想出了解决这个问题的方法.我们可以隐藏所有桌面图标,然后再次显示它们.这将强制桌面重新加载.

更新:在Window 8中,SHELLDLL_DefView是WorkerW窗口之一的子代. (而不是Progman)所以这里是更新的代码,也适用于Windows 8和8.1:

  1. [DllImport("user32.dll",SetLastError = true)]
  2. static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
  3.  
  4. [DllImport("user32.dll",SetLastError = true)]
  5. static extern IntPtr GetWindow(IntPtr hWnd,GetWindow_Cmd uCmd);
  6.  
  7. [DllImport("user32.dll",CharSet = CharSet.Auto)]
  8. static extern IntPtr SendMessage(IntPtr hWnd,UInt32 Msg,IntPtr wParam,IntPtr lParam);
  9.  
  10. enum GetWindow_Cmd : uint
  11. {
  12. GW_HWNDFIRST = 0,GW_HWNDLAST = 1,GW_HWNDNEXT = 2,GW_HWNDPREV = 3,GW_OWNER = 4,GW_CHILD = 5,GW_ENABLEDPOPUP = 6
  13. }
  14.  
  15. private const int WM_COMMAND = 0x111;
  16.  
  17. [DllImport("user32.dll",SetLastError = true)]
  18. static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow);
  19.  
  20. private delegate bool EnumWindowsProc(IntPtr hWnd,IntPtr lParam);
  21.  
  22. [DllImport("user32.dll",CharSet = CharSet.Unicode)]
  23. private static extern int GetWindowText(IntPtr hWnd,StringBuilder strText,int maxCount);
  24.  
  25. [DllImport("user32.dll",CharSet = CharSet.Unicode)]
  26. private static extern int GetWindowTextLength(IntPtr hWnd);
  27.  
  28. [DllImport("user32.dll")]
  29. private static extern bool EnumWindows(EnumWindowsProc enumProc,SetLastError = true,CharSet = CharSet.Auto)]
  30. static extern int GetClassName(IntPtr hWnd,StringBuilder lpClassName,int nMaxCount);
  31.  
  32.  
  33. public static string GetWindowText(IntPtr hWnd)
  34. {
  35. int size = GetWindowTextLength(hWnd);
  36. if (size++ > 0)
  37. {
  38. var builder = new StringBuilder(size);
  39. GetWindowText(hWnd,builder,builder.Capacity);
  40. return builder.ToString();
  41. }
  42.  
  43. return String.Empty;
  44. }
  45.  
  46. public static IEnumerable<IntPtr> FindWindowsWithClass(string className)
  47. {
  48. IntPtr found = IntPtr.Zero;
  49. List<IntPtr> windows = new List<IntPtr>();
  50.  
  51. EnumWindows(delegate(IntPtr wnd,IntPtr param)
  52. {
  53. StringBuilder cl = new StringBuilder(256);
  54. GetClassName(wnd,cl,cl.Capacity);
  55. if (cl.ToString() == className && (GetWindowText(wnd) == "" || GetWindowText(wnd) == null))
  56. {
  57. windows.Add(wnd);
  58. }
  59. return true;
  60. },IntPtr.Zero);
  61.  
  62. return windows;
  63. }
  64.  
  65. static void ToggleDesktopIcons()
  66. {
  67. var toggleDesktopCommand = new IntPtr(0x7402);
  68. IntPtr hWnd = IntPtr.Zero;
  69. if (Environment.OSVersion.Version.Major < 6 || Environment.OSVersion.Version.Minor < 2) //7 and -
  70. hWnd = GetWindow(FindWindow("Progman","Program Manager"),GetWindow_Cmd.GW_CHILD);
  71. else
  72. {
  73. var ptrs = FindWindowsWithClass("WorkerW");
  74. int i = 0;
  75. while (hWnd == IntPtr.Zero && i < ptrs.Count())
  76. {
  77. hWnd = FindWindowEx(ptrs.ElementAt(i),"SHELLDLL_DefView",null);
  78. i++;
  79. }
  80. }
  81. SendMessage(hWnd,WM_COMMAND,toggleDesktopCommand,IntPtr.Zero);
  82. }

现在我们可以两次切换桌面图标:

  1. ToggleDesktopIcons();
  2. ToggleDesktopIcons();

希望这有助于其他人……

猜你在找的C#相关文章