Skip to content

Commit 11f0dd1

Browse files
Update projects to use latest WebView2 SDK 1.0.1414-prerelease (#152)
* Updates for Win32, WPF and WinForms sample apps from 107.0.1414.0 * Updated package version for Win32, WPF and WinForms sample apps to 1.0.1414-prerelease * Catch Win32Exception and not set transparent background Co-authored-by: WebView2 Github Bot <[email protected]>
1 parent dce00b6 commit 11f0dd1

20 files changed

+1189
-54
lines changed

SampleApps/WebView2APISample/AppWindow.cpp

Lines changed: 286 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "ScenarioCustomScheme.h"
3636
#include "ScenarioCustomSchemeNavigate.h"
3737
#include "ScenarioDOMContentLoaded.h"
38+
#include "ScenarioExtensionsManagement.h"
3839
#include "ScenarioIFrameDevicePermission.h"
3940
#include "ScenarioNavigateWithWebResourceRequest.h"
4041
#include "ScenarioSharedWorkerWRR.h"
@@ -46,7 +47,6 @@
4647
#include "SettingsComponent.h"
4748
#include "TextInputDialog.h"
4849
#include "ViewComponent.h"
49-
5050
using namespace Microsoft::WRL;
5151
static constexpr size_t s_maxLoadString = 100;
5252
static constexpr UINT s_runAsyncWindowMessage = WM_APP;
@@ -472,6 +472,18 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
472472
//! [GetUserDataFolder]
473473
return true;
474474
}
475+
case IDM_GET_FAILURE_REPORT_FOLDER:
476+
{
477+
//! [GetFailureReportFolder]
478+
auto experimental_environment =
479+
m_webViewEnvironment.try_query<ICoreWebView2ExperimentalEnvironment>();
480+
CHECK_FEATURE_RETURN(experimental_environment);
481+
wil::unique_cotaskmem_string failureReportFolder;
482+
experimental_environment->get_FailureReportFolderPath(&failureReportFolder);
483+
MessageBox(m_mainWindow, failureReportFolder.get(), L"Failure Report Folder", MB_OK);
484+
//! [GetFailureReportFolder]
485+
return true;
486+
}
475487
case IDM_CLOSE_WEBVIEW:
476488
{
477489
CloseWebView();
@@ -592,6 +604,26 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
592604
NewComponent<ScenarioIFrameDevicePermission>(this);
593605
return true;
594606
}
607+
case IDM_SCENARIO_BROWSER_PRINT_PREVIEW:
608+
{
609+
return ShowPrintUI(COREWEBVIEW2_PRINT_DIALOG_KIND_BROWSER);
610+
}
611+
case IDM_SCENARIO_SYSTEM_PRINT:
612+
{
613+
return ShowPrintUI(COREWEBVIEW2_PRINT_DIALOG_KIND_SYSTEM);
614+
}
615+
case IDM_SCENARIO_PRINT_TO_DEFAULT_PRINTER:
616+
{
617+
return PrintToDefaultPrinter();
618+
}
619+
case IDM_SCENARIO_PRINT_TO_PRINTER:
620+
{
621+
return PrintToPrinter();
622+
}
623+
case IDM_SCENARIO_PRINT_TO_PDF_STREAM:
624+
{
625+
return PrintToPdfStream();
626+
}
595627
}
596628
return false;
597629
}
@@ -693,6 +725,9 @@ bool AppWindow::ExecuteAppCommands(WPARAM wParam, LPARAM lParam)
693725
case IDM_TOGGLE_EXCLUSIVE_USER_DATA_FOLDER_ACCESS:
694726
ToggleExclusiveUserDataFolderAccess();
695727
return true;
728+
case IDM_TOGGLE_CUSTOM_CRASH_REPORTING:
729+
ToggleCustomCrashReporting();
730+
return true;
696731
case IDM_SCENARIO_CLEAR_BROWSING_DATA_COOKIES:
697732
{
698733
return ClearBrowsingData(COREWEBVIEW2_BROWSING_DATA_KINDS_COOKIES);
@@ -795,6 +830,249 @@ void AppWindow::ToggleExclusiveUserDataFolderAccess()
795830
L"Exclusive User Data Folder Access change", MB_OK);
796831
}
797832

