forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileComponent.cpp
More file actions
97 lines (85 loc) · 3.13 KB
/
FileComponent.cpp
File metadata and controls
97 lines (85 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "FileComponent.h"
#include "CheckFailure.h"
#include <commdlg.h>
#include <shlwapi.h>
using namespace Microsoft::WRL;
FileComponent::FileComponent(AppWindow* appWindow)
: m_appWindow(appWindow), m_webView(appWindow->GetWebView())
{
//! [DocumentTitleChanged]
// Register a handler for the DocumentTitleChanged event.
// This handler just announces the new title on the window's title bar.
CHECK_FAILURE(m_webView->add_DocumentTitleChanged(
Callback<ICoreWebView2DocumentTitleChangedEventHandler>(
[this](ICoreWebView2* sender, IUnknown* args) -> HRESULT {
wil::unique_cotaskmem_string title;
CHECK_FAILURE(sender->get_DocumentTitle(&title));
SetWindowText(m_appWindow->GetMainWindow(), title.get());
return S_OK;
})
.Get(),
&m_documentTitleChangedToken));
//! [DocumentTitleChanged]
}
bool FileComponent::HandleWindowMessage(
HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* result)
{
if (message == WM_COMMAND)
{
switch (LOWORD(wParam))
{
case IDM_SAVE_SCREENSHOT:
SaveScreenshot();
return true;
case IDM_GET_DOCUMENT_TITLE:
{
wil::unique_cotaskmem_string title;
m_webView->get_DocumentTitle(&title);
MessageBox(m_appWindow->GetMainWindow(), title.get(), L"Document Title", MB_OK);
return true;
}
}
}
return false;
}
//! [CapturePreview]
// Show the user a file selection dialog, then save a screenshot of the WebView
// to the selected file.
void FileComponent::SaveScreenshot()
{
OPENFILENAME openFileName = {};
openFileName.lStructSize = sizeof(openFileName);
openFileName.hwndOwner = nullptr;
openFileName.hInstance = nullptr;
WCHAR fileName[MAX_PATH] = L"WebView2_Screenshot.png";
openFileName.lpstrFile = fileName;
openFileName.lpstrFilter = L"PNG File\0*.png\0";
openFileName.nMaxFile = ARRAYSIZE(fileName);
openFileName.Flags = OFN_OVERWRITEPROMPT;
if (GetSaveFileName(&openFileName))
{
wil::com_ptr<IStream> stream;
CHECK_FAILURE(SHCreateStreamOnFileEx(
fileName, STGM_READWRITE | STGM_CREATE, FILE_ATTRIBUTE_NORMAL, TRUE, nullptr,
&stream));
HWND mainWindow = m_appWindow->GetMainWindow();
CHECK_FAILURE(m_webView->CapturePreview(
COREWEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT_PNG, stream.get(),
Callback<ICoreWebView2CapturePreviewCompletedHandler>(
[mainWindow](HRESULT error_code) -> HRESULT {
CHECK_FAILURE(error_code);
MessageBox(mainWindow, L"Preview Captured", L"Preview Captured", MB_OK);
return S_OK;
})
.Get()));
}
}
//! [CapturePreview]
FileComponent::~FileComponent()
{
m_webView->remove_DocumentTitleChanged(m_documentTitleChangedToken);
}