| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- // ============================================================================
- //go:build windows && !service
- // desktopicons_windows.go — 隐藏/显示桌面图标(Windows)
- //
- // 修改 HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced 的
- // HideIcons 值(0=显示,1=隐藏),并广播 WM_SETTINGCHANGE 让资源管理器立即刷新,
- // 无需重启 explorer。
- // ============================================================================
- package main
- import (
- "fmt"
- "syscall"
- "unsafe"
- "golang.org/x/sys/windows/registry"
- )
- const (
- // desktopIconsRegPath 桌面图标显示设置所在的注册表键
- desktopIconsRegPath = `Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced`
- // WM_SETTINGCHANGE 系统广播消息:通知资源管理器设置已变更
- wmSettingChange = 0x001A
- )
- var (
- desktopUser32DLL = syscall.NewLazyDLL("user32.dll")
- procSendMessageTimeoutW = desktopUser32DLL.NewProc("SendMessageTimeoutW")
- procFindWindowExW = desktopUser32DLL.NewProc("FindWindowExW")
- )
- const (
- // 窗口显示状态
- swHide = 0
- swShow = 5
- // 窗口类名
- progmanClass = "Progman" // 桌面背景窗口
- workerWClass = "WorkerW" // 桌面图标宿主(Progman 与 WorkerW 之间嵌套)
- desktopViewClass = "SHELLDLL_DefView" // 桌面图标视图
- listViewClass = "SysListView32" // 桌面图标列表控件(DefView 的子窗口,仅图标)
- )
- // findDesktopIconView 查找桌面图标宿主窗口(SHELLDLL_DefView)。
- // 桌面结构:Progman 或其兄弟 WorkerW 下嵌套 SHELLDLL_DefView。
- // 找到返回窗口句柄,未找到返回 0。
- func findDesktopIconView() uintptr {
- // 1. Progman 下直接找
- progman, _, _ := procFindWindowExW.Call(0, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(progmanClass))), 0)
- if hwnd, _, _ := procFindWindowExW.Call(progman, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(desktopViewClass))), 0); hwnd != 0 {
- return hwnd
- }
- // 2. 遍历所有 WorkerW,找其下嵌套的 SHELLDLL_DefView
- var prev uintptr
- for {
- workerW, _, _ := procFindWindowExW.Call(0, prev, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(workerWClass))), 0)
- if workerW == 0 {
- break
- }
- prev = workerW
- if hwnd, _, _ := procFindWindowExW.Call(workerW, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(desktopViewClass))), 0); hwnd != 0 {
- return hwnd
- }
- }
- return 0
- }
- // applyDesktopIconWindowVisibility 直接设置桌面图标宿主窗口可见性(即时生效,无需重启 explorer)。
- // hide=true 隐藏图标,false 显示。返回是否找到并操作了图标窗口。
- func applyDesktopIconWindowVisibility(hide bool) bool {
- hwnd := findDesktopIconView()
- if hwnd == 0 {
- return false
- }
- cmd := uintptr(swShow)
- if hide {
- cmd = swHide
- }
- procShowWindow.Call(hwnd, cmd)
- return true
- }
- // findDesktopIconListView 查找桌面图标列表控件(SysListView32,SHELLDLL_DefView 的子窗口)。
- // 仅隐藏此控件 = 只隐藏图标,壁纸保留。找到返回句柄,未找到返回 0。
- func findDesktopIconListView() uintptr {
- view := findDesktopIconView()
- if view == 0 {
- return 0
- }
- hwnd, _, _ := procFindWindowExW.Call(view, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(listViewClass))), 0)
- return hwnd
- }
- // applyDesktopIconListViewVisibility 只设置桌面图标列表控件可见性(壁纸不受影响)。
- // hide=true 只隐藏图标,false 只显示图标。返回是否找到并操作了图标列表。
- func applyDesktopIconListViewVisibility(hide bool) bool {
- hwnd := findDesktopIconListView()
- if hwnd == 0 {
- return false
- }
- cmd := uintptr(swShow)
- if hide {
- cmd = swHide
- }
- procShowWindow.Call(hwnd, cmd)
- return true
- }
- // desktopIconsRegKey 打开(或创建)桌面图标注册表键
- func desktopIconsRegKey() (registry.Key, error) {
- return registry.OpenKey(registry.CURRENT_USER, desktopIconsRegPath, registry.QUERY_VALUE|registry.SET_VALUE)
- }
- // broadcastDesktopSettingChange 广播 WM_SETTINGCHANGE,让资源管理器立即应用桌面图标显隐。
- // SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"TraySettings", SMTO_ABORTIFHUNG, 5000, &result)
- func broadcastDesktopSettingChange() {
- msg, _ := syscall.UTF16PtrFromString("TraySettings")
- procSendMessageTimeoutW.Call(
- 0xFFFF, // HWND_BROADCAST
- uintptr(wmSettingChange),
- 0,
- uintptr(unsafe.Pointer(msg)),
- 0x0002, // SMTO_ABORTIFHUNG
- 5000,
- uintptr(unsafe.Pointer(new(uintptr))),
- )
- }
- // setDesktopIconsVisible 设置桌面图标是否显示(hide=false 显示 / hide=true 隐藏)。
- // 双管齐下保证立即生效且重启后保持:
- // 1. 写注册表 HideIcons(持久化,重启资源管理器/登录后仍生效);
- // 2. 直接控制桌面图标宿主窗口(SHELLDLL_DefView)可见性(即时生效,无需重启 explorer)。
- //
- // 返回操作结果描述。
- func setDesktopIconsVisible(hide bool) (string, error) {
- k, err := desktopIconsRegKey()
- if err != nil {
- return "", fmt.Errorf("打开桌面图标注册表失败: %w", err)
- }
- defer k.Close()
- value := uint32(0)
- state := "显示"
- if hide {
- value = 1
- state = "隐藏"
- }
- if err := k.SetDWordValue("HideIcons", value); err != nil {
- return "", fmt.Errorf("写入 HideIcons 失败: %w", err)
- }
- // 即时生效:直接隐藏/显示图标窗口(多数系统无需广播即可刷新)。
- // 找不到图标窗口时退回广播 WM_SETTINGCHANGE。
- if !applyDesktopIconWindowVisibility(hide) {
- broadcastDesktopSettingChange()
- }
- return "桌面图标已" + state, nil
- }
- // setDesktopIconOnlyVisible 只隐藏/显示桌面图标(壁纸保留)。
- // hide=true 只隐藏图标(桌面壁纸不受影响),false 恢复显示。
- // 通过隐藏图标列表控件 SysListView32 实现,壁纸由 Progman/WorkerW 背景层绘制,不受影响。
- // 返回操作结果描述。
- func setDesktopIconOnlyVisible(hide bool) (string, error) {
- k, err := desktopIconsRegKey()
- if err != nil {
- return "", fmt.Errorf("打开桌面图标注册表失败: %w", err)
- }
- defer k.Close()
- value := uint32(0)
- state := "显示"
- if hide {
- value = 1
- state = "隐藏"
- }
- if err := k.SetDWordValue("HideIcons", value); err != nil {
- return "", fmt.Errorf("写入 HideIcons 失败: %w", err)
- }
- // 只隐藏图标列表(壁纸保留);找不到则回退广播。
- if !applyDesktopIconListViewVisibility(hide) {
- broadcastDesktopSettingChange()
- }
- return "桌面图标已" + state + "(壁纸保留)", nil
- }
|