[ToggleSilent] Turn on/off Silent SaveAs, which won't show the system default save as dialog. This example hides the default save as dialog and shows a customized dialog.
| 38 | // Turn on/off Silent SaveAs, which won't show the system default save as dialog. |
| 39 | // This example hides the default save as dialog and shows a customized dialog. |
| 40 | bool ScenarioSaveAs::ToggleSilent() |
| 41 | { |
| 42 | if (!m_webView2_25) |
| 43 | return false; |
| 44 | m_silentSaveAs = !m_silentSaveAs; |
| 45 | if (m_silentSaveAs && m_saveAsUIShowingToken.value == 0) |
| 46 | { |
| 47 | // Register a handler for the `SaveAsUIShowing` event. |
| 48 | m_webView2_25->add_SaveAsUIShowing( |
| 49 | Callback<ICoreWebView2SaveAsUIShowingEventHandler>( |
| 50 | [this](ICoreWebView2* sender, ICoreWebView2SaveAsUIShowingEventArgs* args) |
| 51 | -> HRESULT |
| 52 | { |
| 53 | // Hide the system default save as dialog. |
| 54 | CHECK_FAILURE(args->put_SuppressDefaultDialog(TRUE)); |
| 55 | |
| 56 | auto showCustomizedDialog = [this, args = wil::make_com_ptr(args)] |
| 57 | { |
| 58 | // Preview the content mime type, optional. |
| 59 | wil::unique_cotaskmem_string mimeType; |
| 60 | CHECK_FAILURE(args->get_ContentMimeType(&mimeType)); |
| 61 | |
| 62 | SaveAsDialog dialog(m_appWindow->GetMainWindow(), saveAsKind); |
| 63 | if (dialog.confirmed) |
| 64 | { |
| 65 | // Set the SaveAsFilePath, Kind, AllowReplace for the event |
| 66 | // args from this customized dialog inputs, optional. If nothing |
| 67 | // needs to input, the event args will provide default values. |
| 68 | CHECK_FAILURE(args->put_SaveAsFilePath(dialog.path.c_str())); |
| 69 | CHECK_FAILURE(args->put_Kind(dialog.selectedKind)); |
| 70 | CHECK_FAILURE(args->put_AllowReplace(dialog.allowReplace)); |
| 71 | |
| 72 | BOOL allowReplace; |
| 73 | CHECK_FAILURE(args->get_AllowReplace(&allowReplace)); |
| 74 | wil::unique_cotaskmem_string path; |
| 75 | CHECK_FAILURE(args->get_SaveAsFilePath(&path)); |
| 76 | COREWEBVIEW2_SAVE_AS_KIND selectedKind; |
| 77 | CHECK_FAILURE(args->get_Kind(&selectedKind)); |
| 78 | |
| 79 | // Preview silent save as event args inputs, optional. |
| 80 | MessageBox( |
| 81 | m_appWindow->GetMainWindow(), |
| 82 | (std::wstring(L"Content Mime Type: ") + mimeType.get() + L"\n" + |
| 83 | L"Fullpath: " + path.get() + L"\n" + L"Allow Replace: " + |
| 84 | (allowReplace ? L"true" : L"false") + L"\n" + |
| 85 | L"Selected Save As Kind: " + saveAsKindString[selectedKind]) |
| 86 | .c_str(), |
| 87 | L"Silent Save As Parameters Preview", MB_OK); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | // Save As cancelled from this customized dialog. |
| 92 | CHECK_FAILURE(args->put_Cancel(TRUE)); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | wil::com_ptr<ICoreWebView2Deferral> deferral; |
| 97 | CHECK_FAILURE(args->GetDeferral(&deferral)); |
nothing calls this directly
no test coverage detected