Skip to content

Commit 3364921

Browse files
committed
1. 程序状态管理器(查看状态 停止 启动 查看json)
2. 当资源管理器重启时,exe检测到没有窗口会弹窗提醒重启本程序
1 parent 566afbf commit 3364921

File tree

5 files changed

+71
-8
lines changed

5 files changed

+71
-8
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
## 编译和运行方法(手动版)
66

77
```cmd
8-
pyinstaller打包:
8+
pyinstaller打包:(不推荐)
99
pyinstaller.exe -F -c --noupx window_monitor.py
1010
11-
nuitka打包:知乎 nuitka:https://zhuanlan.zhihu.com/p/165978688
11+
nuitka打包:(推荐)
12+
知乎 nuitka:https://zhuanlan.zhihu.com/p/165978688
1213
nuitka --standalone --onefile --windows-disable-console window_monitor.py -o window_monitor_nogui.exe
1314
15+
1416
VirtualDesktop.dll编译:
1517
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:library /out:VirtualDesktop.dll VirtualDesktop.cs
1618
19+
1720
运行方式:
1821
pythonw window_monitor.py(不推荐)
1922
Start-Process -WindowStyle Hidden .\window_monitor.exe (文件名:run_with_new_process.ps1)(不推荐)

mk_release.bat

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ set newdir=releases\window_monitor_NEW
55
mkdir %newdir%
66

77
::复制文件
8-
cp _config.json %newdir%
9-
cp restore_windows.bat %newdir%
10-
cp restore_windows.py %newdir%
11-
cp VirtualDesktop.dll %newdir%
12-
cp window_monitor_nogui.exe %newdir%
8+
cp _config.json %newdir%
9+
cp restart_window_monitor.bat %newdir%
10+
cp restore_windows.bat %newdir%
11+
cp restore_windows.py %newdir%
12+
cp show_json_more.bat %newdir%
13+
cp VirtualDesktop.dll %newdir%
14+
cp window_monitor_nogui.exe %newdir%
1315

1416
echo finished.
1517
echo 复制到了 %newdir% 文件夹中

restart_window_monitor.bat

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
@echo off
3+
4+
set "script_dir=%~dp0"
5+
set "program_name=window_monitor_nogui.exe"
6+
7+
:loop
8+
echo 检查进程是否运行
9+
tasklist | find /i "window_monitor_nogui.exe"
10+
11+
IF %ERRORLEVEL% NEQ 0 (
12+
echo *** 程序未在运行 × ***
13+
) ELSE (
14+
echo *** 程序正在运行 √ ***
15+
)
16+
17+
echo.
18+
echo " 1. 退出循环(或输入 q d)"
19+
echo " 2. 结束 window_monitor_nogui.exe 程序"
20+
echo " 3. 启动 window_monitor_nogui.exe 程序"
21+
echo " 4. 查看 _program_history.json 文件前十行内容"
22+
echo.
23+
24+
REM 重置choice,强制用户输入新的选择
25+
set "choice=0"
26+
set /p choice=请输入指令:
27+
echo 输入的内容为【%choice%
28+
IF "%choice%"=="1" (
29+
goto end
30+
) ELSE IF "%choice%"=="d" (
31+
goto end
32+
) ELSE IF "%choice%"=="q" (
33+
goto end
34+
) ELSE IF "%choice%"=="2" (
35+
taskkill /f /im "%program_name%"
36+
) ELSE IF "%choice%"=="3" (
37+
start "" "%script_dir%%program_name%"
38+
) ELSE IF "%choice%"=="4" (
39+
start cmd /c "show_json_more.bat"
40+
) ELSE (
41+
echo 无效的指令,请重新输入
42+
)
43+
44+
goto loop
45+
46+
:end
47+
exit /b

show_json_more.bat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@echo off
2+
chcp 65001
3+
type "%script_dir%_program_history.json" | more
4+
chcp 936

window_monitor.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import psutil
55
import win32gui
66
import win32process
7+
import pymsgbox
78
# from subprocess import PIPE, Popen
89
# python调用命令行:https://zhuanlan.zhihu.com/p/329957363
910
import clr
@@ -60,7 +61,9 @@ def _get_all_hwnd_func(hwnd,mouse):
6061
# now_hwnd_all["arr"].append((desktop2, desktopname2, processId, hwnd, exename, _title))
6162
try:
6263
rc = VirtualDesktop.Desktop.FromDesktop(VirtualDesktop.Desktop.FromWindow(hwnd))
63-
except Exception:
64+
except Exception as e:
65+
# print("捕获到异常:", type(e)) # 打印异常类型
66+
# print("异常信息:", e) # 打印异常信息
6467
return
6568
desktop3, desktopname3 = str(rc), VirtualDesktop.Desktop.DesktopNameFromIndex(rc)
6669
thread, processId = win32process.GetWindowThreadProcessId(hwnd)
@@ -82,6 +85,8 @@ def update_hwnd_arr(return_value=False):
8285
win32gui.EnumWindows(_get_all_hwnd_func, 0)
8386
now_hwnd_all["arr"].sort(key=lambda x: (x[desktop_index],x[exe_index],x[hwnd_index]))
8487
now_hwnd_all["count"] = len(now_hwnd_all["arr"])
88+
if now_hwnd_all["count"] == 0:
89+
pymsgbox.alert("window_monitor 检测到窗口列表为空\n建议重启本程序", "window_monitor")
8590
if return_value:
8691
return now_hwnd_all
8792

@@ -189,4 +194,6 @@ def save_last_hwnd_all_to_history(a_last):
189194
time.sleep(seconds_per_loop/2)
190195
write_obj_to_json(all_history, program_history_backup_json)
191196
time.sleep(seconds_per_loop/2)
197+
# os.system("pause")
198+
dbg=1
192199
a=1

0 commit comments

Comments
 (0)