Change and refresh icons programmatically (without rebooting)

江枫思渺然 提交于 2019-12-03 15:12:43

By associating files, the system refreshes the icons, so my simple solution was just this:

ASSOC .anyextension=anytype
ASSOC .anyextension=

Second row disassociates the extension.

This page has a VB program that rebuilds the cache (look at frmRebuiltIconCache.frm for the actual code)

What it does is to examine the value of HKCU\Control Panel\Desktop\WindowMetrics\Shell Icon Size, subtract 1 from it, forces a timeout via SendMessageTimeout, then resets the value, and repeats the timeout

due to the fact you cannot call a DLL from vbs (without an external program), this may not give you what you need. I have pared down the program to this:

'SendMessageTimeout values
Const HWND_BROADCAST = &HFFFF
Const WM_SETTINGCHANGE = &H1A
Const SPI_SETNONCLIENTMETRICS = &H2A
Const SMTO_ABORTIFHUNG = &H2

'Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" _
'  (ByVal hwnd As Long, ByVal msg As Long, _
'  ByVal wParam As Long, ByVal lParam As Long, _
'  ByVal fuFlags As Long, ByVal uTimeout As Long, _
'  lpdwResult As Long) As Long

Dim icon_size
Dim new_icon_size
Dim result
Dim SysVarReg
Set SysVarReg = WScript.CreateObject("WScript.Shell")
    ' Get the current icon size.
icon_size = SysVarReg.RegRead ("HKCU\Control Panel\Desktop\WindowMetrics\Shell Icon Size")
    ' Increase the value by 1.
new_icon_size = CInt(icon_size) + 1
SysVarReg.RegWrite "HKCU\Control Panel\Desktop\WindowMetrics\Shell Icon Size", new_icon_size
    ' Send HWND_BROADCAST to refresh the icons.
SendMessageTimeout HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETNONCLIENTMETRICS, 0, SMTO_ABORTIFHUNG, 10000, result
    ' Restore the original value.
SysVarReg.RegWrite "HKCU\Control Panel\Desktop\WindowMetrics\Shell Icon Size", icon_size
    ' Send HWND_BROADCAST to refresh the icons again.
SendMessageTimeout HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETNONCLIENTMETRICS, 0, SMTO_ABORTIFHUNG, 10000, result

but without something to wrap the DLL call in, you will be unable to do this via vbs

' It work in XP.

' The following is "VBA" Module.

' ---------------------------------------------------------

'SendMessageTimeout values
Const HWND_BROADCAST = &HFFFF
Const WM_SETTINGCHANGE = &H1A
Const SPI_SETNONCLIENTMETRICS = &H2A
Const SMTO_ABORTIFHUNG = &H2

Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" _
  (ByVal hwnd As Long, ByVal msg As Long, _
  ByVal wParam As Long, ByVal lParam As Long, _
  ByVal fuFlags As Long, ByVal uTimeout As Long, _
  lpdwResult As Long) As Long


Sub Rebuild_Icon_cache()

Dim icon_size
Dim new_icon_size
Dim result
Dim SysVarReg

Set SysVarReg = CreateObject("WScript.Shell")

' Get the current icon size.
icon_size = SysVarReg.RegRead("HKCU\Control Panel\Desktop\WindowMetrics\Shell Icon Size")

' Increase the value by 1.
new_icon_size = CInt(icon_size) + 1
SysVarReg.RegWrite "HKCU\Control Panel\Desktop\WindowMetrics\Shell Icon Size", new_icon_size

' Send HWND_BROADCAST to refresh the icons.
SendMessageTimeout HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETNONCLIENTMETRICS, 0, SMTO_ABORTIFHUNG, 10000, result

' Restore the original value.
SysVarReg.RegWrite "HKCU\Control Panel\Desktop\WindowMetrics\Shell Icon Size", icon_size

' Send HWND_BROADCAST to refresh the icons again.
SendMessageTimeout HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETNONCLIENTMETRICS, 0, SMTO_ABORTIFHUNG, 10000, result

End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!