用C++实现windows下的关机操作

前题:

  在linux下面,有很多命令,键盘操作非常方便;其实windows下面也有少许的命名,可以经过DOS界面操作:点击左下角的开始,输入CMD,然后enter,输入相关DOS命令即可。

  因为学习C++,就自己写了个小程序,简化这个敲命令的过程。当然,灵感也来源于网络上的一篇文章,链接如下:

http://www.oschina.net/code/snippet_1021299_19727

我参考了下他的思路,然后我自己实现了遍,适合编程新手练习打字的速度。下面是我的代码:

 1 #ifndef _DOS_H_

 2 #define _DOS_H_

 3 

 4 class Dos {

 5 public:

 6     Dos();

 7     ~Dos();

 8 

 9     void display();

10     void Operation_handle();

11     void shutdown();

12     void reboot();

13     void logout();

14     void abolish();

15     void dormancy();

16     void lock();

17 

18 };

19 

20 

21 #endif //_DOS_H_

接着就是该类中的函数的实现,代码如下:

  1 #include <iostream>

  2 #include <cstdlib>

  3 #include "dos.h"

  4 using namespace std;

  5 

  6 //构造和析构函数,在本程序中其实可以采取系统默认的即可

  7 Dos::Dos(){}

  8 Dos::~Dos(){}

  9 

 10 //界面信息显示函数,

 11 void Dos::display()

 12 {

 13     cout <<"================欢迎你使用该DOS工具=============================="<<endl;

 14     cout <<"||              version 1,by jason                              ||"<<endl;

 15     cout <<"================================================================="<<endl;

 16     cout <<endl;

 17     cout << "1、关机"<<endl;

 18     cout << "2、重启"<<endl;

 19     cout << "3、注销"<<endl;

 20     cout << "4、休眠"<<endl;

 21     cout << "5、锁定"<<endl;

 22     cout << "6、取消<关机/重启>"<<endl;

 23     cout << "请选择:"<<endl;

 24 }

 25 

 26 //接口操作函数,各个功能函数和用户的交替,就靠这个函数完成

 27 void Dos::Operation_handle()

 28 {

 29     int number;

 30     cin >> number;

 31     switch(number)

 32     {

 33         case 1: shutdown();break;

 34         case 2: reboot();break;

 35         case 3: logout();break;

 36         case 4: dormancy();break;

 37         case 5: lock();break;

 38         case 6: abolish();break;

 39         default:break;

 40     }

 41     

 42 }

 43 

 44 //关机函数

 45 void Dos::shutdown()

 46 {

 47     char ch;

 48     cout <<"请在关机前保存好文件,确认Y,取消N:"<<endl;

 49 

 50 shut_lable:

 51     cin >> ch;

 52     if(ch == 'Y' || ch == 'y')

 53     {

 54         system("shutdown -s");

 55     

 56     }else if(ch == 'N' || ch == 'n')

 57     {

 58         cout <<"你取消了,计算机将不会给关闭"<<endl;

 59         cout <<"================非常感谢使用该DOS工具============================="<<endl;

 60         cout <<"||                   bye-bye                                    ||"<<endl;

 61         cout <<"================================================================="<<endl;

 62     }else

 63     {

 64         cout <<"输入错误,请重新输入:"<<endl;

 65         goto shut_lable;

 66     }

 67 

 68 }

 69 

 70 //系统重启函数

 71 void Dos::reboot()

 72 {

 73     char ch;

 74     cout << "请在重启前保存好文件,确认Y/y,取消N/n:"<<endl;

 75 

 76 re_lable:

 77     cin >> ch;

 78     if(ch == 'Y' || ch == 'y')

 79     {

 80         system("shutdown -r");

 81     }else if(ch == 'N' || ch == 'n')

 82     {

 83         cout <<"你取消了,计算机将不会被重启"<<endl;

 84         cout <<"================非常感谢使用该DOS工具============================="<<endl;

 85         cout <<"||                   bye-bye                                    ||"<<endl;

 86         cout <<"================================================================="<<endl;

 87     }else

 88     {

 89         cout << "输入错误,请重新输入:"<<endl;

 90         goto re_lable;

 91     }

 92 

 93 }

 94 

 95 //系统登出(注销)函数

 96 void Dos::logout()

 97 {

 98     char ch;

 99     cout <<"注销前请注意保存好文件,确认Y/y,取消N/n:"<<endl;

100 

101 lo_lable:

102     cin >>ch;

103     if(ch == 'Y' || ch == 'y')

104     {

105         system("shutdown -l");

106     }else if(ch == 'N' || ch == 'n')

107     {

108         cout <<"你取消了,计算机将不会登出"<<endl;

109         cout <<"================非常感谢使用该DOS工具============================="<<endl;

110         cout <<"||                   bye-bye                                    ||"<<endl;

111         cout <<"================================================================="<<endl;

112     }else

113     {

114         cout <<"输入错误,请重新输入:"<<endl;

115         goto lo_lable;

116     }

117 }

118 

119 //系统休眠函数

120 void Dos::dormancy()

121 {

122     cout << "你的计算机即将进入休眠状态:"<<endl;

123     system("shutdown -h");

124 }

125 

126 //系统锁定函数

127 void Dos::lock()

128 {

129     cout <<"你的计算机即将被锁定:"<<endl;

130     system("rundll32.exe user32.dll,LockWorkStation");

131     cout <<"锁定完成 !"<<endl;

132 }

133 

134 //取消系统关机或者重启函数

135 void Dos::abolish()

136 {

137     cout <<"你取消了"<<endl;

138     cout <<"================非常感谢使用该DOS工具============================="<<endl;

139     cout <<"||                   bye-bye                                    ||"<<endl;

140     cout <<"================================================================="<<endl;

141     system("shutdown -a");

142 }

接下来就是main函数,或者说测试文件了,代码如下:

1 #include "dos.h"

2 int main()

3 {

4     Dos dos;

5     dos.display();

6     dos.Operation_handle();

7     return 0;

8 }

后记:

  DOS 界面上,比如想120S才关机,那么输入格式是  shutdown -s -t 120,我上面的代码没有实现这个时间控制。

你可能感兴趣的:(windows)