# 定时关机重启工具 **Repository Path**: user_gitee/TimedShutdown ## Basic Information - **Project Name**: 定时关机重启工具 - **Description**: 可为win10/win11电脑提供定时关机或重启服务;可一键清理内存。 - **Primary Language**: C++ - **License**: AGPL-3.0 - **Default Branch**: master - **Homepage**: https://gitee.com/user_gitee/TimedShutdown - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-07-10 - **Last Updated**: 2026-07-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: 定时关机, 定时重启, 清理内存 ## README # 定时关机/重启程序 (TimedShutdown) > **版本**: 2.0.6.0 | **开发框架**: Qt6 / C++17 | **平台**: Windows x64 --- ## 项目概述 本程序是一款 Windows 定时关机/重启工具,支持以下核心功能: - **定时操作**:设定指定时间自动执行关机或重启 - **立即操作**:一键立即关机或重启(含进程清理) - **一键清理**:终止非系统进程以释放内存 - **系统托盘**:后台静默运行,支持托盘菜单 - **开机自启**:通过注册表实现开机自动启动(`--silent` 模式) - **单例运行**:通过 Windows Mutex 防止重复启动 - **操作日志**:实时显示日志并写入文件 --- ## 构建前提 | 依赖 | 版本要求 | |------|----------| | Qt | 6.x(Core, Widgets, Gui 模块) | | 编译器 | MinGW 11+ 或 MSVC 2022 | | 构建工具 | qmake(推荐 Qt Creator 或命令行) | | 操作系统 | Windows 10/11 x64 | --- ## 构建与运行 ### 使用 Qt Creator 1. 打开 `TimedShutdown.pro` 项目文件 2. 选择 **Release** 配置 3. 构建并运行 ### 命令行构建 ```bash # 生成 Makefile(Release) qmake TimedShutdown.pro -r -spec win32-g++ "CONFIG+=release" # 编译 mingw32-make -j$(nproc) # 生成的可执行文件位于 release/TimedShutdown.exe ``` ### 运行参数 | 参数 | 说明 | |------|------| | `--silent` | 静默模式启动,不显示主窗口(用于开机自启) | --- ## 项目结构 ``` demo22/ ├── TimedShutdown.pro # Qt 项目文件(qmake) │ ├── src/ # 源代码文件 │ ├── main.cpp # 程序入口 + 单例检查 │ ├── mainwindow.cpp # 主窗口实现 │ ├── switchbutton.cpp # 自定义开关控件实现 │ ├── logging.cpp # 日志模块实现 │ ├── systemoperations.cpp # 系统关机/重启实现 │ └── processmanager.cpp # 进程管理实现 │ ├── include/ # 头文件 │ ├── config.h # 全局常量、枚举、结构体定义 │ ├── mainwindow.h # 主窗口类声明 │ ├── doublespinbox.h # 自定义两位数 QSpinBox │ ├── switchbutton.h # 自定义开关控件声明 │ ├── logging.h # 日志模块声明 │ ├── systemoperations.h # 系统操作声明 │ └── processmanager.h # 进程管理声明 │ ├── resources/ # 资源文件 │ ├── resources.qrc # Qt 资源文件 │ ├── app.ico # 应用程序图标 │ └── version.rc # Windows 版本信息 │ ├── pickage/ # 打包部署目录(运行时分发) │ ├── docs/ # 文档 │ └── README.md # 开发文档(本文件) │ ├── debug/ # Debug 构建输出(自动生成) ├── release/ # Release 构建输出(自动生成) │ └── config.ini # 运行时配置文件(自动生成) ``` --- ## 模块说明 ### 1. 入口模块 (`src/main.cpp`) **职责**: - Windows Mutex 单例检测(`CheckAndActivateExistingInstance()`) - 解析命令行参数(`--silent`) - 初始化日志系统 - 启动 `QApplication` 和 `MainWindow` **核心逻辑**: ``` main() ├─ CheckAndActivateExistingInstance() → 已有实例则激活并退出 ├─ QApplication 初始化 ├─ 解析 --silent 参数 ├─ initLogFile() ├─ MainWindow 构造 └─ a.exec() ``` ### 2. 主窗口模块 (`mainwindow.h/.cpp`) **类**: `MainWindow : public QMainWindow, public QAbstractNativeEventFilter` **职责**: - 构建完整的 GUI 界面(时间选择器、按钮组、日志框) - 管理定时任务的调度和执行 - 系统托盘图标与菜单 - 配置文件读写(`config.ini`) - 模式切换(关机/重启) **关键方法**: | 方法 | 说明 | |------|------| | `setupUI()` | 初始化界面布局和样式 | | `createTrayIcon()` | 创建系统托盘图标和菜单 | | `loadSettings()` / `saveSettings()` | 读写 `config.ini` | | `onTimerCheck()` | 每分钟检查定时条件是否触发 | | `performOperationWithCleanup()` | 清理进程后执行关机/重启 | | `bringToFront()` | 将窗口前置激活 | **信号/槽流程**: ``` 用户点击"定时关机" → onTimedOperation() → 记录定时时间,saveSettings() → checkTimer 每分钟触发 onTimerCheck() → 时间匹配时弹出确认框 → 用户确认 → performOperationWithCleanup() → 超时 → 自动 performOperationWithCleanup() ``` ### 3. 开关控件模块 (`switchbutton.h/.cpp`) **类**: `SwitchButton : public QPushButton` **职责**:仿 Android 风格滑动开关,带圆点滑动动画。 **动画流程**: ``` setChecked() → startAnimation() 驱动 circleOffset → paintEvent() 根据 offset 绘制圆点位置 ``` ### 4. 数字输入框模块 (`doublespinbox.h`) **类**: `DoubleSpinBox : public QSpinBox` **职责**:始终显示两位数字(如 00, 01, …, 23),支持循环滚动。 ### 5. 系统操作模块 (`systemoperations.h/.cpp`) **函数**: - `SetShutdownPriority()` — 设置进程关闭优先级 - `PerformForceShutdown()` — 强制关机(多级降级策略) - `PerformForceReboot()` — 强制重启(多级降级策略) **降级策略**(依次尝试): 1. `ExitWindowsEx` + 提权 2. `InitiateShutdown` 3. `system("shutdown /s /f /t 0")` ### 6. 进程管理模块 (`processmanager.h/.cpp`) **结构体**: `MemInfo` — 内存信息(总内存、清理前/后空闲、释放量、百分比) **函数**: `killNonSystemProcesses()` — 遍历进程快照,排除系统进程后逐个终止 **排除列表** (`excludedProcesses`): 系统关键进程:`explorer.exe`, `svchost.exe`, `csrss.exe`, `winlogon.exe`, `services.exe`, `lsass.exe`, …(共 24 个) ### 7. 日志模块 (`logging.h/.cpp`) **全局变量**(定义在 `src/main.cpp`): | 变量 | 类型 | 说明 | |------|------|------| | `logFile` | `QFile*` | 日志文件指针 | | `logStream` | `QTextStream*` | 日志输出流 | **函数**: - `logMessage(msg, textEdit)` — 写入时间戳消息到控件和文件 - `initLogFile()` — 在 `日志/` 目录创建带时间戳的文件 - `closeLogFile()` — 关闭并清理日志文件 --- ## 配置文件 (`config.ini`) 运行时自动生成在可执行文件同级目录下。 ```ini [General] ShutdownHour=22 # 定时小时 (0-23) ShutdownMinute=0 # 定时分钟 (0-59) AutoStart=false # 开机自启 ScheduledActive=false # 定时任务是否激活 ScheduledHour=22 # 已设定的定时小时 ScheduledMinute=0 # 已设定的定时分钟 WorkMode=0 # 0=关机, 1=重启 ``` --- ## 类层次结构 ``` QObject ├─ QWidget │ ├─ QPushButton → SwitchButton │ ├─ QSpinBox → DoubleSpinBox │ └─ QMainWindow → MainWindow (implements QAbstractNativeEventFilter) └─ QTimer (MainWindow 成员: checkTimer, delayedShutdownTimer) QSystemTrayIcon (MainWindow 成员) QMenu (MainWindow 成员) ``` --- ## 开发者指南 ### 添加新功能 1. **新增 UI 控件**:在 `MainWindow::setupUI()` 中添加控件和布局 2. **新增槽函数**:在 `mainwindow.h` 中声明 `private slots`,在 `mainwindow.cpp` 中实现 3. **新增数据持久化**:在 `loadSettings()` / `saveSettings()` 中添加对应字段 4. **新增自定义控件**:创建 `include/xxx.h` + `src/xxx.cpp`,在 `TimedShutdown.pro` 中添加文件 ### 扩展进程排除列表 编辑 `src/processmanager.cpp` 中的 `excludedProcesses` 列表。 ### 添加新的执行模式 1. 在 `include/config.h` 的 `WorkMode` 枚举中添加新值 2. 在 `MainWindow` 中处理模式切换逻辑 3. 在 `systemoperations` 中实现对应的系统操作函数 ### 构建配置 `TimedShutdown.pro` 中可调整: - `CONFIG += debug` / `release` — 构建类型 - `TARGET` — 输出文件名 - `DEFINES += _WIN32_WINNT=0x0601` — Windows 目标版本 --- ## 依赖关系图 ``` main.cpp ├── include/config.h (全局常量和类型) ├── include/logging.h (日志) │ └── include/config.h ├── include/mainwindow.h (主窗口) │ ├── include/config.h │ ├── include/doublespinbox.h │ └── include/switchbutton.h │ mainwindow.cpp ├── include/mainwindow.h ├── include/logging.h ├── include/systemoperations.h └── include/processmanager.h └── include/config.h switchbutton.cpp → include/switchbutton.h logging.cpp → include/logging.h (+ config.h) systemoperations.cpp → include/systemoperations.h (+ logging.h) processmanager.cpp → include/processmanager.h (+ logging.h) ``` --- ## 许可信息 版权所有 © 2026 XAF Studio。保留所有权利。