Skip to content

Commit 7f06fc2

Browse files
authored
Win32 Getting Started Guide Improvements (#144)
* Address proposed changes in Win32 Getting Started Guide * Add code snippet labels * [PR feedback] PWSTR -> wil::unique_cotaskmem_string
1 parent da3d0b8 commit 7f06fc2

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

GettingStartedGuides/Win32_GettingStarted/HelloWebView.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
2828
static wil::com_ptr<ICoreWebView2Controller> webviewController;
2929

3030
// Pointer to WebView window
31-
static wil::com_ptr<ICoreWebView2> webviewWindow;
31+
static wil::com_ptr<ICoreWebView2> webview;
3232

3333
int CALLBACK WinMain(
3434
_In_ HINSTANCE hInstance,
@@ -116,67 +116,71 @@ int CALLBACK WinMain(
116116
[hWnd](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
117117
if (controller != nullptr) {
118118
webviewController = controller;
119-
webviewController->get_CoreWebView2(&webviewWindow);
119+
webviewController->get_CoreWebView2(&webview);
120120
}
121121

122122
// Add a few settings for the webview
123123
// 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);
124+
wil::com_ptr<ICoreWebView2Settings> settings;
125+
webview->get_Settings(&settings);
126+
settings->put_IsScriptEnabled(TRUE);
127+
settings->put_AreDefaultScriptDialogsEnabled(TRUE);
128+
settings->put_IsWebMessageEnabled(TRUE);
129129

130130
// Resize WebView to fit the bounds of the parent window
131131
RECT bounds;
132132
GetClientRect(hWnd, &bounds);
133133
webviewController->put_Bounds(bounds);
134134

135135
// Schedule an async task to navigate to Bing
136-
webviewWindow->Navigate(L"https://www.bing.com/");
136+
webview->Navigate(L"https://www.bing.com/");
137137

138+
// <Step4>
138139
// Step 4 - Navigation events
139140
// register an ICoreWebView2NavigationStartingEventHandler to cancel any non-https navigation
140141
EventRegistrationToken token;
141-
webviewWindow->add_NavigationStarting(Callback<ICoreWebView2NavigationStartingEventHandler>(
142+
webview->add_NavigationStarting(Callback<ICoreWebView2NavigationStartingEventHandler>(
142143
[](ICoreWebView2* webview, ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT {
143-
PWSTR uri;
144+
wil::unique_cotaskmem_string uri;
144145
args->get_Uri(&uri);
145-
std::wstring source(uri);
146+
std::wstring source(uri.get());
146147
if (source.substr(0, 5) != L"https") {
147148
args->put_Cancel(true);
148149
}
149-
CoTaskMemFree(uri);
150150
return S_OK;
151151
}).Get(), &token);
152+
// <Step4>
152153

154+
// <Step5>
153155
// Step 5 - Scripting
154156
// Schedule an async task to add initialization script that freezes the Object object
155-
webviewWindow->AddScriptToExecuteOnDocumentCreated(L"Object.freeze(Object);", nullptr);
157+
webview->AddScriptToExecuteOnDocumentCreated(L"Object.freeze(Object);", nullptr);
156158
// Schedule an async task to get the document URL
157-
webviewWindow->ExecuteScript(L"window.document.URL;", Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
159+
webview->ExecuteScript(L"window.document.URL;", Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
158160
[](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT {
159161
LPCWSTR URL = resultObjectAsJson;
160162
//doSomethingWithURL(URL);
161163
return S_OK;
162164
}).Get());
165+
// <Step5>
163166

167+
// <Step6>
164168
// Step 6 - Communication between host and web content
165169
// Set an event handler for the host to return received message back to the web content
166-
webviewWindow->add_WebMessageReceived(Callback<ICoreWebView2WebMessageReceivedEventHandler>(
170+
webview->add_WebMessageReceived(Callback<ICoreWebView2WebMessageReceivedEventHandler>(
167171
[](ICoreWebView2* webview, ICoreWebView2WebMessageReceivedEventArgs* args) -> HRESULT {
168-
PWSTR message;
172+
wil::unique_cotaskmem_string message;
169173
args->TryGetWebMessageAsString(&message);
170174
// processMessage(&message);
171-
webview->PostWebMessageAsString(message);
172-
CoTaskMemFree(message);
175+
webview->PostWebMessageAsString(message.get());
173176
return S_OK;
174177
}).Get(), &token);
178+
// <Step6>
175179

176180
// Schedule an async task to add initialization script that
177181
// 1) Add an listener to print message from the host
178182
// 2) Post document URL to the host
179-
webviewWindow->AddScriptToExecuteOnDocumentCreated(
183+
webview->AddScriptToExecuteOnDocumentCreated(
180184
L"window.chrome.webview.addEventListener(\'message\', event => alert(event.data));" \
181185
L"window.chrome.webview.postMessage(window.document.URL);",
182186
nullptr);

0 commit comments

Comments
 (0)