wpf 如何刷新/重新加载桌面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17503289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:10:09  来源:igfitidea点击:

How to refresh/reload Desktop

c#wpfdesktop

提问by SepehrM

I have a WPF C# project in which I'm implementing settings for Windows folder options. One of them is "Single-click to open an item" (instead of double-click). When I change the registry keys for that matter, I need to refresh Windows Explorer which I found a solution for. But desktop doesn't refresh, and even refreshing it manually doesn't apply the changes. I've used IActiveDesktop::ApplyChangesmethod but didn't work (or maybe I made a mistake). I've also used this code snippet, but it still doesn't apply the changes I've made:

我有一个 WPF C# 项目,我正在其中实现 Windows 文件夹选项的设置。其中之一是“单击以打开项目”(而不是双击)。当我为此更改注册表项时,我需要刷新我找到解决方案的 Windows 资源管理器。但是桌面不会刷新,即使手动刷新也不会应用更改。我使用过IActiveDesktop::ApplyChanges方法但没有用(或者我犯了一个错误)。我也使用了这个代码片段,但它仍然没有应用我所做的更改:

SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);

And here is the full code snippet that I used for refreshing Windows Explorer (which is from this site):

这是我用于刷新 Windows 资源管理器的完整代码片段(来自此站点):

[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, 0x1000, IntPtr.Zero, IntPtr.Zero);

        // Refresh any open explorer windows
        // based on http://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, null, windows, null);
        for (int i = 0; i < (int)count; i++)
        {
            object item = windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, 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", System.Reflection.BindingFlags.GetProperty, null, item, null);
            if (itemName == "Windows Explorer")
            {
                itemType.InvokeMember("Refresh", System.Reflection.BindingFlags.InvokeMethod, null, item, null);
            }
        }
    }

That works for Windows Explorer but not the desktop (which is odd since desktop depends on explorer too). So how should I reload the desktop so that my changes take effect?

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

采纳答案by SepehrM

Thanks for all your replies and comments. I finally figured out a workaround for this problem. We could just hide all desktop icons and then show them again. This will force the desktop to reload.

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

Update:In Window 8, SHELLDLL_DefViewis the child of one of WorkerWwindows. (instead of Progman) So here is the updated code which works on Windows 8 and 8.1 too:

更新:在 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, IntPtr lParam);

    [DllImport("user32.dll", 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), IntPtr.Zero, "SHELLDLL_DefView", null);
                i++;
            }
        }
        SendMessage(hWnd, WM_COMMAND, toggleDesktopCommand, IntPtr.Zero);
    }

Now we can just toggle desktop icons twice:

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

        ToggleDesktopIcons();
        ToggleDesktopIcons();

Hope this helps someone else ...

希望这对其他人有帮助...

回答by Adam White

If you would have posted the code to change that setting, I would have tested it against the following suggestions before replying.

如果您发布了更改该设置的代码,我会在回复之前根据以下建议对其进行测试。

Have you tried:

你有没有尝试过:

1) Removing the statement if (itemName == "Windows Explorer")from the above code, so it refreshes every window (including the desktop)?

1)if (itemName == "Windows Explorer")从上面的代码中删除语句,所以它刷新每个窗口(包括桌面)?

2) Broadcasting a WM_SETTINGCHANGE via SendMessage WIN32 API?

2) 通过 SendMessage WIN32 API 广播 WM_SETTINGCHANGE?

private const int HWND_BROADCAST = 0xffff;
private const int WM_WININICHANGE = 0x001a, WM_SETTINGCHANGE = 0x001a, INI_INTL = 1;
[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, uint wMsg, uint wParam, uint lParam);

SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, INI_INTL);

[Credit]

[信用]

3) IActiveDesktop.ApplyChanges

3) IActiveDesktop.ApplyChanges

[ComImport]
[Guid("F490EB00-1240-11D1-9888-006097DEACF9")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IActiveDesktop
{
     [PreserveSig]
     int ApplyChanges(AD_Apply dwFlags);
     // [...]
     // Note: There is a lot more to this interface,
     //        please see PInvoke.net link below.
}
private const int AD_APPLY_REFRESH = 4;

IActiveDesktop.ApplyChanges(AD_APPLY_REFRESH);

[PInvoke.net - IActiveDesktop]

[PInvoke.net - IActiveDesktop]

If these do not work, let me know. If it comes down to it, it is possible to save all the open explorer windows & their positions, terminate explorer, wait for explorer to restart, re-open each explorer window and re-position them... if that would acceptable.

如果这些都不起作用,请告诉我。如果归根结底,可以保存所有打开的资源管理器窗口及其位置,终止资源管理器,等待资源管理器重新启动,重新打开每个资源管理器窗口并重新定位它们......如果这可以接受的话。

Hope this helps.

希望这可以帮助。