WPF - ChromiumWebBrowser inherits from Control rather than UserControl

Based on #1499

Popup is created in xaml
Canvas used to absolute position the two images, one on top of the other
No Mouse.Capture required
This commit is contained in:
amaitland
2018-01-18 13:15:30 +10:00
parent 67b8b038af
commit c021be6c8d
3 changed files with 51 additions and 65 deletions
+6 -1
View File
@@ -102,7 +102,12 @@
<Name>CefSharp</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Page Include="Themes\Generic.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
+15 -64
View File
@@ -24,10 +24,10 @@ namespace CefSharp.Wpf
/// <summary>
/// ChromiumWebBrowser is the WPF web browser control
/// </summary>
/// <seealso cref="System.Windows.Controls.ContentControl" />
/// <seealso cref="System.Windows.Controls.Control" />
/// <seealso cref="CefSharp.Internals.IRenderWebBrowser" />
/// <seealso cref="CefSharp.Wpf.IWpfWebBrowser" />
public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrowser
public class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBrowser
{
/// <summary>
/// The source
@@ -71,10 +71,6 @@ namespace CefSharp.Wpf
/// </summary>
private Image popupImage;
/// <summary>
/// The popup
/// </summary>
private Popup popup;
/// <summary>
/// The browser
/// </summary>
private IBrowser browser;
@@ -379,6 +375,10 @@ namespace CefSharp.Wpf
/// </summary>
static ChromiumWebBrowser()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(ChromiumWebBrowser),
new FrameworkPropertyMetadata(typeof(ChromiumWebBrowser)));
if (CefSharpSettings.ShutdownOnExit)
{
var app = Application.Current;
@@ -472,7 +472,6 @@ namespace CefSharp.Wpf
PresentationSource.AddSourceChangedHandler(this, PresentationSourceChangedHandler);
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
UseLayoutRounding = true;
}
@@ -544,13 +543,6 @@ namespace CefSharp.Wpf
IsVisibleChanged -= OnIsVisibleChanged;
if(popup != null)
{
popup.Opened -= PopupOpened;
popup.Closed -= PopupClosed;
popup = null;
}
if (tooltipTimer != null)
{
tooltipTimer.Tick -= OnTooltipTimerTick;
@@ -759,7 +751,7 @@ namespace CefSharp.Wpf
/// <param name="isOpen">if set to <c>true</c> [is open].</param>
void IRenderWebBrowser.SetPopupIsOpen(bool isOpen)
{
UiThreadRunAsync(() => { popup.IsOpen = isOpen; });
UiThreadRunAsync(() => { popupImage.Visibility = isOpen ? Visibility.Visible : Visibility.Hidden; });
}
/// <summary>
@@ -1780,55 +1772,15 @@ namespace CefSharp.Wpf
if (image == null)
{
// Create main window
Content = image = CreateImage();
image = (Image)GetTemplateChild("PART_image");
}
if (popup == null)
if (popupImage == null)
{
popup = CreatePopup();
popupImage = (Image)GetTemplateChild("PART_popupImage");
}
}
/// <summary>
/// Creates the image.
/// </summary>
/// <returns>Image.</returns>
private Image CreateImage()
{
var img = new Image();
BindingOperations.SetBinding(img, RenderOptions.BitmapScalingModeProperty, new Binding
{
Path = new PropertyPath(RenderOptions.BitmapScalingModeProperty),
Source = this,
});
img.Stretch = Stretch.None;
img.HorizontalAlignment = HorizontalAlignment.Left;
img.VerticalAlignment = VerticalAlignment.Top;
return img;
}
/// <summary>
/// Creates the popup.
/// </summary>
/// <returns>Popup.</returns>
private Popup CreatePopup()
{
var newPopup = new Popup
{
Child = popupImage = CreateImage(),
PlacementTarget = this,
Placement = PlacementMode.Absolute,
};
newPopup.Opened += PopupOpened;
newPopup.Closed += PopupClosed;
return newPopup;
}
/// <summary>
/// WindowProc callback interceptor. Handles Windows messages intended for the source hWnd, and passes them to the
/// contained browser as needed.
@@ -1903,13 +1855,12 @@ namespace CefSharp.Wpf
/// <param name="y">The y.</param>
private void SetPopupSizeAndPositionImpl(int width, int height, int x, int y)
{
popup.Width = width ;
popup.Height = height;
popupImage.Width = width;
popupImage.Height = height;
var popupOffset = new Point(x, y);
var locationFromScreen = PointToScreen(popupOffset);
popup.HorizontalOffset = locationFromScreen.X / DpiScaleFactor;
popup.VerticalOffset = locationFromScreen.Y / DpiScaleFactor;
//TODO: Test with High DPI
popupImage.SetValue(Canvas.LeftProperty, x / DpiScaleFactor);
popupImage.SetValue(Canvas.TopProperty, y / DpiScaleFactor);
}
/// <summary>
+30
View File
@@ -0,0 +1,30 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CefSharp.Wpf">
<Style TargetType="{x:Type local:ChromiumWebBrowser}">
<Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ChromiumWebBrowser}">
<Canvas>
<Image Name="PART_image"
Stretch="None"
Canvas.Left="0"
Canvas.Bottom="0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
RenderOptions.BitmapScalingMode="{TemplateBinding Property=RenderOptions.BitmapScalingMode}" />
<Image Name="PART_popupImage"
Stretch="None"
Visibility="Hidden"
HorizontalAlignment="Left"
VerticalAlignment="Top"
RenderOptions.BitmapScalingMode="{TemplateBinding Property=RenderOptions.BitmapScalingMode}" />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>