使用 CMake 构建 Win32 C++ 与 Webview2 窗口

CMake 示例,重点在最后三行,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cmake_minimum_required(VERSION 3.25.0)
project(DirectWriteExperiment VERSION 1.0)

# Specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)


add_definitions(
/D_UNICODE=1
/DUNICODE=1
)

include_directories("./includes/")

set(HEADER_FILES
./includes/main.h
)

set(SRC_FILES
src/webview2/demo02.cpp
)

add_executable(DirectWriteExperiment WIN32
${HEADER_FILES}
${SRC_FILES}
)

set_target_properties(DirectWriteExperiment PROPERTIES VS_GLOBAL_WebView2LoaderPreference "Static")
set_target_properties(DirectWriteExperiment PROPERTIES VS_USER_PROPS "${CMAKE_SOURCE_DIR}/Demo.props")
set_target_properties(DirectWriteExperiment PROPERTIES VS_PACKAGE_REFERENCES "Microsoft.Web.WebView2_1.0.3124.44;Microsoft.Windows.ImplementationLibrary_1.0.240803.1")

同时,上面需要用的配置 Demo.props 如下,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>

<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
<ProjectCapability Include="PackageReferences" />
</ItemGroup>

<PropertyGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<NuGetPackageImportStamp>true</NuGetPackageImportStamp>

<Platform Condition="'$(Platform)' == ''">x64</Platform>
<TargetFramework>native</TargetFramework>

<NuGetPackageRoot Condition="'$(NuGetPackageRoot)' == ''">$(UserProfile)\.nuget\packages</NuGetPackageRoot>

<WebView2LoaderPreference>Static</WebView2LoaderPreference>

<NuGetTargetMoniker Condition="'$(NuGetTargetMoniker)' == ''">native,Version=v0.0</NuGetTargetMoniker>
<RuntimeIdentifiers Condition="'$(RuntimeIdentifiers)' == ''">win;win-x86;win-x64;win-arm;win-arm64</RuntimeIdentifiers>
</PropertyGroup>
</Project>

一份可以运行的单文件,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Work with WebView2
*/
#include "WebView2.h"
#include <dwmapi.h>
#include <stdlib.h>
#include <string>
#include <tchar.h>
#include <windows.h>
#include <wrl.h>

#pragma comment(lib, "dwmapi.lib")

// The main window class name.
static TCHAR szWindowClass[] = _T("DesktopApp");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("WebView sample");

HINSTANCE hInst = 0;

using namespace Microsoft::WRL;

// don't care memory leak for now
static ICoreWebView2Controller *webviewController = nullptr;
static ICoreWebView2 *webview = nullptr;

static 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:
// Release WebView2 objects
if (webviewController != nullptr)
{
webviewController->Release();
webviewController = nullptr;
}
if (webview != nullptr)
{
webview->Release();
webview = nullptr;
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}

return 0;
}

int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);

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

BOOL useDarkMode = TRUE;
DwmSetWindowAttribute( //
hWnd, //
DWMWA_USE_IMMERSIVE_DARK_MODE, //
&useDarkMode, //
sizeof(useDarkMode) //
);

// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

CreateCoreWebView2EnvironmentWithOptions(
nullptr, nullptr, nullptr,
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[hWnd](HRESULT result, ICoreWebView2Environment *env) -> HRESULT {
// Create a CoreWebView2Controller and get the associated
// CoreWebView2 whose parent is the main window hWnd
env->CreateCoreWebView2Controller(
hWnd,
Callback<
ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
[hWnd](HRESULT result,
ICoreWebView2Controller *controller) -> HRESULT {
if (controller != nullptr)
{
controller->AddRef();
webviewController = controller;
webviewController->get_CoreWebView2(&webview);
}

// Add a few settings for the webview
// The demo step is redundant since the values are
// the default settings
ICoreWebView2Settings *settings;
webview->get_Settings(&settings);
settings->put_IsScriptEnabled(TRUE);
settings->put_AreDefaultScriptDialogsEnabled(TRUE);
settings->put_IsWebMessageEnabled(TRUE);
// settings->Release();

// 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
auto hr = webview->Navigate(
L"https://fanlumaster.github.io/");

// <NavigationEvents>
// Step 4 - Navigation events
// register an
// ICoreWebView2NavigationStartingEventHandler to
// cancel any non-https navigation
EventRegistrationToken token;
webview->add_NavigationStarting(
Callback<
ICoreWebView2NavigationStartingEventHandler>(
[](ICoreWebView2 *webview,
ICoreWebView2NavigationStartingEventArgs
*args) -> HRESULT {
wchar_t *uri;
args->get_Uri(&uri);
std::wstring source(uri);
// CoTaskMemFree(uri);
if (source.substr(0, 5) != L"https")
{
args->put_Cancel(true);
}
return S_OK;
})
.Get(),
&token);
// </NavigationEvents>

// <Scripting>
// Step 5 - Scripting
// Schedule an async task to add initialization
// script that freezes the Object object
webview->AddScriptToExecuteOnDocumentCreated(
L"Object.freeze(Object);", nullptr);
// Schedule an async task to get the document URL
webview->ExecuteScript(
L"window.document.URL;",
Callback<
ICoreWebView2ExecuteScriptCompletedHandler>(
[](HRESULT errorCode,
LPCWSTR resultObjectAsJson) -> HRESULT {
LPCWSTR URL = resultObjectAsJson;
// doSomethingWithURL(URL);
return S_OK;
})
.Get());
// </Scripting>

// <CommunicationHostWeb>
// Step 6 - Communication between host and web
// content Set an event handler for the host to
// return received message back to the web content
webview->add_WebMessageReceived(
Callback<
ICoreWebView2WebMessageReceivedEventHandler>(
[](ICoreWebView2 *webview,
ICoreWebView2WebMessageReceivedEventArgs
*args) -> HRESULT {
wchar_t *message;
args->TryGetWebMessageAsString(
&message);
// processMessage(&message);
webview->PostWebMessageAsString(
message);
// CoTaskMemFree(message);
return S_OK;
})
.Get(),
&token);

// Schedule an async task to add initialization
// script that 1) Add an listener to print message
// from the host 2) Post document URL to the host
webview->AddScriptToExecuteOnDocumentCreated(
L"window.chrome.webview.addEventListener("
L"\'message\', event => "
L"alert(event.data));"
L"window.chrome.webview.postMessage(window."
L"document.URL);",
nullptr);
// </CommunicationHostWeb>

return S_OK;
})
.Get());
return S_OK;
})
.Get());
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}


使用 CMake 构建 Win32 C++ 与 Webview2 窗口
http://fanlumaster.github.io/2025/03/19/building-win32-cpp-window-with-webview2-using-cmake/
作者
fanlumaster
发布于
2025年3月19日
许可协议