Skip to content

Commit 453fc77

Browse files
authored
Merge 4f67c2a into a5be9e4
2 parents a5be9e4 + 4f67c2a commit 453fc77

File tree

1 file changed

+73
-55
lines changed

1 file changed

+73
-55
lines changed

CefSharp.WinForms.Example/BrowserTabUserControl.cs

Lines changed: 73 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Runtime.InteropServices;
9-
using System.Threading;
109
using System.Threading.Tasks;
1110
using System.Windows.Forms;
1211
using CefSharp.Example;
@@ -232,71 +231,90 @@ private void OnIsBrowserInitializedChanged(object sender, IsBrowserInitializedCh
232231
// which running in a different thread we have to use a NativeWindow
233232
if (multiThreadedMessageLoopEnabled)
234233
{
235-
Task.Run(() =>
234+
SetupMessageInterceptor();
235+
}
236+
}
237+
}
238+
239+
private void SetupMessageInterceptor()
240+
{
241+
if (messageInterceptor != null)
242+
{
243+
messageInterceptor.ReleaseHandle();
244+
messageInterceptor = null;
245+
}
246+
247+
Task.Run(async () =>
248+
{
249+
try
250+
{
251+
while (true)
236252
{
237-
try
253+
IntPtr chromeWidgetHostHandle;
254+
if (ChromeWidgetHandleFinder.TryFindHandle(browserHandle, out chromeWidgetHostHandle))
238255
{
239-
while (true)
256+
messageInterceptor = new ChromeWidgetMessageInterceptor((Control)Browser, chromeWidgetHostHandle, message =>
240257
{
241-
IntPtr chromeWidgetHostHandle;
242-
if (ChromeWidgetHandleFinder.TryFindHandle(browserHandle, out chromeWidgetHostHandle))
258+
const int WM_MOUSEACTIVATE = 0x0021;
259+
const int WM_NCLBUTTONDOWN = 0x00A1;
260+
const int WM_LBUTTONDOWN = 0x0201;
261+
const int WM_DESTROY = 0x0002;
262+
263+
// Render process switch happened, need to find the new handle
264+
if (message.Msg == WM_DESTROY)
243265
{
244-
messageInterceptor = new ChromeWidgetMessageInterceptor((Control)Browser, chromeWidgetHostHandle, message =>
245-
{
246-
const int WM_MOUSEACTIVATE = 0x0021;
247-
const int WM_NCLBUTTONDOWN = 0x00A1;
248-
const int WM_LBUTTONDOWN = 0x0201;
249-
250-
if (message.Msg == WM_MOUSEACTIVATE)
251-
{
252-
// The default processing of WM_MOUSEACTIVATE results in MA_NOACTIVATE,
253-
// and the subsequent mouse click is eaten by Chrome.
254-
// This means any .NET ToolStrip or ContextMenuStrip does not get closed.
255-
// By posting a WM_NCLBUTTONDOWN message to a harmless co-ordinate of the
256-
// top-level window, we rely on the ToolStripManager's message handling
257-
// to close any open dropdowns:
258-
// http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ToolStripManager.cs,1249
259-
var topLevelWindowHandle = message.WParam;
260-
PostMessage(topLevelWindowHandle, WM_NCLBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
261-
}
262-
//Forward mouse button down message to browser control
263-
//else if(message.Msg == WM_LBUTTONDOWN)
264-
//{
265-
// PostMessage(browserHandle, WM_LBUTTONDOWN, message.WParam, message.LParam);
266-
//}
267-
268-
// The ChromiumWebBrowserControl does not fire MouseEnter/Move/Leave events, because Chromium handles these.
269-
// However we can hook into Chromium's messaging window to receive the events.
270-
//
271-
//const int WM_MOUSEMOVE = 0x0200;
272-
//const int WM_MOUSELEAVE = 0x02A3;
273-
//
274-
//switch (message.Msg) {
275-
// case WM_MOUSEMOVE:
276-
// Console.WriteLine("WM_MOUSEMOVE");
277-
// break;
278-
// case WM_MOUSELEAVE:
279-
// Console.WriteLine("WM_MOUSELEAVE");
280-
// break;
281-
//}
282-
});
283-
284-
break;
266+
SetupMessageInterceptor();
267+
return;
285268
}
286-
else
269+
270+
if (message.Msg == WM_MOUSEACTIVATE)
287271
{
288-
// Chrome hasn't yet set up its message-loop window.
289-
Thread.Sleep(10);
272+
// The default processing of WM_MOUSEACTIVATE results in MA_NOACTIVATE,
273+
// and the subsequent mouse click is eaten by Chrome.
274+
// This means any .NET ToolStrip or ContextMenuStrip does not get closed.
275+
// By posting a WM_NCLBUTTONDOWN message to a harmless co-ordinate of the
276+
// top-level window, we rely on the ToolStripManager's message handling
277+
// to close any open dropdowns:
278+
// http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ToolStripManager.cs,1249
279+
var topLevelWindowHandle = message.WParam;
280+
PostMessage(topLevelWindowHandle, WM_NCLBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
290281
}
291-
}
282+
//Forward mouse button down message to browser control
283+
//else if(message.Msg == WM_LBUTTONDOWN)
284+
//{
285+
// PostMessage(browserHandle, WM_LBUTTONDOWN, message.WParam, message.LParam);
286+
//}
287+
288+
// The ChromiumWebBrowserControl does not fire MouseEnter/Move/Leave events, because Chromium handles these.
289+
// However we can hook into Chromium's messaging window to receive the events.
290+
//
291+
//const int WM_MOUSEMOVE = 0x0200;
292+
//const int WM_MOUSELEAVE = 0x02A3;
293+
//
294+
//switch (message.Msg) {
295+
// case WM_MOUSEMOVE:
296+
// Console.WriteLine("WM_MOUSEMOVE");
297+
// break;
298+
// case WM_MOUSELEAVE:
299+
// Console.WriteLine("WM_MOUSELEAVE");
300+
// break;
301+
//}
302+
});
303+
304+
break;
292305
}
293-
catch
306+
else
294307
{
295-
// Errors are likely to occur if browser is disposed, and no good way to check from another thread
308+
// Chrome hasn't yet set up its message-loop window.
309+
await Task.Delay(10);
296310
}
297-
});
311+
}
298312
}
299-
}
313+
catch
314+
{
315+
// Errors are likely to occur if browser is disposed, and no good way to check from another thread
316+
}
317+
});
300318
}
301319

302320
private void DisplayOutput(string output)

0 commit comments

Comments
 (0)