-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebBrowserOverlayWF.cs
94 lines (79 loc) · 3.61 KB
/
WebBrowserOverlayWF.cs
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
using System.Windows.Forms;
using System.Windows;
using System.Diagnostics;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using System.ComponentModel;
namespace WebBrowserOnTransparentWindow
{
/// <summary>
/// Displays a WinForms.WebBrowser control over a given placement target element in a WPF Window.
/// Applies the opacity of the Window to the WebBrowser control.
/// </summary>
class WebBrowserOverlayWF
{
Window _owner;
FrameworkElement _placementTarget;
Form _form; // the top-level window holding the WebBrowser control
WebBrowser _wb = new WebBrowser();
public WebBrowser WebBrowser { get { return _wb; } }
public WebBrowserOverlayWF(FrameworkElement placementTarget)
{
_placementTarget = placementTarget;
Window owner = Window.GetWindow(placementTarget);
Debug.Assert(owner != null);
_owner = owner;
_form = new Form();
_form.Opacity = owner.Opacity;
_form.ShowInTaskbar = false;
_form.FormBorderStyle = FormBorderStyle.None;
_wb.Dock = DockStyle.Fill;
_form.Controls.Add(_wb);
//owner.SizeChanged += delegate { OnSizeLocationChanged(); };
owner.LocationChanged += delegate { OnSizeLocationChanged(); };
_placementTarget.SizeChanged += delegate { OnSizeLocationChanged(); };
if (owner.IsVisible)
InitialShow();
else
owner.SourceInitialized += delegate
{
InitialShow();
};
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(UIElement.OpacityProperty, typeof(Window));
dpd.AddValueChanged(owner, delegate { _form.Opacity = _owner.Opacity; });
_form.FormClosing += delegate { _owner.Close(); };
}
void InitialShow()
{
NativeWindow owner = new NativeWindow();
owner.AssignHandle(((HwndSource)HwndSource.FromVisual(_owner)).Handle);
_form.Show(owner);
owner.ReleaseHandle();
}
DispatcherOperation _repositionCallback;
void OnSizeLocationChanged()
{
// To reduce flicker when transparency is applied without DWM composition,
// do resizing at lower priority.
if (_repositionCallback == null)
_repositionCallback = _owner.Dispatcher.BeginInvoke(Reposition, DispatcherPriority.Input);
}
void Reposition()
{
_repositionCallback = null;
Point offset = _placementTarget.TranslatePoint(new Point(), _owner);
Point size = new Point(_placementTarget.ActualWidth, _placementTarget.ActualHeight);
HwndSource hwndSource = (HwndSource)HwndSource.FromVisual(_owner);
CompositionTarget ct = hwndSource.CompositionTarget;
offset = ct.TransformToDevice.Transform(offset);
size = ct.TransformToDevice.Transform(size);
Win32.POINT screenLocation = new Win32.POINT(offset);
Win32.ClientToScreen(hwndSource.Handle, ref screenLocation);
Win32.POINT screenSize = new Win32.POINT(size);
Win32.MoveWindow(_form.Handle, screenLocation.X, screenLocation.Y, screenSize.X, screenSize.Y, true);
//_form.SetBounds(screenLocation.X, screenLocation.Y, screenSize.X, screenSize.Y);
//_form.Update();
}
};
}