目录
功能描述
坦白了,我是懒人,欸嘿。
不想手动点开关机的那些步骤,那我写一个双击关机的C小程序吧。
延时3秒,弹窗倒计时关机
可以自己修改延时
代码内容
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
int main() {
int shutdownTime = 3; // 定义关机时间(秒)
printf("计算机将会在%d秒后关机。\n", shutdownTime);
printf("按下ESC键取消关机。\n");
while (shutdownTime > 0) {
Sleep(1000); // 暂停1S
if (_kbhit()) {
if (_getch() == 27) { // 27 是 ESC 键的 ASCII 码
printf("已取消关机。\n");
return 0;
}
}
printf("%d\n", shutdownTime);
shutdownTime--;
}
// 执行关机命令
system("shutdown /s /t 0");
return 0;
}
当然,这儿还有一个更简单,更直接的关机程序
注释掉延时,双击后电脑马上关机
#include <stdio.h>
#include <stdlib.h>
int main() {
int time;
printf("请输入时间(单位\秒):");
scanf("%d",&time);
printf("您的电脑将在%d秒内关机\n",time);
system("shutdown -s -t %d");
//如果想取消关机,可用如下方法
//system("shutdown -a");
return 0;
}