C++实现Windows的运行(Win+R)

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

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

#include 
#include 
int main()
{
    system ("notepad");
    return 0;
}

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

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

#include 
#include 
#include 
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; 
}

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

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

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

while(true)
{
...   //中间的程序(省略)
}

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

#include 
#include 
#include 
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++,c++,windows,c语言)