833+
void AppWindow::ToggleCustomCrashReporting()
834+
{
835+
m_CustomCrashReportingEnabled = !m_CustomCrashReportingEnabled;
836+
MessageBox(
837+
nullptr,
838+
m_CustomCrashReportingEnabled ? L"Crash reporting will be disabled for new WebView "
839+
L"created after all webviews are closed."
840+
: L"Crash reporting will be enabled for new WebView "
841+
L"created after all webviews are closed.",
842+
L"Custom Crash Reporting", MB_OK);
843+
}
844+
845+
//! [ShowPrintUI]
846+
// Shows the user a print dialog. If `printDialogKind` is
847+
// COREWEBVIEW2_PRINT_DIALOG_KIND_BROWSER, opens a browser print preview dialog,
848+
// COREWEBVIEW2_PRINT_DIALOG_KIND_SYSTEM opens a system print dialog.
849+
bool AppWindow::ShowPrintUI(COREWEBVIEW2_PRINT_DIALOG_KIND printDialogKind)
850+
{
851+
auto webView2Experimental17 = m_webView.try_query<ICoreWebView2Experimental17>();
852+
CHECK_FEATURE_RETURN(webView2Experimental17);
853+
CHECK_FAILURE(webView2Experimental17->ShowPrintUI(printDialogKind));
854+
return true;
855+
}
856+
//! [ShowPrintUI]
857+
858+
// This example prints the current web page without a print dialog to default printer
859+
// with the default print settings.
860+
bool AppWindow::PrintToDefaultPrinter()
861+
{
862+
wil::com_ptr<ICoreWebView2Experimental17> webView2Experimental17;
863+
CHECK_FAILURE(m_webView->QueryInterface(IID_PPV_ARGS(&webView2Experimental17)));
864+
865+
wil::unique_cotaskmem_string title;
866+
CHECK_FAILURE(m_webView->get_DocumentTitle(&title));
867+
868+
// Passing nullptr for `ICoreWebView2PrintSettings` results in default print settings used.
869+
// Prints current web page with the default page and printer settings.
870+
CHECK_FAILURE(webView2Experimental17->Print(
871+
nullptr, Callback<ICoreWebView2ExperimentalPrintCompletedHandler>(
872+
[title = std::move(title),
873+
this](HRESULT errorCode, COREWEBVIEW2_PRINT_STATUS printStatus) -> HRESULT
874+
{
875+
std::wstring message = L"";
876+
if (errorCode == S_OK &&
877+
printStatus == COREWEBVIEW2_PRINT_STATUS_SUCCEEDED)
878+
{
879+
message = L"Printing " + std::wstring(title.get()) +
880+
L" document to printer is succedded";
881+
}
882+
else if (
883+
errorCode == S_OK &&
884+
printStatus == COREWEBVIEW2_PRINT_STATUS_PRINTER_UNAVAILABLE)
885+
{
886+
message = L"Printer is not available, offline or error state";
887+
}
888+
else if (errorCode == E_ABORT)
889+
{
890+
message = L"Printing " + std::wstring(title.get()) +
891+
L" document is in progress";
892+
}
893+
else
894+
{
895+
message = L"Printing " + std::wstring(title.get()) +
896+
L" document to printer is failed";
897+
}
898+
899+
AsyncMessageBox(message, L"Print to default printer");
900+
901+
return S_OK;
902+
})
903+
.Get()));
904+
return true;
905+
}
906+
907+
// Function to get printer name by displaying printer text input dialog to the user.
908+
// User has to specify the desired printer name by querying the installed printers list on the
909+
// OS to print the web page. You may also choose to display printers list to the user and return
910+
// user selected printer.
911+
std::wstring AppWindow::GetPrinterName()
912+
{
913+
std::wstring printerName;
914+
915+
TextInputDialog dialog(
916+
GetMainWindow(), L"Printer Name", L"Printer Name:",
917+
L"Specify a printer name from the installed printers list on the OS.", L"");
918+
919+
if (dialog.confirmed)
920+
{
921+
printerName = (dialog.input).c_str();
922+
}
923+
return printerName;
924+
925+
// or
926+
//
927+
// Use win32 EnumPrinters function to get locally installed printers.
928+
// Display the printer list to the user and get the user desired printer to print.
929+
// Return the user selected printer name.
930+
}
931+
932+
// Function to get print settings for the selected printer.
933+
// You may also choose get the capabilties from the native printer API, display to the user to
934+
// get the print settings for the current web page and for the selected printer.
935+
SamplePrintSettings AppWindow::GetSelectedPrinterPrintSettings(std::wstring printerName)
936+
{
937+
SamplePrintSettings samplePrintSettings;
938+
samplePrintSettings.PrintBackgrounds = true;
939+
samplePrintSettings.HeaderAndFooter = true;
940+
941+
return samplePrintSettings;
942+
943+
// or
944+
//
945+
// Use win32 DeviceCapabilitiesA function to get the capabilities of the selected printer.
946+
// Display the printer capabilities to the user along with the page settings.
947+
// Return the user selected settings.
948+
}
949+
950+
//! [PrintToPrinter]
951+
// This example prints the current web page to a specified printer with the settings.
952+
bool AppWindow::PrintToPrinter()
953+
{
954+
std::wstring printerName = GetPrinterName();
955+
// Host apps custom print settings based on the user selection.
956+
SamplePrintSettings samplePrintSettings = GetSelectedPrinterPrintSettings(printerName);
957+
958+
wil::com_ptr<ICoreWebView2Experimental17> webView2Experimental17;
959+
CHECK_FAILURE(m_webView->QueryInterface(IID_PPV_ARGS(&webView2Experimental17)));
960+
961+
wil::com_ptr<ICoreWebView2Environment6> webviewEnvironment6;
962+
CHECK_FAILURE(m_webViewEnvironment->QueryInterface(IID_PPV_ARGS(&webviewEnvironment6)));
963+
964+
wil::com_ptr<ICoreWebView2PrintSettings> printSettings;
965+
CHECK_FAILURE(webviewEnvironment6->CreatePrintSettings(&printSettings));
966+
967+
wil::com_ptr<ICoreWebView2ExperimentalPrintSettings2> printSettings2;
968+
CHECK_FAILURE(printSettings->QueryInterface(IID_PPV_ARGS(&printSettings2)));
969+
970+
CHECK_FAILURE(printSettings->put_Orientation(samplePrintSettings.Orientation));
971+
CHECK_FAILURE(printSettings2->put_Copies(samplePrintSettings.Copies));
972+
CHECK_FAILURE(printSettings2->put_PagesPerSide(samplePrintSettings.PagesPerSide));
973+
CHECK_FAILURE(printSettings2->put_PageRanges(samplePrintSettings.Pages.c_str()));
974+
if (samplePrintSettings.Media == COREWEBVIEW2_PRINT_MEDIA_SIZE_CUSTOM)
975+
{
976+
CHECK_FAILURE(printSettings->put_PageWidth(samplePrintSettings.PaperWidth));
977+
CHECK_FAILURE(printSettings->put_PageHeight(samplePrintSettings.PaperHeight));
978+
}
979+
CHECK_FAILURE(printSettings2->put_ColorMode(samplePrintSettings.ColorMode));
980+
CHECK_FAILURE(printSettings2->put_Collation(samplePrintSettings.Collation));
981+
CHECK_FAILURE(printSettings2->put_Duplex(samplePrintSettings.Duplex));
982+
CHECK_FAILURE(printSettings->put_ScaleFactor(samplePrintSettings.ScaleFactor));
983+
CHECK_FAILURE(
984+
printSettings->put_ShouldPrintBackgrounds(samplePrintSettings.PrintBackgrounds));
985+
CHECK_FAILURE(
986+
printSettings->put_ShouldPrintBackgrounds(samplePrintSettings.PrintBackgrounds));
987+
CHECK_FAILURE(
988+
printSettings->put_ShouldPrintHeaderAndFooter(samplePrintSettings.HeaderAndFooter));
989+
CHECK_FAILURE(printSettings->put_HeaderTitle(samplePrintSettings.HeaderTitle.c_str()));
990+
CHECK_FAILURE(printSettings->put_FooterUri(samplePrintSettings.FooterUri.c_str()));
991+
CHECK_FAILURE(printSettings2->put_PrinterName(printerName.c_str()));
992+
993+
wil::unique_cotaskmem_string title;
994+
CHECK_FAILURE(m_webView->get_DocumentTitle(&title));
995+
996+
CHECK_FAILURE(webView2Experimental17->Print(
997+
printSettings.get(),
998+
Callback<ICoreWebView2ExperimentalPrintCompletedHandler>(
999+
[title = std::move(title),
1000+
this](HRESULT errorCode, COREWEBVIEW2_PRINT_STATUS printStatus) -> HRESULT
1001+
{
1002+
std::wstring message = L"";
1003+
if (errorCode == S_OK && printStatus == COREWEBVIEW2_PRINT_STATUS_SUCCEEDED)
1004+
{
1005+
message = L"Printing " + std::wstring(title.get()) +
1006+
L" document to printer is succedded";
1007+
}
1008+
else if (
1009+
errorCode == S_OK &&
1010+
printStatus == COREWEBVIEW2_PRINT_STATUS_PRINTER_UNAVAILABLE)
1011+
{
1012+
message = L"Selected printer is not found, not available, offline or "
1013+
L"error state.";
1014+
}
1015+
else if (errorCode == E_INVALIDARG)
1016+
{
1017+
message = L"Invalid settings provided for the specified printer";
1018+
}
1019+
else if (errorCode == E_ABORT)
1020+
{
1021+
message = L"Printing " + std::wstring(title.get()) +
1022+
L" document already in progress";
1023+
}
1024+
else
1025+
{
1026+
message = L"Printing " + std::wstring(title.get()) +
1027+
L" document to printer is failed";
1028+
}
1029+
1030+
AsyncMessageBox(message, L"Print to printer");
1031+
1032+
return S_OK;
1033+
})
1034+
.Get()));
1035+
return true;
1036+
}
1037+
//! [PrintToPrinter]
1038+
1039+
// Function to display current web page pdf data in a custom print preview dialog.
1040+
static void DisplayPdfDataInPrintDialog(IStream* pdfData)
1041+
{
1042+
// You can display the printable pdf data in a custom print preview dialog to the end user.
1043+
}
1044+
1045+
//! [PrintToPdfStream]
1046+
// This example prints the Pdf data of the current page to a stream.
1047+
bool AppWindow::PrintToPdfStream()
1048+
{
1049+
wil::com_ptr<ICoreWebView2Experimental17> webView2Experimental17;
1050+
CHECK_FAILURE(m_webView->QueryInterface(IID_PPV_ARGS(&webView2Experimental17)));
1051+
1052+
wil::unique_cotaskmem_string title;
1053+
CHECK_FAILURE(m_webView->get_DocumentTitle(&title));
1054+
1055+
// Passing nullptr for `ICoreWebView2PrintSettings` results in default print settings used.
1056+
CHECK_FAILURE(webView2Experimental17->PrintToPdfStream(
1057+
nullptr,
1058+
Callback<ICoreWebView2ExperimentalPrintToPdfStreamCompletedHandler>(
1059+
[title = std::move(title), this](HRESULT errorCode, IStream* pdfData) -> HRESULT
1060+
{
1061+
DisplayPdfDataInPrintDialog(pdfData);
1062+
1063+
std::wstring message =
1064+
L"Printing " + std::wstring(title.get()) + L" document to PDF Stream " +
1065+
((errorCode == S_OK && pdfData != nullptr) ? L"succedded" : L"failed");
1066+
1067+
AsyncMessageBox(message, L"Print to PDF Stream");
1068+
1069+
return S_OK;
1070+
})
1071+
.Get()));
1072+
return true;
1073+
}
1074+
//! [PrintToPdfStream]
1075+
7981076
// Message handler for about dialog.
7991077
INT_PTR CALLBACK AppWindow::About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
8001078
{
@@ -924,6 +1202,13 @@ void AppWindow::InitializeWebView()
9241202
}
9251203
//! [CoreWebView2CustomSchemeRegistration]
9261204

