使用C++在浏览器中打开 HTML 文件

Open HTML file in browser using C++

本文关键字:HTML 文件 C++ 浏览器 使用      更新时间:2023-10-16

我正在使用Visual studio编写C++程序,我需要做的是创建一个HTML文件并在其中写入数据,然后我希望在浏览器中打开它。现在我可以创建文件,写东西,但我无法打开它,有人可以帮忙吗?

这可能是一个简单的问题,但我只是一个初学者。

#include <windows.h>
void main()
{  
   ShellExecute(NULL, "open", "http://dreamincode.net",
                NULL, NULL, SW_SHOWNORMAL);
}

http://www.dreamincode.net/code/snippet357.htm您只需将代码中显示的上述 URL 替换为 html 文件的绝对路径即可。当然,这可以用变量来完成。

    void CAboutDlg::OnButton1()
{
    CString strDir;
    char buffer[255];
    GetCurrentDirectory(255, buffer);
    strDir = buffer;
    strDir.TrimRight("\");
    strDir += "\";
    strDir += _T("helpindex.html");
    if( 32 >= (int)ShellExecute( NULL, "open", strDir, NULL, NULL, SW_SHOWNORMAL))
    {
        AfxMessageBox("::ShellExecuteFailed"
             " to open this link!");
    }  
}

试试这个...它对我来说效果很好...

#include <windows.h>
void main()
{ 
    LPCTSTR helpFile = "chelphelpFile.html";
    ShellExecute(NULL, "open", helpFile, NULL, NULL, SW_SHOWNORMAL);
    system("PAUSE");
}

如果你正在为 UWP 进行开发:

    Windows::System::Launcher::LaunchUriAsync(ref new Uri("https://www.google.com"));