1
0

desktopicons_windows.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // ============================================================================
  2. //go:build windows && !service
  3. // desktopicons_windows.go — 隐藏/显示桌面图标(Windows)
  4. //
  5. // 修改 HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced 的
  6. // HideIcons 值(0=显示,1=隐藏),并广播 WM_SETTINGCHANGE 让资源管理器立即刷新,
  7. // 无需重启 explorer。
  8. // ============================================================================
  9. package main
  10. import (
  11. "fmt"
  12. "syscall"
  13. "unsafe"
  14. "golang.org/x/sys/windows/registry"
  15. )
  16. const (
  17. // desktopIconsRegPath 桌面图标显示设置所在的注册表键
  18. desktopIconsRegPath = `Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced`
  19. // WM_SETTINGCHANGE 系统广播消息:通知资源管理器设置已变更
  20. wmSettingChange = 0x001A
  21. )
  22. var (
  23. desktopUser32DLL = syscall.NewLazyDLL("user32.dll")
  24. procSendMessageTimeoutW = desktopUser32DLL.NewProc("SendMessageTimeoutW")
  25. procFindWindowExW = desktopUser32DLL.NewProc("FindWindowExW")
  26. )
  27. const (
  28. // 窗口显示状态
  29. swHide = 0
  30. swShow = 5
  31. // 窗口类名
  32. progmanClass = "Progman" // 桌面背景窗口
  33. workerWClass = "WorkerW" // 桌面图标宿主(Progman 与 WorkerW 之间嵌套)
  34. desktopViewClass = "SHELLDLL_DefView" // 桌面图标视图
  35. listViewClass = "SysListView32" // 桌面图标列表控件(DefView 的子窗口,仅图标)
  36. )
  37. // findDesktopIconView 查找桌面图标宿主窗口(SHELLDLL_DefView)。
  38. // 桌面结构:Progman 或其兄弟 WorkerW 下嵌套 SHELLDLL_DefView。
  39. // 找到返回窗口句柄,未找到返回 0。
  40. func findDesktopIconView() uintptr {
  41. // 1. Progman 下直接找
  42. progman, _, _ := procFindWindowExW.Call(0, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(progmanClass))), 0)
  43. if hwnd, _, _ := procFindWindowExW.Call(progman, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(desktopViewClass))), 0); hwnd != 0 {
  44. return hwnd
  45. }
  46. // 2. 遍历所有 WorkerW,找其下嵌套的 SHELLDLL_DefView
  47. var prev uintptr
  48. for {
  49. workerW, _, _ := procFindWindowExW.Call(0, prev, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(workerWClass))), 0)
  50. if workerW == 0 {
  51. break
  52. }
  53. prev = workerW
  54. if hwnd, _, _ := procFindWindowExW.Call(workerW, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(desktopViewClass))), 0); hwnd != 0 {
  55. return hwnd
  56. }
  57. }
  58. return 0
  59. }
  60. // applyDesktopIconWindowVisibility 直接设置桌面图标宿主窗口可见性(即时生效,无需重启 explorer)。
  61. // hide=true 隐藏图标,false 显示。返回是否找到并操作了图标窗口。
  62. func applyDesktopIconWindowVisibility(hide bool) bool {
  63. hwnd := findDesktopIconView()
  64. if hwnd == 0 {
  65. return false
  66. }
  67. cmd := uintptr(swShow)
  68. if hide {
  69. cmd = swHide
  70. }
  71. procShowWindow.Call(hwnd, cmd)
  72. return true
  73. }
  74. // findDesktopIconListView 查找桌面图标列表控件(SysListView32,SHELLDLL_DefView 的子窗口)。
  75. // 仅隐藏此控件 = 只隐藏图标,壁纸保留。找到返回句柄,未找到返回 0。
  76. func findDesktopIconListView() uintptr {
  77. view := findDesktopIconView()
  78. if view == 0 {
  79. return 0
  80. }
  81. hwnd, _, _ := procFindWindowExW.Call(view, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(listViewClass))), 0)
  82. return hwnd
  83. }
  84. // applyDesktopIconListViewVisibility 只设置桌面图标列表控件可见性(壁纸不受影响)。
  85. // hide=true 只隐藏图标,false 只显示图标。返回是否找到并操作了图标列表。
  86. func applyDesktopIconListViewVisibility(hide bool) bool {
  87. hwnd := findDesktopIconListView()
  88. if hwnd == 0 {
  89. return false
  90. }
  91. cmd := uintptr(swShow)
  92. if hide {
  93. cmd = swHide
  94. }
  95. procShowWindow.Call(hwnd, cmd)
  96. return true
  97. }
  98. // desktopIconsRegKey 打开(或创建)桌面图标注册表键
  99. func desktopIconsRegKey() (registry.Key, error) {
  100. return registry.OpenKey(registry.CURRENT_USER, desktopIconsRegPath, registry.QUERY_VALUE|registry.SET_VALUE)
  101. }
  102. // broadcastDesktopSettingChange 广播 WM_SETTINGCHANGE,让资源管理器立即应用桌面图标显隐。
  103. // SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"TraySettings", SMTO_ABORTIFHUNG, 5000, &result)
  104. func broadcastDesktopSettingChange() {
  105. msg, _ := syscall.UTF16PtrFromString("TraySettings")
  106. procSendMessageTimeoutW.Call(
  107. 0xFFFF, // HWND_BROADCAST
  108. uintptr(wmSettingChange),
  109. 0,
  110. uintptr(unsafe.Pointer(msg)),
  111. 0x0002, // SMTO_ABORTIFHUNG
  112. 5000,
  113. uintptr(unsafe.Pointer(new(uintptr))),
  114. )
  115. }
  116. // setDesktopIconsVisible 设置桌面图标是否显示(hide=false 显示 / hide=true 隐藏)。
  117. // 双管齐下保证立即生效且重启后保持:
  118. // 1. 写注册表 HideIcons(持久化,重启资源管理器/登录后仍生效);
  119. // 2. 直接控制桌面图标宿主窗口(SHELLDLL_DefView)可见性(即时生效,无需重启 explorer)。
  120. //
  121. // 返回操作结果描述。
  122. func setDesktopIconsVisible(hide bool) (string, error) {
  123. k, err := desktopIconsRegKey()
  124. if err != nil {
  125. return "", fmt.Errorf("打开桌面图标注册表失败: %w", err)
  126. }
  127. defer k.Close()
  128. value := uint32(0)
  129. state := "显示"
  130. if hide {
  131. value = 1
  132. state = "隐藏"
  133. }
  134. if err := k.SetDWordValue("HideIcons", value); err != nil {
  135. return "", fmt.Errorf("写入 HideIcons 失败: %w", err)
  136. }
  137. // 即时生效:直接隐藏/显示图标窗口(多数系统无需广播即可刷新)。
  138. // 找不到图标窗口时退回广播 WM_SETTINGCHANGE。
  139. if !applyDesktopIconWindowVisibility(hide) {
  140. broadcastDesktopSettingChange()
  141. }
  142. return "桌面图标已" + state, nil
  143. }
  144. // setDesktopIconOnlyVisible 只隐藏/显示桌面图标(壁纸保留)。
  145. // hide=true 只隐藏图标(桌面壁纸不受影响),false 恢复显示。
  146. // 通过隐藏图标列表控件 SysListView32 实现,壁纸由 Progman/WorkerW 背景层绘制,不受影响。
  147. // 返回操作结果描述。
  148. func setDesktopIconOnlyVisible(hide bool) (string, error) {
  149. k, err := desktopIconsRegKey()
  150. if err != nil {
  151. return "", fmt.Errorf("打开桌面图标注册表失败: %w", err)
  152. }
  153. defer k.Close()
  154. value := uint32(0)
  155. state := "显示"
  156. if hide {
  157. value = 1
  158. state = "隐藏"
  159. }
  160. if err := k.SetDWordValue("HideIcons", value); err != nil {
  161. return "", fmt.Errorf("写入 HideIcons 失败: %w", err)
  162. }
  163. // 只隐藏图标列表(壁纸保留);找不到则回退广播。
  164. if !applyDesktopIconListViewVisibility(hide) {
  165. broadcastDesktopSettingChange()
  166. }
  167. return "桌面图标已" + state + "(壁纸保留)", nil
  168. }