如何以编程方式刷新 Windows 资源管理器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/647316/
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-15 12:09:40  来源:igfitidea点击:

How can I programmatically refresh Windows Explorer?

windowswinapiwindows-vistaexplorerwindows-explorer

提问by HughE

I have a Windows shell extension that uses IShellIconOverlayIdentifierinterface to display overlay icons on files and folders. My extension is a little like TortoiseCVSor TortoiseSVN.

我有一个 Windows shell 扩展,它使用IShellIconOverlayIdentifier接口在文件和文件夹上显示覆盖图标。我的扩展有点像TortoiseCVSTortoiseSVN

Sometimes I need to make Windows Explorer redraw all it's icons. To do this, I call SHChangeNotifylike this:

有时我需要让 Windows 资源管理器重绘它的所有图标。为此,我像这样调用SHChangeNotify

SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL)

This refreshes the desktop and right hand pane of any open explorer windows. It doesn't refresh the folder tree on the left hand side of any Explorer windows.

这会刷新任何打开的资源管理器窗口的桌面和右侧窗格。它不会刷新任何资源管理器窗口左侧的文件夹树。

So I tried sending WM_SETTINGCHANGElike this:

所以我尝试像这样发送WM_SETTINGCHANGE

SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0) 

on Vista this refreshes the folder tree, but not the right hand pane.

在 Vista 上,这会刷新文件夹树,但不会刷新右侧窗格。

The combination of SHChangeNotify()followed by WM_SETTINGCHANGEseems to work quite well on Vista. But I still can't refresh the folder tree on XP if it is displayed.

SHChangeNotify()后跟的组合WM_SETTINGCHANGE似乎在 Vista 上工作得很好。但是如果显示,我仍然无法在 XP 上刷新文件夹树。

Does anyone have any ideas how to do this better?

有没有人有任何想法如何更好地做到这一点?

Is there a better solution for XP?

XP有更好的解决方案吗?

Sending SHCNE_ASSOCCHANGEDis a bit like clubbing Explorer over the head. It causes the whole desktop to refresh quite violently and casues any open Explorer windows to loose there scroll position. Is there anything that's a bit less violent?

发送SHCNE_ASSOCCHANGED有点像在头上对 Explorer 进行泡吧。它会导致整个桌面非常猛烈地刷新,并导致任何打开的资源管理器窗口失去滚动位置。有没有什么不那么暴力的东西?

采纳答案by Simon Lieschke

Does anyone have any ideas how to do this better?

有没有人有任何想法如何更好地做到这一点?

Personally I don't know. You mention the Tortoise programs which do a similar thing, so an excellent starting point would be to have a look at what they do in their source :)

我个人不知道。你提到了做类似事情的 Tortoise 程序,所以一个很好的起点是看看他们在他们的源代码中做了什么:)

These look to be the relevant source files that handle this problem:

这些看起来是处理这个问题的相关源文件:

I note in the RebuildIconsmethod in each of those will:

RebuildIcons在每个人的方法中都注意到:

  1. set the shell icon size or colour depth to a temporary value
  2. updates all the windows by broadcasting the setting change
  3. resets the shell icon size or colour depth to the original value
  4. updates all the windows a second time with a broadcast of the setting change
  1. 将外壳图标大小或颜色深度设置为临时值
  2. 通过广播设置更改来更新所有窗口
  3. 将外壳图标大小或颜色深度重置为原始值
  4. 通过设置更改的广播第二次更新所有窗口

Perhaps this is part of the trick to get things working in XP.

也许这是让事情在 XP 中工作的技巧的一部分。

回答by Martlark

Use spy++ to see what WM_COMMMAND message gets sent when you press F5 in windows explorer or find what menu message is used for view/refresh

使用 spy++ 查看在 Windows 资源管理器中按 F5 时发送的 WM_COMMMAND 消息或查找用于查看/刷新的菜单消息

Then use FindWindow to get the explorer window you want and send the WM_COMMAND recorded earlier etc message to it.

然后使用 FindWindow 获取您想要的资源管理器窗口,并将之前记录的 WM_COMMAND 等消息发送给它。

This is a fun way to control all sorts of Windows programs.

这是控制各种 Windows 程序的有趣方式。

回答by Tamer Mash

You can also send a WM_KEYDOWN message with the F5 keycode to all open explorer windows. This is a bit of a hack though.

您还可以将带有 F5 键码的 WM_KEYDOWN 消息发送到所有打开的资源管理器窗口。虽然这有点黑客。