Skip to content

Commit 10db37a

Browse files
authored
Update projects to use latest WebView2 SDK 1.0.781-prerelease (#74)
1 parent 062c3b3 commit 10db37a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+759
-295
lines changed

SampleApps/WebView2APISample/AppStartPage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ std::wstring ResolvePathAndTrimFile(std::wstring path)
3838
PathCchCanonicalize(resultPath, ARRAYSIZE(resultPath), path.c_str());
3939
PathCchRemoveFileSpec(resultPath, ARRAYSIZE(resultPath));
4040
#else
41-
// Surpress compiler warning for PathCanonicalize. It is only used on Win7
41+
// Supress compiler warning for PathCanonicalize. It is only used on Win7
4242
// where PathCchCanonicalize doesn't exist.
4343
#pragma warning(suppress : 4995)
4444
PathCanonicalize(resultPath, path.c_str());

SampleApps/WebView2APISample/AppWindow.cpp

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ AppWindow::AppWindow(
124124
0, CW_USEDEFAULT, 0, nullptr, nullptr, g_hInstance, nullptr);
125125
}
126126

127+
m_appBackgroundImageHandle = (HBITMAP)LoadImage(
128+
NULL, L"AppBackground.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
129+
GetObject(m_appBackgroundImageHandle, sizeof(m_appBackgroundImage), &m_appBackgroundImage);
130+
m_memHdc = CreateCompatibleDC(GetDC(m_mainWindow));
131+
SelectObject(m_memHdc, m_appBackgroundImageHandle);
132+
127133
SetWindowLongPtr(m_mainWindow, GWLP_USERDATA, (LONG_PTR)this);
128134

129135
#ifdef USE_WEBVIEW2_WIN10
@@ -260,7 +266,22 @@ bool AppWindow::HandleWindowMessage(
260266
case WM_PAINT:
261267
{
262268
PAINTSTRUCT ps;
269+
HDC hdc;
270+
RECT mainWindowBounds;
271+
RECT webViewBounds = {0};
263272
BeginPaint(hWnd, &ps);
273+
274+
hdc = GetDC(hWnd);
275+
GetClientRect(hWnd, &mainWindowBounds);
276+
277+
if (auto viewComponent = GetComponent<ViewComponent>())
278+
{
279+
webViewBounds = viewComponent->GetBounds();
280+
}
281+
282+
StretchBlt(hdc, webViewBounds.left, webViewBounds.top, webViewBounds.right, webViewBounds.bottom,
283+
m_memHdc, 0, 0, m_appBackgroundImage.bmWidth, m_appBackgroundImage.bmHeight, SRCCOPY);
284+
264285
EndPaint(hWnd, &ps);
265286
return true;
266287
}
@@ -282,6 +303,8 @@ bool AppWindow::HandleWindowMessage(
282303
{
283304
PostQuitMessage(retValue);
284305
}
306+
DeleteObject(m_appBackgroundImageHandle);
307+
DeleteDC(m_memHdc);
285308
}
286309
break;
287310
//! [RestartManager]
@@ -630,23 +653,23 @@ HRESULT AppWindow::OnCreateEnvironmentCompleted(
630653
CHECK_FAILURE(result);
631654
m_webViewEnvironment = environment;
632655

633-
auto webViewExperimentalEnvironment =
634-
m_webViewEnvironment.try_query<ICoreWebView2ExperimentalEnvironment>();
656+
auto webViewEnvironment3 =
657+
m_webViewEnvironment.try_query<ICoreWebView2Environment3>();
635658
#ifdef USE_WEBVIEW2_WIN10
636-
if (webViewExperimentalEnvironment && (m_dcompDevice || m_wincompCompositor))
659+
if (webViewEnvironment3 && (m_dcompDevice || m_wincompCompositor))
637660
#else
638-
if (webViewExperimentalEnvironment && m_dcompDevice)
661+
if (webViewEnvironment3 && m_dcompDevice)
639662
#endif
640663
{
641-
CHECK_FAILURE(webViewExperimentalEnvironment->CreateCoreWebView2CompositionController(
664+
CHECK_FAILURE(webViewEnvironment3->CreateCoreWebView2CompositionController(
642665
m_mainWindow,
643666
Callback<
644-
ICoreWebView2ExperimentalCreateCoreWebView2CompositionControllerCompletedHandler>(
667+
ICoreWebView2CreateCoreWebView2CompositionControllerCompletedHandler>(
645668
[this](
646669
HRESULT result,
647-
ICoreWebView2ExperimentalCompositionController* compositionController) -> HRESULT {
670+
ICoreWebView2CompositionController* compositionController) -> HRESULT {
648671
auto controller =
649-
wil::com_ptr<ICoreWebView2ExperimentalCompositionController>(compositionController)
672+
wil::com_ptr<ICoreWebView2CompositionController>(compositionController)
650673
.query<ICoreWebView2Controller>();
651674
return OnCreateCoreWebView2ControllerCompleted(result, controller.get());
652675
})
@@ -693,13 +716,16 @@ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(HRESULT result, ICore
693716
m_creationModeId == IDM_CREATION_MODE_TARGET_DCOMP);
694717
NewComponent<ControlComponent>(this, &m_toolbar);
695718

696-
wil::com_ptr<ICoreWebView2Experimental2> webview2;
697-
webview2 = coreWebView2.query<ICoreWebView2Experimental2>();
698-
//! [AddVirtualHostNameToFolderMapping]
699-
// Setup host resource mapping for local files.
700-
webview2->SetVirtualHostNameToFolderMapping(
701-
L"appassets.example", L"assets", COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY_CORS);
702-
//! [AddVirtualHostNameToFolderMapping]
719+
m_webView3 = coreWebView2.query<ICoreWebView2_3>();
720+
if (m_webView3)
721+
{
722+
//! [AddVirtualHostNameToFolderMapping]
723+
// Setup host resource mapping for local files.
724+
m_webView3->SetVirtualHostNameToFolderMapping(
725+
L"appassets.example", L"assets",
726+
COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY_CORS);
727+
//! [AddVirtualHostNameToFolderMapping]
728+
}
703729

704730
// We have a few of our own event handlers to register here as well
705731
RegisterEventHandlers();
@@ -1096,10 +1122,24 @@ std::wstring AppWindow::GetLocalPath(std::wstring relativePath, bool keep_exe_pa
10961122
}
10971123
std::wstring AppWindow::GetLocalUri(std::wstring relativePath)
10981124
{
1099-
//! [LocalUrlUsage]
1100-
const std::wstring localFileRootUrl = L"https://appassets.example/";
1101-
return localFileRootUrl + regex_replace(relativePath, std::wregex(L"\\\\"), L"/");
1102-
//! [LocalUrlUsage]
1125+
if (m_webView3)
1126+
{
1127+
//! [LocalUrlUsage]
1128+
const std::wstring localFileRootUrl = L"https://appassets.example/";
1129+
return localFileRootUrl + regex_replace(relativePath, std::wregex(L"\\\\"), L"/");
1130+
//! [LocalUrlUsage]
1131+
}
1132+
else
1133+
{
1134+
std::wstring path = GetLocalPath(L"assets\\" + relativePath, false);
1135+
1136+
wil::com_ptr<IUri> uri;
1137+
CHECK_FAILURE(CreateUri(path.c_str(), Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &uri));
1138+
1139+
wil::unique_bstr uriBstr;
1140+
CHECK_FAILURE(uri->GetAbsoluteUri(&uriBstr));
1141+
return std::wstring(uriBstr.get());
1142+
}
11031143
}
11041144

11051145
void AppWindow::RunAsync(std::function<void()> callback)

SampleApps/WebView2APISample/AppWindow.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class AppWindow
130130
wil::com_ptr<ICoreWebView2Environment> m_webViewEnvironment;
131131
wil::com_ptr<ICoreWebView2Controller> m_controller;
132132
wil::com_ptr<ICoreWebView2> m_webView;
133+
wil::com_ptr<ICoreWebView2_3> m_webView3;
133134

134135
// All components are deleted when the WebView is closed.
135136
std::vector<std::unique_ptr<ComponentBase>> m_components;
@@ -157,6 +158,11 @@ class AppWindow
157158
winrtComp::Compositor m_wincompCompositor{ nullptr };
158159
winrt::Windows::UI::ViewManagement::UISettings m_uiSettings{ nullptr };
159160
#endif
161+
162+
// Background Image members
163+
HBITMAP m_appBackgroundImageHandle;
164+
BITMAP m_appBackgroundImage;
165+
HDC m_memHdc;
160166
};
161167

162168
template <class ComponentType, class... Args> void AppWindow::NewComponent(Args&&... args)

SampleApps/WebView2APISample/CheckFailure.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ void CheckFailure(HRESULT hr, const std::wstring& message)
2323
FAIL_FAST();
2424
}
2525
}
26+
27+
void ExperimentalFeatureNotAvailable()
28+
{
29+
MessageBox(nullptr,
30+
L"This experimental feature is not available in the browser version currently being used.",
31+
L"Feature Not Available", MB_OK);
32+
}

SampleApps/WebView2APISample/CheckFailure.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ void ShowFailure(HRESULT hr, const std::wstring& message = L"Error");
1111
// If something failed, show the error code and fail fast.
1212
void CheckFailure(HRESULT hr, const std::wstring& message = L"Error");
1313

14-
1514
// Needs to be a separate macro because the preprocessor is weird
1615
#define CHECK_FAILURE_STRINGIFY(arg) #arg
1716

@@ -26,3 +25,10 @@ void CheckFailure(HRESULT hr, const std::wstring& message = L"Error");
2625
#define CHECK_FAILURE_FILE_LINE(file, line) ([](HRESULT hr){ CheckFailure(hr, L"Failure at " CHECK_FAILURE_STRINGIFY(file) L"(" CHECK_FAILURE_STRINGIFY(line) L")"); })
2726
#define CHECK_FAILURE CHECK_FAILURE_FILE_LINE(__FILE__, __LINE__)
2827
#define CHECK_FAILURE_BOOL(value) CHECK_FAILURE((value) ? S_OK : E_UNEXPECTED)
28+
29+
// Show a message box indicating that an experimental interface isn't available in this browser version.
30+
// Only call this in direct response to a specific user action.
31+
void ExperimentalFeatureNotAvailable();
32+
33+
// Wraps the above in a conditional.
34+
#define CHECK_FEATURE_RETURN(feature, ret) { if (!feature) { ExperimentalFeatureNotAvailable(); return (ret); } }

0 commit comments

Comments
 (0)