最近项目需要集成WebView,于时尝试引入一下webView2。
官方文档实在是一言难尽。
对于C++使用NuGet集成包的步骤有缺失,而且NuGet中发布的webView2包还存在错误!
从头来一遍
开始前先下一个beta版的edge https://www.microsoftedgeinsider.com/zh-cn/
首先 创建一个新的win32窗口项目
image
image
添加选择管理NuGet程序包
image
注意 如果是从方案级别进行包管理 要勾选项目,项目级别则不需要
image
image
接下来直接选择安装
这里使用官方的例子还需要安装一个 Microsoft.Windows.ImplementationLibrary
如果这里按照官方的教程就已经集成完毕了,接下来就是直接 #include "WebView2.h"
然而
image
由于Nuget并不是为了C++写的包管理工具。因此C++项目集成时并不能自动将引用文件添加进去。需要手动指定引用文件
添加target文件引用
实际上nuget包的中头文件、可执行文件的信息都在包中的.target文件内,只需要将这些文件添加到项目中就可以了
右键项目选择添加自定义
image
选择添加现有项目,并找到你的目录下的package中native下的target文件
分别手动添加两个包的target文件并勾选
image
image
运行官方例子
#include "framework.h"
// C 运行时头文件
#include
#include
#include
#include
#include
#include
// include WebView2 header
#include "WebView2.h"
using namespace Microsoft::WRL;
// Global variables
// The main window class name.
static TCHAR szWindowClass[] = _T("DesktopApp");
// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("cppnugetwebview");
HINSTANCE hInst;
// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
// Pointer to WebViewController
static wil::com_ptr webviewController;
// Pointer to WebView window
static wil::com_ptr webviewWindow;
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
// Store instance handle in our global variable
hInst = hInstance;
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
1200, 900,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
nCmdShow);
UpdateWindow(hWnd);
// <-- WebView2 sample code starts here -->
HRESULT res = CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr,
Callback(
[hWnd](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {
//MessageBoxA(hWnd, "createView", "", NULL);
// Create a CoreWebView2Controller and get the associated CoreWebView2 whose parent is the main window hWnd
env->CreateCoreWebView2Controller(hWnd, Callback(
[hWnd](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
if (controller != nullptr) {
webviewController = controller;
webviewController->get_CoreWebView2(&webviewWindow);
}
// Add a few settings for the webview
// The demo step is redundant since the values are the default settings
ICoreWebView2Settings* Settings;
webviewWindow->get_Settings(&Settings);
Settings->put_IsScriptEnabled(TRUE);
Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
Settings->put_IsWebMessageEnabled(TRUE);
// Resize WebView to fit the bounds of the parent window
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewController->put_Bounds(bounds);
// Schedule an async task to navigate to Bing
HRESULT res = webviewWindow->Navigate(L"https:\\www.baidu.com");
std::string sres = std::to_string(res).c_str();
// Step 4 - Navigation events
// Step 5 - Scripting
// Step 6 - Communication between host and web content
return S_OK;
}).Get());
return S_OK;
}).Get());
// <-- WebView2 sample code ends here -->
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_DESTROY - post a quit message and return
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
TCHAR greeting[] = _T("Hello, Windows desktop!");
switch (message)
{
case WM_SIZE:
if (webviewController != nullptr) {
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewController->put_Bounds(bounds);
};
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
注意 官方的WebView2.1.0.818.41是有问题的 (之后版本已解决)
这个是微软提交包的错误,并不在集成流程中
到你的项目文件夹中
找到package下webview的路径
image
找到common.targets文件打开找到以下三个位置
image
微软发布的包中这个三个位置的路径缺了一级
他包实际的路径是/native/xxx
因此我我们自己加上
image
接下来运行就可以了
image