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

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

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

[System.Runtime.InteropServices.DllImport("Shell32.dll")]
    private static extern int SHChangeNotify(int eventId,int flags,IntPtr item1,IntPtr item2);

    public static void RefreshWindowsExplorer()
    {
        // Refresh the desktop
        SHChangeNotify(0x8000000,IntPtr.Zero);

        // Refresh any open explorer windows
        // based on https://stackoverflow.com/questions/2488727/refresh-windows-explorer-in-win7
        Guid CLSID_ShellApplication = new Guid("13709620-C279-11CE-A49E-444553540000");
        Type shellApplicationType = Type.GetTypeFromCLSID(CLSID_ShellApplication,true);

        object shellApplication = Activator.CreateInstance(shellApplicationType);
        object windows = shellApplicationType.InvokeMember("Windows",System.Reflection.BindingFlags.InvokeMethod,null,shellApplication,new object[] { });

        Type windowsType = windows.GetType();
        object count = windowsType.InvokeMember("Count",System.Reflection.BindingFlags.GetProperty,windows,null);
        for (int i = 0; i < (int)count; i++)
        {
            object item = windowsType.InvokeMember("Item",new object[] { i });
            Type itemType = item.GetType();

            // Only refresh Windows Explorer,without checking for the name this could refresh open IE windows
            string itemName = (string)itemType.InvokeMember("Name",item,null);
            if (itemName == "Windows Explorer")
            {
                itemType.InvokeMember("Refresh",null);
            }
        }
    }

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

解决方法

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

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

[DllImport("user32.dll",SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

    [DllImport("user32.dll",SetLastError = true)]
    static extern IntPtr GetWindow(IntPtr hWnd,GetWindow_Cmd uCmd);

    [DllImport("user32.dll",CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd,UInt32 Msg,IntPtr wParam,IntPtr lParam);

    enum GetWindow_Cmd : uint
    {
        GW_HWNDFIRST = 0,GW_HWNDLAST = 1,GW_HWNDNEXT = 2,GW_HWNDPREV = 3,GW_OWNER = 4,GW_CHILD = 5,GW_ENABLEDPOPUP = 6
    }

    private const int WM_COMMAND = 0x111;

    [DllImport("user32.dll",SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow);

    private delegate bool EnumWindowsProc(IntPtr hWnd,IntPtr lParam);

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

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

    [DllImport("user32.dll")]
    private static extern bool EnumWindows(EnumWindowsProc enumProc,SetLastError = true,CharSet = CharSet.Auto)]
    static extern int GetClassName(IntPtr hWnd,StringBuilder lpClassName,int nMaxCount);


    public static string GetWindowText(IntPtr hWnd)
    {
        int size = GetWindowTextLength(hWnd);
        if (size++ > 0)
        {
            var builder = new StringBuilder(size);
            GetWindowText(hWnd,builder,builder.Capacity);
            return builder.ToString();
        }

        return String.Empty;
    }

    public static IEnumerable<IntPtr> FindWindowsWithClass(string className)
    {
        IntPtr found = IntPtr.Zero;
        List<IntPtr> windows = new List<IntPtr>();

        EnumWindows(delegate(IntPtr wnd,IntPtr param)
        {
            StringBuilder cl = new StringBuilder(256);
            GetClassName(wnd,cl,cl.Capacity);
            if (cl.ToString() == className && (GetWindowText(wnd) == "" || GetWindowText(wnd) == null))
            {
                windows.Add(wnd);
            }
            return true;
        },IntPtr.Zero);

        return windows;
    }

    static void ToggleDesktopIcons()
    {
        var toggleDesktopCommand = new IntPtr(0x7402);
        IntPtr hWnd = IntPtr.Zero;
        if (Environment.OSVersion.Version.Major < 6 || Environment.OSVersion.Version.Minor < 2) //7 and -
            hWnd = GetWindow(FindWindow("Progman","Program Manager"),GetWindow_Cmd.GW_CHILD);
        else
        {
            var ptrs = FindWindowsWithClass("WorkerW");
            int i = 0;
            while (hWnd == IntPtr.Zero && i < ptrs.Count())
            {
                hWnd = FindWindowEx(ptrs.ElementAt(i),"SHELLDLL_DefView",null);
                i++;
            }
        }
        SendMessage(hWnd,WM_COMMAND,toggleDesktopCommand,IntPtr.Zero);
    }

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

ToggleDesktopIcons();
        ToggleDesktopIcons();

希望这有助于其他人……

猜你在找的C#相关文章