我正在用C ++编写一个程序,其中有一个Web链接,这样当我键入输入或只需按Enter键时,它会将我重定向到我输入的链接。 例如,如果我有3个选项并且我选择选项A,程序会将我重定向到选项A中的该链接。
这是一个样本: -
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
system("COLOR B1");
int choice;
cout << "Menu" << endl;
cout << "1. Pasta" << "\n";
cout << "2. Cold Drink" << "\n";
cout << "Your choice (1-2)" << "\n";
cin >> choice;
if(choice == 1)
{
cout << "Thanks" << "\n"; //Here i want a url and when i choose 1 it
//will direct me to that url
}
else if(choice == 2)
{
cout << "Thanks" << "\n"; // And here also...
}
else
{
return 0;
}
}
请帮忙。 感谢
答案 0 :(得分:2)
在Windows桌面程序中,您可以使用ShellExecute API和open
操作来使用默认应用程序(通常是Web浏览器)打开URL。
ShellExecute(NULL, L"open", L"https://example.com", nullptr, nullptr, SW_SHOWNORMAL);
商店应用根本无法使用ShellExecute
,但您可以使用UWP LaunchUriAsync。
task = Windows::System::Launcher::LaunchUriAsync(
ref new Windows::Foundation::Uri("https://example.com"));
其他各种平台通常都有自己的API。一般而言,您希望查找并使用它们,而不是假设路径上有任何特定的浏览器可执行文件阻止用户使用另一个或安装到非默认位置(或被更新等破坏)。