Skip to content

Commit 290cce4

Browse files
author
Michael Hoffman
committed
Merge branch 'user/v-mhoffman/readmes-content' of https://github.com/MicrosoftDocs/edge-developer into user/v-mhoffman/readmes-content
2 parents 968c2a5 + 24be9c8 commit 290cce4

File tree

1 file changed

+6
-68
lines changed
  • microsoft-edge/webview2/get-started

1 file changed

+6
-68
lines changed

microsoft-edge/webview2/get-started/win32.md

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.author: msedgedevrel
66
ms.topic: conceptual
77
ms.prod: microsoft-edge
88
ms.technology: webview
9-
ms.date: 04/27/2022
9+
ms.date: 07/29/2022
1010
---
1111
# Get started with WebView2 in Win32 apps
1212

@@ -277,10 +277,7 @@ If you want to follow the steps below to add the WebView2 code to `HelloWebView.
277277

278278
1. In `HelloWebView.cpp`, delete the following code:
279279

280-
```cpp
281-
// include WebView2 header
282-
#include "WebView2.h"
283-
```
280+
:::code language="cpp" source="../code/sample/GettingStartedGuides/Win32_GettingStarted/HelloWebView.cpp" id="IncludeHeader":::
284281

285282
1. In `HelloWebView.cpp`, delete the lines of code that are in between these two comment lines, but keep these two comment lines:
286283

@@ -309,10 +306,7 @@ Next, add WebView2 features to the app, as follows:
309306

310307
1. If the following code isn't already present, paste the following code in `HelloWebView.cpp`, after the last `#include` line:
311308

312-
```cpp
313-
// include WebView2 header
314-
#include "WebView2.h"
315-
```
309+
:::code language="cpp" source="../code/sample/GettingStartedGuides/Win32_GettingStarted/HelloWebView.cpp" id="IncludeHeader":::
316310

317311
Make sure that the `include` section looks like the following:
318312

@@ -421,17 +415,11 @@ Now to do the above, in the callback, you'll:
421415
// Schedule an async task to navigate to Bing
422416
webview->Navigate(L"https://www.bing.com/");
423417
424-
// <Step4>
425418
// Step 4 - Navigation events
426-
// <Step4>
427419
428-
// <Step5>
429420
// Step 5 - Scripting
430-
// <Step5>
431421
432-
// <Step6>
433422
// Step 6 - Communication between host and web content
434-
// <Step6>
435423
436424
return S_OK;
437425
}).Get());
@@ -494,23 +482,7 @@ As an example of using navigation events, register a handler for the `Navigation
494482

495483
1. If it's not already present, paste the following code into `HelloWebView.cpp`, below the Step 3 code:
496484

497-
```cpp
498-
// <Step4>
499-
// Step 4 - Navigation events
500-
// register an ICoreWebView2NavigationStartingEventHandler to cancel any non-https navigation
501-
EventRegistrationToken token;
502-
webview->add_NavigationStarting(Callback<ICoreWebView2NavigationStartingEventHandler>(
503-
[](ICoreWebView2* webview, ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT {
504-
wil::unique_cotaskmem_string uri;
505-
args->get_Uri(&uri);
506-
std::wstring source(uri.get());
507-
if (source.substr(0, 5) != L"https") {
508-
args->put_Cancel(true);
509-
}
510-
return S_OK;
511-
}).Get(), &token);
512-
// <Step4>
513-
```
485+
:::code language="cpp" source="../code/sample/GettingStartedGuides/Win32_GettingStarted/HelloWebView.cpp" id="NavigationEvents":::
514486

515487
Now the app doesn't open any non-https sites. You can use a similar mechanism to accomplish other tasks, such as restricting navigation to within your own domain.
516488

@@ -533,20 +505,7 @@ The injected JavaScript is run with specific timing:
533505

534506
1. If the following code isn't present already, paste the following code into `HelloWebView.cpp`:
535507

536-
```cpp
537-
// <Step5>
538-
// Step 5 - Scripting
539-
// Schedule an async task to add initialization script that freezes the Object object
540-
webview->AddScriptToExecuteOnDocumentCreated(L"Object.freeze(Object);", nullptr);
541-
// Schedule an async task to get the document URL
542-
webview->ExecuteScript(L"window.document.URL;", Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
543-
[](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT {
544-
LPCWSTR URL = resultObjectAsJson;
545-
//doSomethingWithURL(URL);
546-
return S_OK;
547-
}).Get());
548-
// <Step5>
549-
```
508+
:::code language="cpp" source="../code/sample/GettingStartedGuides/Win32_GettingStarted/HelloWebView.cpp" id="Scripting":::
550509

551510
1. Select **File** > **Save All** (`Ctrl`+`Shift`+`S`) to save the project.
552511

@@ -589,28 +548,7 @@ Have the host app and web content communicate through `postMessage`, as follows:
589548

590549
1. If it's not already present, paste the following code into `HelloWebView.cpp`:
591550

592-
```cpp
593-
// <Step6>
594-
// Step 6 - Communication between host and web content
595-
// Set an event handler for the host to return received message back to the web content
596-
webview->add_WebMessageReceived(Callback<ICoreWebView2WebMessageReceivedEventHandler>(
597-
[](ICoreWebView2* webview, ICoreWebView2WebMessageReceivedEventArgs* args) -> HRESULT {
598-
wil::unique_cotaskmem_string message;
599-
args->TryGetWebMessageAsString(&message);
600-
// processMessage(&message);
601-
webview->PostWebMessageAsString(message.get());
602-
return S_OK;
603-
}).Get(), &token);
604-
// <Step6>
605-
606-
// Schedule an async task to add initialization script that
607-
// 1) Add an listener to print message from the host
608-
// 2) Post document URL to the host
609-
webview->AddScriptToExecuteOnDocumentCreated(
610-
L"window.chrome.webview.addEventListener(\'message\', event => alert(event.data));" \
611-
L"window.chrome.webview.postMessage(window.document.URL);",
612-
nullptr);
613-
```
551+
:::code language="cpp" source="../code/sample/GettingStartedGuides/Win32_GettingStarted/HelloWebView.cpp" id="CommunicationHostWeb":::
614552

615553
1. Select **File** > **Save All** (`Ctrl`+`Shift`+`S`) to save the project.
616554

0 commit comments

Comments
 (0)