forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewComponent.cpp
More file actions
215 lines (195 loc) · 6.08 KB
/
ViewComponent.cpp
File metadata and controls
215 lines (195 loc) · 6.08 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// 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 "ViewComponent.h"
#include <d2d1helper.h>
#include <sstream>
#include <windowsx.h>
#include "CheckFailure.h"
using namespace Microsoft::WRL;
ViewComponent::ViewComponent(AppWindow* appWindow)
: m_appWindow(appWindow), m_host(appWindow->GetWebViewHost()),
m_webView(appWindow->GetWebView())
{
//! [ZoomFactorChanged]
// Register a handler for the ZoomFactorChanged event.
// This handler just announces the new level of zoom on the window's title bar.
CHECK_FAILURE(m_host->add_ZoomFactorChanged(
Callback<ICoreWebView2ZoomFactorChangedEventHandler>(
[this](ICoreWebView2Host* sender, IUnknown* args) -> HRESULT {
double zoomFactor;
CHECK_FAILURE(sender->get_ZoomFactor(&zoomFactor));
std::wstring message = L"WebView2APISample (Zoom: " +
std::to_wstring(int(zoomFactor * 100)) + L"%)";
SetWindowText(m_appWindow->GetMainWindow(), message.c_str());
return S_OK;
})
.Get(),
&m_zoomFactorChangedToken));
//! [ZoomFactorChanged]
ResizeWebView();
}
bool ViewComponent::HandleWindowMessage(
HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* result)
{
if (message == WM_COMMAND)
{
switch (LOWORD(wParam))
{
case IDM_TOGGLE_VISIBILITY:
ToggleVisibility();
return true;
case IDM_ZOOM_05:
SetZoomFactor(0.5f);
return true;
case IDM_ZOOM_10:
SetZoomFactor(1.0f);
return true;
case IDM_ZOOM_20:
SetZoomFactor(2.0f);
return true;
case IDM_SIZE_25:
SetSizeRatio(0.5f);
return true;
case IDM_SIZE_50:
SetSizeRatio(0.7071f);
return true;
case IDM_SIZE_75:
SetSizeRatio(0.866f);
return true;
case IDM_SIZE_100:
SetSizeRatio(1.0f);
return true;
case IDM_SCALE_50:
SetScale(0.5f);
return true;
case IDM_SCALE_100:
SetScale(1.0f);
return true;
case IDM_SCALE_125:
SetScale(1.25f);
return true;
case IDM_SCALE_150:
SetScale(1.5f);
return true;
case IDM_GET_WEBVIEW_BOUNDS:
ShowWebViewBounds();
return true;
case IDM_GET_WEBVIEW_ZOOM:
ShowWebViewZoom();
return true;
}
}
//! [ToggleIsVisibleOnMinimize]
if (message == WM_SYSCOMMAND)
{
if (wParam == SC_MINIMIZE)
{
// Hide the webview when the app window is minimized.
m_host->put_IsVisible(FALSE);
}
else if (wParam == SC_RESTORE)
{
// When the app window is restored, show the webview
// (unless the user has toggle visibility off).
if (m_isVisible)
{
m_host->put_IsVisible(TRUE);
}
}
}
//! [ToggleIsVisibleOnMinimize]
//! [NotifyParentWindowPositionChanged]
if (message == WM_MOVE || message == WM_MOVING)
{
m_host->NotifyParentWindowPositionChanged();
return true;
}
//! [NotifyParentWindowPositionChanged]
return false;
}
//! [ToggleIsVisible]
void ViewComponent::ToggleVisibility()
{
BOOL visible;
m_host->get_IsVisible(&visible);
m_isVisible = !visible;
m_host->put_IsVisible(m_isVisible);
}
//! [ToggleIsVisible]
void ViewComponent::SetSizeRatio(float ratio)
{
m_webViewRatio = ratio;
ResizeWebView();
}
void ViewComponent::SetZoomFactor(float zoom)
{
m_webViewZoomFactor = zoom;
m_host->put_ZoomFactor(zoom);
}
void ViewComponent::SetBounds(RECT bounds)
{
m_webViewBounds = bounds;
ResizeWebView();
}
//! [SetBoundsAndZoomFactor]
void ViewComponent::SetScale(float scale)
{
RECT bounds;
CHECK_FAILURE(m_host->get_Bounds(&bounds));
double scaleChange = scale / m_webViewScale;
bounds.bottom = LONG(
(bounds.bottom - bounds.top) * scaleChange + bounds.top);
bounds.right = LONG(
(bounds.right - bounds.left) * scaleChange + bounds.left);
m_webViewScale = scale;
m_host->SetBoundsAndZoomFactor(bounds, scale);
}
//! [SetBoundsAndZoomFactor]
//! [ResizeWebView]
// Update the bounds of the WebView window to fit available space.
void ViewComponent::ResizeWebView()
{
SIZE webViewSize = {
LONG((m_webViewBounds.right - m_webViewBounds.left) * m_webViewRatio * m_webViewScale),
LONG((m_webViewBounds.bottom - m_webViewBounds.top) * m_webViewRatio * m_webViewScale) };
RECT desiredBounds = m_webViewBounds;
desiredBounds.bottom = LONG(
webViewSize.cy + m_webViewBounds.top);
desiredBounds.right = LONG(
webViewSize.cx + m_webViewBounds.left);
m_host->put_Bounds(desiredBounds);
}
//! [ResizeWebView]
// Show the current bounds of the WebView.
void ViewComponent::ShowWebViewBounds()
{
RECT bounds;
HRESULT result = m_host->get_Bounds(&bounds);
if (SUCCEEDED(result))
{
std::wstringstream message;
message << L"Left:\t" << bounds.left << L"\n"
<< L"Top:\t" << bounds.top << L"\n"
<< L"Right:\t" << bounds.right << L"\n"
<< L"Bottom:\t" << bounds.bottom << std::endl;
MessageBox(nullptr, message.str().c_str(), L"WebView Bounds", MB_OK);
}
}
// Show the current zoom factor of the WebView.
void ViewComponent::ShowWebViewZoom()
{
double zoomFactor;
HRESULT result = m_host->get_ZoomFactor(&zoomFactor);
if (SUCCEEDED(result))
{
std::wstringstream message;
message << L"Zoom Factor:\t" << zoomFactor << std::endl;
MessageBox(nullptr, message.str().c_str(), L"WebView Zoom Factor", MB_OK);
}
}
ViewComponent::~ViewComponent()
{
m_host->remove_ZoomFactorChanged(m_zoomFactorChangedToken);
}