admin
2020-06-10 a610f2ab6e543d2cb78c1ef212ac6a74ddc067d9
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright © 2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using CefSharp.Internals;
 
namespace CefSharp.WinForms.Internals
{
    /// <summary>
    /// ParentFormMessageInterceptor - hooks into the parent forms
    /// message loop to incercept messages like WM_MOVE
    /// </summary>
    /// <seealso cref="System.Windows.Forms.NativeWindow" />
    /// <seealso cref="System.IDisposable" />
    internal class ParentFormMessageInterceptor : NativeWindow, IDisposable
    {
        /// <summary>
        /// Keep track of whether a move is in progress.
        /// </summary>
        private bool isMoving;
 
        /// <summary>
        /// Used to determine the coordinates involved in the move
        /// </summary>
        private Rectangle movingRectangle;
 
        /// <summary>
        /// Store the previous window state, used to determine if the
        /// Windows was previously <see cref="FormWindowState.Minimized"/>
        /// and resume rendering
        /// </summary>
        private FormWindowState previousWindowState;
 
        /// <summary>
        /// Gets or sets the browser.
        /// </summary>
        /// <value>The browser.</value>
        private ChromiumWebBrowser Browser { get; set; }
 
        /// <summary>
        /// Gets or sets the parent form.
        /// </summary>
        /// <value>The parent form.</value>
        private Form ParentForm { get; set; }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="ParentFormMessageInterceptor"/> class.
        /// </summary>
        /// <param name="browser">The browser.</param>
        public ParentFormMessageInterceptor(ChromiumWebBrowser browser)
        {
            Browser = browser;
            // Get notified if our browser window parent changes:
            Browser.ParentChanged += ParentParentChanged;
            // Find the browser form to subclass to monitor WM_MOVE/WM_MOVING
            RefindParentForm();
        }
 
        /// <summary>
        /// Call to force refinding of the parent Form.
        /// (i.e. top level window that owns the ChromiumWebBrowserControl)
        /// </summary>
        public void RefindParentForm()
        {
            ParentParentChanged(Browser, null);
        }
 
        /// <summary>
        /// Adjust the form to listen to if the ChromiumWebBrowserControl's parent changes.
        /// </summary>
        /// <param name="sender">The ChromiumWebBrowser whose parent has changed.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void ParentParentChanged(object sender, EventArgs e)
        {
            var control = (Control)sender;
            var oldForm = ParentForm;
            var newForm = control.FindForm();
 
            if (oldForm == null || newForm == null || oldForm.Handle != newForm.Handle)
            {
                if (Handle != IntPtr.Zero)
                {
                    ReleaseHandle();
                }
                if (oldForm != null)
                {
                    oldForm.HandleCreated -= OnHandleCreated;
                    oldForm.HandleDestroyed -= OnHandleDestroyed;
                    oldForm.Resize -= OnResize;
                }
                ParentForm = newForm;
                if (newForm != null)
                {
                    newForm.HandleCreated += OnHandleCreated;
                    newForm.HandleDestroyed += OnHandleDestroyed;
                    newForm.Resize += OnResize;
 
                    previousWindowState = newForm.WindowState;
 
                    // If newForm's Handle has been created already,
                    // our event listener won't be called, so call it now.
                    if (newForm.IsHandleCreated)
                    {
                        OnHandleCreated(newForm, null);
                    }
                }
            }
        }
 
        private void OnResize(object sender, EventArgs e)
        {
            var form = (Form)sender;
 
            if (previousWindowState == form.WindowState)
            {
                return;
            }
 
            switch (form.WindowState)
            {
                case FormWindowState.Normal:
                case FormWindowState.Maximized:
                {
                    if (previousWindowState == FormWindowState.Minimized)
                    {
                        Browser?.ShowInternal();
                    }
                    break;
                }
                case FormWindowState.Minimized:
                {
                    Browser?.HideInternal();
 
                    break;
                }
            }
 
            previousWindowState = form.WindowState;
        }
 
        /// <summary>
        /// Handles the <see cref="E:HandleCreated" /> event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void OnHandleCreated(object sender, EventArgs e)
        {
            AssignHandle(((Form)sender).Handle);
        }
 
        /// <summary>
        /// Handles the <see cref="E:HandleDestroyed" /> event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void OnHandleDestroyed(object sender, EventArgs e)
        {
            ReleaseHandle();
        }
 
