From fb5fdcbf143735664360eb6f057ba9cfca968b95 Mon Sep 17 00:00:00 2001 From: LZY <2217445143@qq.com> Date: Mon, 2 Feb 2026 16:00:21 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\345\256\236\351\252\214.py" | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 "\345\256\236\351\252\214.py" diff --git "a/\345\256\236\351\252\214.py" "b/\345\256\236\351\252\214.py" new file mode 100644 index 0000000..3dced11 --- /dev/null +++ "b/\345\256\236\351\252\214.py" @@ -0,0 +1,36 @@ +import ctypes + +# 常量 +WM_KEYDOWN = 0x0100 +WM_KEYUP = 0x0101 +VK_F5 = 0x74 + +user32 = ctypes.windll.user32 + +# 找 Progman(桌面顶层窗口) +progman = user32.FindWindowW("Progman", None) + +# 找桌面 DefView +defview = user32.FindWindowExW(progman, None, "SHELLDLL_DefView", None) + +# Win10/11 有时 DefView 在 WorkerW 下 +if not defview: + worker = None + while True: + worker = user32.FindWindowExW(None, worker, "WorkerW", None) + if not worker: + break + defview = user32.FindWindowExW(worker, None, "SHELLDLL_DefView", None) + if defview: + break + +# 找桌面图标列表窗口 +listview = user32.FindWindowExW(defview, None, "SysListView32", None) + +if listview: + # 发送 F5 消息刷新桌面 + user32.PostMessageW(listview, WM_KEYDOWN, VK_F5, 0) + user32.PostMessageW(listview, WM_KEYUP, VK_F5, 0) + print("桌面已刷新 ✅") +else: + print("找不到桌面图标窗口 ❌") -- Gitee From 77383d9911ae53b40ccf5f420053fc40b6e36e56 Mon Sep 17 00:00:00 2001 From: LZY <2217445143@qq.com> Date: Mon, 2 Feb 2026 16:49:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A1=8C=E9=9D=A2?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../refresh_desktop.py" | 38 ++++++++++--------- .../switch_desktop.py" | 27 +++++++------ 2 files changed, 35 insertions(+), 30 deletions(-) rename "\345\256\236\351\252\214.py" => "\346\241\214\351\235\242\345\210\207\346\215\242/refresh_desktop.py" (36%) diff --git "a/\345\256\236\351\252\214.py" "b/\346\241\214\351\235\242\345\210\207\346\215\242/refresh_desktop.py" similarity index 36% rename from "\345\256\236\351\252\214.py" rename to "\346\241\214\351\235\242\345\210\207\346\215\242/refresh_desktop.py" index 3dced11..00a4b58 100644 --- "a/\345\256\236\351\252\214.py" +++ "b/\346\241\214\351\235\242\345\210\207\346\215\242/refresh_desktop.py" @@ -1,20 +1,17 @@ import ctypes -# 常量 WM_KEYDOWN = 0x0100 WM_KEYUP = 0x0101 VK_F5 = 0x74 user32 = ctypes.windll.user32 -# 找 Progman(桌面顶层窗口) -progman = user32.FindWindowW("Progman", None) - -# 找桌面 DefView -defview = user32.FindWindowExW(progman, None, "SHELLDLL_DefView", None) - -# Win10/11 有时 DefView 在 WorkerW 下 -if not defview: +def refresh_desktop(): + """ + 刷新 Windows 桌面(模拟 F5)。 + 返回 True 表示刷新成功,False 表示未找到桌面窗口。 + """ + defview = None worker = None while True: worker = user32.FindWindowExW(None, worker, "WorkerW", None) @@ -24,13 +21,18 @@ if not defview: if defview: break -# 找桌面图标列表窗口 -listview = user32.FindWindowExW(defview, None, "SysListView32", None) + listview = user32.FindWindowExW(defview, None, "SysListView32", None) if defview else None + + if listview: + user32.PostMessageW(listview, WM_KEYDOWN, VK_F5, 0) + user32.PostMessageW(listview, WM_KEYUP, VK_F5, 0) + return True + else: + return False -if listview: - # 发送 F5 消息刷新桌面 - user32.PostMessageW(listview, WM_KEYDOWN, VK_F5, 0) - user32.PostMessageW(listview, WM_KEYUP, VK_F5, 0) - print("桌面已刷新 ✅") -else: - print("找不到桌面图标窗口 ❌") +# --------------------------------- +if __name__ == "__main__": + if refresh_desktop(): + print("桌面已刷新 ✅") + else: + print("找不到桌面图标窗口 ❌") diff --git "a/\346\241\214\351\235\242\345\210\207\346\215\242/switch_desktop.py" "b/\346\241\214\351\235\242\345\210\207\346\215\242/switch_desktop.py" index a6bc2a1..6ad212f 100644 --- "a/\346\241\214\351\235\242\345\210\207\346\215\242/switch_desktop.py" +++ "b/\346\241\214\351\235\242\345\210\207\346\215\242/switch_desktop.py" @@ -1,7 +1,10 @@ import ctypes import os from ctypes import wintypes +from refresh_desktop import refresh_desktop # 你之前封装的函数 +# ----------------------------- +# GUID 定义,用于桌面路径修改 class GUID(ctypes.Structure): _fields_ = [ ("Data1", wintypes.DWORD), @@ -20,8 +23,14 @@ FOLDERID_Desktop = GUID( ) ) +# ----------------------------- +# 修改桌面路径并刷新桌面 def switch_desktop_path(new_path: str) -> bool: - # 1️⃣ 先检测目录是否存在 + """ + 修改 Windows 桌面路径并刷新桌面 + 返回 True 表示成功,False 表示失败 + """ + # 1️⃣ 检查目录是否存在 if not os.path.isdir(new_path): return False @@ -34,25 +43,19 @@ def switch_desktop_path(new_path: str) -> bool: None, ctypes.c_wchar_p(new_path) ) - if hr != 0: return False - # 3️⃣ 刷新桌面 - shell32.SHChangeNotify( - 0x8000000, # SHCNE_ASSOCCHANGED - 0x0000, - None, - None - ) - + # 3️⃣ 刷新桌面(快速刷新) + refresh_desktop() return True - +# ----------------------------- +# 脚本直接运行示例 if __name__ == "__main__": new_path = r"C:\Users\lqvsy\Desktop" if switch_desktop_path(new_path): - print("✅ 桌面切换完成,右键新建文件夹可用") + print("✅ 桌面切换完成并刷新,右键新建文件夹可用") else: print("❌ 桌面切换失败(目录不存在或设置失败)") -- Gitee