我是靠谱客的博主 淡淡向日葵,这篇文章主要介绍C++实现Windows的运行(Win+R),现在分享给大家,希望可以做个参考。

本文章学习:
1、system函数调用
2、#include <windows.h>的添加
3、主程序

1、& 2、了解system函数的运用
system函数可以调用windows的程序,但必须先导入windows.h模块
示例代码

复制代码
1
2
3
4
5
6
7
8
#include <bits/stdc++.h> #include <windows.h> int main() { system ("notepad"); return 0; }

学过cmd的朋友都知道,在命令提示符中输入notepad可以打开记事本,在C++中,同样可以用system函数做到这点。
因此,只要我们把用户输入的各种可能用if语句写出来,就可以实现C++的运行程序了(Win+R打开运行窗口)

3、程序优化
目前,我们的程序代码还是这样的:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <bits/stdc++.h> #include <windows.h> #include <cmath> using namespace std; int main(void) { cout << "此程序为C++编写的Windows运行程序,与原版运行有着相似的功能. . ." <<endl; cout << "作者:Jerry"<<endl; cout << "请输入指令: "; string input; cin >> input; if(input == "cmd") { cout << "打开系统cmd"<<endl; system ("cmd.exe"); cout << "请输入指令: "; } if(input == "notepad") { cout << "打开记事本"<<endl; system ("notepad.exe"); cout << "请输入指令: "; } if(input == "mspaint") { cout << "打开画图"<<endl; system ("mspaint.exe"); cout << "请输入指令: "; } if(input == "shutdown") { cout << "警告: 系统即将关机, 是否继续, 继续按 Enter ,否则直接退出"<<endl; system ("pause"); system ("shutdown -s -t 60"); cout << "请输入指令: "; } if(input == "shutd_false") { cout << "此命令可取消定时关机"<<endl; system ("shutdown -a"); cout << "已取消关机"<<endl; cout << "请输入指令: "; } if(input == "winver") { cout << "查看系统版本"<<endl; system ("winver"); cout << "请输入指令: "; } if(input == "slmgr.vbs") { cout << "查看系统激活状态"<<endl; system ("slmgr.vbs -dlv"); cout << "请输入指令: "; } if(input == "calc") { cout << "打开计算器"<<endl; system ("calc"); cout << "请输入指令: "; } if(input == string("exit")) { cout << "感谢您的使用..." << endl; cout << "未经作者允许, 禁止转载"<<endl; cout << "2019 - 2022 Jerry. All Rights Reserved."<<endl; break; } if(input == "chkdsk") { cout << "磁盘扫描工具"<<endl; cout << "默认扫描C盘"<<endl; system ("chkdsk C:"); cout << "请输入指令: "; } if(input == "tree") { cout << "显示树形目录"<<endl; cout << "默认为C盘"<<endl; system ("tree C:"); cout << "请输入指令: "; } if(input == "dir") { cout << "显示所有文件"<<endl; cout << "默认参数为: dir /s /a"<<endl; system ("dir /s /a"); cout << "请输入指令: "; } if(input == "taskmgr") { cout << "打开任务管理器"<<endl; system ("taskmgr.exe"); cout << "请输入指令: "; } if(input == "explorer") { cout << "打开桌面资源管理器"; system ("explorer.exe"); cout << "请输入指令: "; } if(input == "systeminfo") { cout << "系统配置信息"<<endl; system ("systeminfo"); cout << "请输入指令: "; } if(input == "ping") { cout << "对IP地址发送数据包"<<endl; cout << "默认参数: ping 127.0.0.1 -t -l 65500"<<endl; system ("ping 127.0.0.1 -t -l 65500"); cout << "请输入指令: "; } return true; }

运行后可以发现,程序只让用户输入了一次,执行命令后就关闭了。。。

这怎么行呢?难道用户想执行多个程序还要重开吗?

我们只需要在主程序的适当位置加上

复制代码
1
2
3
4
5
while(true) { ... //中间的程序(省略) }

让用户重复输入命令
最终代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <bits/stdc++.h> #include <windows.h> #include <cmath> using namespace std; int main(void) { system ("title 命令提示符"); cout << "此程序为C++编写的Windows运行程序,与原版运行有着相似的功能. . ." <<endl; cout << "作者:Jerry"<<endl; cout << "请输入指令: "; while(true){ string input; cin >> input; if(input == "cmd") { cout << "打开系统cmd"<<endl; system ("cmd.exe"); cout << "请输入指令: "; } if(input == "notepad") { cout << "打开记事本"<<endl; system ("notepad.exe"); cout << "请输入指令: "; } if(input == "mspaint") { cout << "打开画图"<<endl; system ("mspaint.exe"); cout << "请输入指令: "; } if(input == "shutdown") { cout << "警告: 系统即将关机, 是否继续, 继续按 Enter ,否则直接退出"<<endl; system ("pause"); system ("shutdown -s -t 60"); cout << "请输入指令: "; } if(input == "shutd_false") { cout << "此命令可取消定时关机"<<endl; system ("shutdown -a"); cout << "已取消关机"<<endl; cout << "请输入指令: "; } if(input == "winver") { cout << "查看系统版本"<<endl; system ("winver"); cout << "请输入指令: "; } if(input == "slmgr.vbs") { cout << "查看系统激活状态"<<endl; system ("slmgr.vbs -dlv"); cout << "请输入指令: "; } if(input == "calc") { cout << "打开计算器"<<endl; system ("calc"); cout << "请输入指令: "; } if(input == string("exit")) { cout << "感谢您的使用..." << endl; cout << "未经作者允许, 禁止转载"<<endl; cout << "2019 - 2022 Jerry. All Rights Reserved."<<endl; break; } if(input == "chkdsk") { cout << "磁盘扫描工具"<<endl; cout << "默认扫描C盘"<<endl; system ("chkdsk C:"); cout << "请输入指令: "; } if(input == "tree") { cout << "显示树形目录"<<endl; cout << "默认为C盘"<<endl; system ("tree C:"); cout << "请输入指令: "; } if(input == "dir") { cout << "显示所有文件"<<endl; cout << "默认参数为: dir /s /a"<<endl; system ("dir /s /a"); cout << "请输入指令: "; } if(input == "taskmgr") { cout << "打开任务管理器"<<endl; system ("taskmgr.exe"); cout << "请输入指令: "; } if(input == "explorer") { cout << "打开桌面资源管理器"; system ("explorer.exe"); cout << "请输入指令: "; } if(input == "systeminfo") { cout << "系统配置信息"<<endl; system ("systeminfo"); cout << "请输入指令: "; } if(input == "ping") { cout << "对IP地址发送数据包"<<endl; cout << "默认参数: ping 127.0.0.1 -t -l 65500"<<endl; system ("ping 127.0.0.1 -t -l 65500"); cout << "请输入指令: "; } } return true; }

本文章原创,如需转载请备注作者名与原文地址!

最后

以上就是淡淡向日葵最近收集整理的关于C++实现Windows的运行(Win+R)的全部内容,更多相关C++实现Windows内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部