受欢迎的博客标签

diy Browser - WebView2Browser:Creating a WebView2 component with MFC c++(Tsking)

Published

https://mariusbancila.ro/blog/2020/01/29/using-microsoft-edge-in-a-native-windows-desktop-app-part-2/

 

WebView2Browser

A c++ web browser built with the Microsoft Edge WebView2 control.

step 1: fork from

https://github.com/MicrosoftEdge/WebView2Browser

 

step 2:change webview2 the user data folder

see:https://github.com/iaspnetcore/WebView2BrowserAppCplusplus/commit/317e687c80d349465db032eac2441e7298e34927

BOOL BrowserWindow::InitInstance(HINSTANCE hInstance, int nCmdShow) 
{
//L"C:\\Users\\Administrator2\\AppData\\Roaming\\Microsoft\\WebView2Browser"
    std::wstring userDataDirectory = GetAppDataDirectory();

 userDataDirectory.append(L"\\User Data2");

    // Create WebView environment for web content requested by the user. All
    // tabs will be created from this environment and kept isolated from the
    // browser UI. This enviroment is created first so the UI can request new
    // tabs when it's ready.
    HRESULT hr = CreateCoreWebView2EnvironmentWithOptions(nullptr, userDataDirectory.c_str(),
        nullptr, Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
            [this](HRESULT result, ICoreWebView2Environment* env) -> HRESULT
    {
        RETURN_IF_FAILED(result);

        m_contentEnv = env;
        HRESULT hr = InitUIWebViews();

        if (!SUCCEEDED(hr))
        {
            OutputDebugString(L"UI WebViews environment creation failed\n");
        }

        return hr;
    }).Get());

    if (!SUCCEEDED(hr))
    {
        OutputDebugString(L"Content WebViews environment creation failed\n");
        return FALSE;
    }

    return TRUE;
}

 

HRESULT hresult = m_webView2->Navigate
                             (L"https://ayushshyam.wixsite.com/perdesijaat");

        if (hresult == S_OK)
        {
            TRACE("Web Page Opened Successfully");
            ResizeEverything();
        }

 

Get started with WebView2 in Win32 apps (c++)

https://docs.microsoft.com/en-us/microsoft-edge/webview2/get-started/win32

 

WebView2 Edge Browser step by step in MFC C++ Application

https://www.codeproject.com/Tips/5287858/WebView2-Edge-Browser-in-MFC-Cplusplus-Application

 

Establish the communication between WebView2 and JavaScript by which you can send and receive the message via WebView2

https://www.codeproject.com/Articles/5295819/Send-and-Receive-Message-through-WebView2-to-from

 

 

MFC C++ 平台工具集

C++ 平台工具集 (Platform Toolset)” = 编译器版本

VS C++ Tools / 工作负载” = 整个开发环境(编译器 + SDK + 工具)

 

项目属性 → 常规 → 平台工具集

本质是: MSVC 编译器 + 标准库版本

v145(VS2026编译器)

v143(VS2022编译器)
v142(VS2019编译器)
v141(VS2017编译器)

 

VS C++ Tools
 ├── MSVC v143
 ├── Windows 10 SDK
 ├── MFC

 

[ VS C++ Tools ]
      ↓
[ 平台工具集 v143 ]
      ↓
[ Windows SDK ]
      ↓
[ 你的程序 ]

 

Windows API(头文件 + 库)= 你能“调用系统做什么”
MSVC 编译器 + 标准库 = 你“用什么语言规则把代码变成程序”

#include <windows.h>
#include <vector>

int main() {
    std::vector<int> v;
    MessageBox(NULL, L"Hi", L"Test", MB_OK);
}

MessageBox 是 Windows API

std::vector 来自 C++标准库,不是来自 Windows API

 

MSVC 编译器 + 标准库

本质是把 C++ 代码变成机器码

 

Windows SDK版本

c++平台工具集

c++语言标准

c语言标准

 

[ C/C++语言标准 ]   → 代码语法规则
         ↓
[ C++平台工具集 ]   → 编译器实现(MSVC版本)
         ↓
[ Windows SDK ]     → 操作系统API
         ↓
[ MFC ]             → 基于WinAPI的封装库

 

Windows SDK版本

能调用哪些 Windows API

支持哪个系统版本(Win10 / Win11)

 

C++ 平台工具集(编译器版本)

编译器版本(v143 / v142)

 

 

C++ 语言标准(语法规则)

决定写c++代码的语法

auto x = 1;        // C++11
std::optional<int> // C++17
std::ranges        // C++20

 

工具集      支持的C++ 语言标准     对应vs
v141          C++14/17             
v143          C++20/部分C++23        vs2022

 

vs→ 先选工具集 → 再选语言标

 

新的Windows SDK 可能用到新特性(inline、constexpr)老工具集可能编译不过。

所以建议:新 SDK + 新工具集

 

推荐组合(稳定 + 性能)

Windows SDK        = 10.0.22621.0
平台工具集         = v143
C++语言标准        = /std:c++20
C语言标准          = 默认 或 /std:c11
字符集             = Unicode