1205+
Microsoft::WRL::ComPtr<ICoreWebView2ExperimentalEnvironmentOptions2> optionsExperimental2;
1206+
if (options.As(&optionsExperimental2) == S_OK)
1207+
{
1208+
CHECK_FAILURE(optionsExperimental2->put_IsCustomCrashReportingEnabled(
1209+
m_CustomCrashReportingEnabled ? TRUE : FALSE));
1210+
}
1211+
9271212
HRESULT hr = CreateCoreWebView2EnvironmentWithOptions(
9281213
subFolder, m_userDataFolder.c_str(), options.Get(),
9291214
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(

SampleApps/WebView2APISample/AppWindow.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ struct WebViewCreateOption
6161
void PopupDialog(AppWindow* app);
6262
};
6363

64+
// SamplePrintSettings also defaults to the defaults of the ICoreWebView2PrintSettings
65+
// defaults.
66+
struct SamplePrintSettings
67+
{
68+
COREWEBVIEW2_PRINT_ORIENTATION Orientation = COREWEBVIEW2_PRINT_ORIENTATION_PORTRAIT;
69+
int Copies = 1;
70+
int PagesPerSide = 1;
71+
std::wstring Pages = L"";
72+
COREWEBVIEW2_PRINT_COLLATION Collation = COREWEBVIEW2_PRINT_COLLATION_DEFAULT;
73+
COREWEBVIEW2_PRINT_COLOR_MODE ColorMode = COREWEBVIEW2_PRINT_COLOR_MODE_DEFAULT;
74+
COREWEBVIEW2_PRINT_DUPLEX Duplex = COREWEBVIEW2_PRINT_DUPLEX_DEFAULT;
75+
COREWEBVIEW2_PRINT_MEDIA_SIZE Media = COREWEBVIEW2_PRINT_MEDIA_SIZE_DEFAULT;
76+
double PaperWidth = 8.5;
77+
double PaperHeight = 11;
78+
double ScaleFactor = 1.0;
79+
bool PrintBackgrounds = false;
80+
bool HeaderAndFooter = false;
81+
bool ShouldPrintSelectionOnly = false;
82+
std::wstring HeaderTitle = L"";
83+
std::wstring FooterUri = L"";
84+
};
85+
6486
class AppWindow
6587
{
6688
public:
@@ -180,11 +202,18 @@ class AppWindow
180202
bool ClearBrowsingData(COREWEBVIEW2_BROWSING_DATA_KINDS dataKinds);
181203
void UpdateAppTitle();
182204
void ToggleExclusiveUserDataFolderAccess();
205+
void ToggleCustomCrashReporting();
183206
#ifdef USE_WEBVIEW2_WIN10
184207
void OnTextScaleChanged(
185208
winrt::Windows::UI::ViewManagement::UISettings const& uiSettings,
186209
winrt::Windows::Foundation::IInspectable const& args);
187210
#endif
211+
bool ShowPrintUI(COREWEBVIEW2_PRINT_DIALOG_KIND printDialogKind);
212+
bool PrintToDefaultPrinter();
213+
bool PrintToPrinter();
214+
std::wstring GetPrinterName();
215+
SamplePrintSettings GetSelectedPrinterPrintSettings(std::wstring printerName);
216+
bool PrintToPdfStream();
188217

189218
std::wstring GetLocalPath(std::wstring path, bool keep_exe_path);
190219
void DeleteAllComponents();
@@ -237,6 +266,8 @@ class AppWindow
237266
bool m_AADSSOEnabled = false;
238267
bool m_ExclusiveUserDataFolderAccess = false;
239268

269+
bool m_CustomCrashReportingEnabled = false;
270+
240271
// Fullscreen related code
241272
RECT m_previousWindowRect;
242273
HMENU m_hMenu;

0 commit comments

Comments
 (0)