        /// <summary>
        /// Invokes the default window procedure associated with this window.
        /// </summary>
        /// <param name="m">A <see cref="T:System.Windows.Forms.Message" /> that is associated with the current Windows message.</param>
        protected override void WndProc(ref Message m)
        {
            var isMovingMessage = false;
 
            // Negative initial values keeps the compiler quiet and to
            // ensure we have actual window movement to notify CEF about.
            const int invalidMoveCoordinate = -1;
            var x = invalidMoveCoordinate;
            var y = invalidMoveCoordinate;
 
            // Listen for operating system messages 
            switch (m.Msg)
            {
                case NativeMethods.WM_ACTIVATE:
                {
                    // Intercept (de)activate messages for our form so that we can
                    // ensure that we play nicely with WinForms .ActiveControl
                    // tracking.
                    var browser = Browser;
                    if ((int)m.WParam == 0x0) // WA_INACTIVE
                    {
                        // If the CEF browser no longer has focus,
                        // we won't get a call to OnLostFocus on ChromiumWebBrowser.
                        // However, it doesn't matter so much since the CEF
                        // browser will receive it instead.
 
                        // Paranoia about making sure the IsActivating state is correct now.
                        browser.IsActivating = false;
                        DefWndProc(ref m);
                    }
                    else // WA_ACTIVE or WA_CLICKACTIVE
                    {
                        // Only set IsActivating if the ChromiumWebBrowser was the form's
                        // ActiveControl before the last deactivation.
                        browser.IsActivating = browser.IsActiveControl();
                        DefWndProc(ref m);
                        // During activation, WM_SETFOCUS gets sent to
                        // to the CEF control since it's the root window
                        // of the CEF UI thread.
                        //
                        // Therefore, don't set .IsActivating to false here
                        // instead do so in DefaultFocusHandler.OnGotFocus.
                        // Otherwise there's a race condition between this
                        // thread setting activating to false and
                        // the CEF DefaultFocusHandler executing to determine
                        // it shouldn't Activate() the control.
                    }
                    return;
                }
                case NativeMethods.WM_MOVING:
                {
                    movingRectangle = (Rectangle)Marshal.PtrToStructure(m.LParam, typeof(Rectangle));
                    x = movingRectangle.Left;
                    y = movingRectangle.Top;
                    isMovingMessage = true;
                    break;
                }
                case NativeMethods.WM_MOVE:
                {
                    // Convert IntPtr into 32bit int safely without 
                    // exceptions:
                    int dwLParam = m.LParam.CastToInt32();
 
                    // Extract coordinates from lo/hi word:
                    x = dwLParam & 0xffff;
                    y = (dwLParam >> 16) & 0xffff;
 
                    isMovingMessage = true;
                    break;
                }
            }
 
            // Only notify about movement if:
            // * Browser Handle Created
            //   NOTE: This is checked for paranoia. 
            //         This WndProc can't be called unless ParentForm has 
            //         its handle created, but that doesn't necessarily mean 
            //         Browser has had its handle created.
            //         WinForm controls don't usually get eagerly created Handles
            //         in their constructors.
            // * ParentForm Actually moved
            // * Not currently moving (on the UI thread only of course)
            // * The current WindowState is Normal.
            //      This check is to simplify the effort here.
            //      Other logic already handles the maximize/minimize
            //      cases just fine.
            // You might consider checking Browser.Visible and
            // not notifying our browser control if the browser control isn't visible.
            // However, if you do that, the non-Visible CEF tab will still
            // have any SELECT drop downs rendering where they shouldn't.
            if (isMovingMessage
                && Browser.IsHandleCreated
                && ParentForm.WindowState == FormWindowState.Normal
                && (ParentForm.Left != x || ParentForm.Top != y)
                && !isMoving)
            {
                // ParentForm.Left & .Right are negative when the window
                // is transitioning from maximized to normal.
                // If we are transitioning, the form will also receive
                // a WM_SIZE which can deal with the move/size combo itself.
                if (ParentForm.Left >= 0 && ParentForm.Right >= 0)
                {
                    OnMoving();
                }
            }
            DefWndProc(ref m);
        }
 
        /// <summary>
        /// Called when [moving].
        /// </summary>
        protected virtual void OnMoving()
        {
            isMoving = true;
 
            if (Browser.IsBrowserInitialized)
            {
                Browser.GetBrowser().GetHost().NotifyMoveOrResizeStarted();
            }
 
            isMoving = false;
        }
 
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
        }
 
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (ParentForm != null)
                {
                    ParentForm.HandleCreated -= OnHandleCreated;
                    ParentForm.HandleDestroyed -= OnHandleDestroyed;
                    ParentForm.Resize -= OnResize;
                    ParentForm = null;
                }
 
                // Unmanaged resource, but release here anyway.
                // NativeWindow has its own finalization logic 
                // that should be run if this instance isn't disposed
                // properly before arriving at the finalization thread.
                // See: http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/NativeWindow.cs,147
                // for the gruesome details.
                if (Handle != IntPtr.Zero)
                {
                    ReleaseHandle();
                }
 
                if (Browser != null)
                {
                    Browser.ParentChanged -= ParentParentChanged;
                    Browser = null;
                }
            }
        }
 
        /// <summary>
        /// When overridden in a derived class, manages an unhandled thread exception.
        /// </summary>
        /// <param name="e">An <see cref="T:System.Exception" /> that specifies the unhandled thread exception.</param>
        protected override void OnThreadException(Exception e)
        {
            // TODO: Do something more interesting here, logging, whatever, something.
            base.OnThreadException(e);
        }
    }
}