|
7 | 7 | #include <wrl.h>
|
8 | 8 | #include <wil/com.h>
|
9 | 9 | // include WebView2 header
|
| 10 | +#include "WebView2.h" |
10 | 11 |
|
11 | 12 | using namespace Microsoft::WRL;
|
12 | 13 |
|
@@ -104,6 +105,86 @@ int CALLBACK WinMain(
|
104 | 105 | UpdateWindow(hWnd);
|
105 | 106 |
|
106 | 107 | // <-- 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()); |
107 | 188 |
|
108 | 189 |
|
109 | 190 |
|
|
0 commit comments