Skip to content

Commit 9bf2c49

Browse files
authored
Complete Win32 guide (#101)
1 parent ae44622 commit 9bf2c49

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

GettingStartedGuides/Win32_GettingStarted/HelloWebView.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <wrl.h>
88
#include <wil/com.h>
99
// include WebView2 header
10+
#include "WebView2.h"
1011

1112
using namespace Microsoft::WRL;
1213

@@ -104,6 +105,86 @@ int CALLBACK WinMain(
104105
UpdateWindow(hWnd);
105106

106107
// <-- WebView2 sample code starts here -->
108+
// Step 3 - Create a single WebView within the parent window
109+
// Locate the browser and set up the environment for WebView
110+
CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr,
111+
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
112+
[hWnd](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {
113+
114+
// Create a CoreWebView2Controller and get the associated CoreWebView2 whose parent is the main window hWnd
115+
env->CreateCoreWebView2Controller(hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
116+
[hWnd](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
117+
if (controller != nullptr) {
118+
webviewController = controller;
119+
webviewController->get_CoreWebView2(&webviewWindow);
120+
}
121+
122+
// Add a few settings for the webview
123+
// The demo step is redundant since the values are the default settings
124+
ICoreWebView2Settings* Settings;
125+
webviewWindow->get_Settings(&Settings);
126+
Settings->put_IsScriptEnabled(TRUE);
127+
Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
128+
Settings->put_IsWebMessageEnabled(TRUE);
129+
130+
// Resize WebView to fit the bounds of the parent window
131+
RECT bounds;
132+
GetClientRect(hWnd, &bounds);
133+
webviewController->put_Bounds(bounds);
134+
135+
// Schedule an async task to navigate to Bing
136+
webviewWindow->Navigate(L"https://www.bing.com/");
137+
138+
// Step 4 - Navigation events
139+
// register an ICoreWebView2NavigationStartingEventHandler to cancel any non-https navigation
140+
EventRegistrationToken token;
141+
webviewWindow->add_NavigationStarting(Callback<ICoreWebView2NavigationStartingEventHandler>(
142+
[](ICoreWebView2* webview, ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT {
143+
PWSTR uri;
144+
args->get_Uri(&uri);
145+
std::wstring source(uri);
146+
if (source.substr(0, 5) != L"https") {
147+
args->put_Cancel(true);
148+
}
149+
CoTaskMemFree(uri);
150+
return S_OK;
151+
}).Get(), &token);
152+
153+
// Step 5 - Scripting
154+
// Schedule an async task to add initialization script that freezes the Object object
155+
webviewWindow->AddScriptToExecuteOnDocumentCreated(L"Object.freeze(Object);", nullptr);
156+
// Schedule an async task to get the document URL
157+
webviewWindow->ExecuteScript(L"window.document.URL;", Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
158+
[](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT {
159+
LPCWSTR URL = resultObjectAsJson;
160+
//doSomethingWithURL(URL);
161+
return S_OK;
162+
}).Get());
163+
164+
// Step 6 - Communication between host and web content
165+
// Set an event handler for the host to return received message back to the web content
166+
webviewWindow->add_WebMessageReceived(Callback<ICoreWebView2WebMessageReceivedEventHandler>(
167+
[](ICoreWebView2* webview, ICoreWebView2WebMessageReceivedEventArgs* args) -> HRESULT {
168+
PWSTR message;
169+
args->TryGetWebMessageAsString(&message);
170+
// processMessage(&message);
171+
webview->PostWebMessageAsString(message);
172+
CoTaskMemFree(message);
173+
return S_OK;
174+
}).Get(), &token);
175+
176+
// Schedule an async task to add initialization script that
177+
// 1) Add an listener to print message from the host
178+
// 2) Post document URL to the host
179+
webviewWindow->AddScriptToExecuteOnDocumentCreated(
180+
L"window.chrome.webview.addEventListener(\'message\', event => alert(event.data));" \
181+
L"window.chrome.webview.postMessage(window.document.URL);",
182+
nullptr);
183+
184+
return S_OK;
185+
}).Get());
186+
return S_OK;
187+
}).Get());
107188

108189

109190

GettingStartedGuides/Win32_GettingStarted/WebView2GettingStarted.vcxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,19 @@
217217
<ItemGroup>
218218
<ClCompile Include="HelloWebView.cpp" />
219219
</ItemGroup>
220+
<ItemGroup>
221+
<None Include="packages.config" />
222+
</ItemGroup>
220223
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
221224
<ImportGroup Label="ExtensionTargets">
225+
<Import Project="packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
226+
<Import Project="packages\Microsoft.Web.WebView2.1.0.902.49\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('packages\Microsoft.Web.WebView2.1.0.902.49\build\native\Microsoft.Web.WebView2.targets')" />
222227
</ImportGroup>
228+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
229+
<PropertyGroup>
230+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
231+
</PropertyGroup>
232+
<Error Condition="!Exists('packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
233+
<Error Condition="!Exists('packages\Microsoft.Web.WebView2.1.0.902.49\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Web.WebView2.1.0.902.49\build\native\Microsoft.Web.WebView2.targets'))" />
234+
</Target>
223235
</Project>

GettingStartedGuides/Win32_GettingStarted/WebView2GettingStarted.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
<Filter>Source Files</Filter>
2020
</ClCompile>
2121
</ItemGroup>
22+
<ItemGroup>
23+
<None Include="packages.config" />
24+
</ItemGroup>
2225
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.Web.WebView2" version="1.0.902.49" targetFramework="native" />
4+
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.191107.2" targetFramework="native" />
5+
</packages>

0 commit comments

Comments
 (0)