首页 > 系统相关 >WPF应用中对WindowsFormHost内容进行裁剪

WPF应用中对WindowsFormHost内容进行裁剪

时间:2023-07-10 17:33:37浏览次数:45  
标签:控件 裁剪 WindowsFormHost path WPF 加载

问题1: 

  WPF中在使用WindowsFormsHost调用WinFrom控件时,若在WindowsFormsHost上层添加了WPF控件,该控件不会显示出来。

复制代码
 <Grid>
    <WindowsFormsHost Background="White">
          <Winfrm:WebBrowser x:Name="WinFrmWebBrowser"/>
    </WindowsFormsHost>
    <!--运行时 Ellipse 不会显示出来-->
    <Ellipse Width="100" Height="100" Fill="Red"/>
 </Grid>
复制代码

解决方案: 使用Popup对上层的WPF控件内容进行包装。

复制代码
<Style TargetType="{x:Type local:MyBrowser}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyBrowser}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Border x:Name="Part_BdrWinfrmHostContainer">
                            <WindowsFormsHost x:Name="Part_WinfrmHost" Background="Gray">
                                <Winfrm:WebBrowser x:Name="Part_WinFrmWebBrowser"/>
                            </WindowsFormsHost>
                        </Border>
                        <Popup x:Name="PART_Popup" IsOpen="True" Placement="Center" 
                               AllowsTransparency="True">
                            <!--所有WPF内容添加至这个Border里面-->
                            <Border x:Name="PART_Content"/>
                        </Popup>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
复制代码

 我测试时封装成了自定义的CustomControl。 对应的.cs文件中定义了Link、 Content两个依赖属性接收参数。

复制代码
<Grid x:Name="GdPopupWays" Grid.Column="1">
    <local:MyBrowser Link="http://www.baidu.com">
        <local:MyBrowser.Content>
            <!--local:OverLayer是自定义的UserControl-->
            <local:OverLayer/>
        </local:MyBrowser.Content>
    </local:MyBrowser>
</Grid>
复制代码

如右侧,我创建了一个黄色的Ellipse叠加在WindowsFormHost 上面成功呈现出来。(tips:我在WindowsFormHost 里面加载了WinForm的WebBrowser)。

问题2:

想要将加载在WindowsFormHost中的内容进行裁剪。

解决方案:WinForm控件的Region属性限制显示区域。 相当于WPF的Clip。 示例如下:

复制代码
GraphicsPath path = new GraphicsPath() { FillMode = FillMode.Winding };
path.StartFigure();
path.AddEllipse(new System.Drawing.Rectangle(0, 0, (int)182, (int)182));
path.AddRectangle(new System.Drawing.Rectangle(90, 0, 90, 90));
path.CloseFigure();
this.WinformRtx.Region = new Region(path);
复制代码

我的测试效果,WPF中用Image加载了一张星空图,右上角放置了WindowsFormHost内容。我对其进行了显示区域限制。

3:问题三

   两个WindowsFormHost叠加时,WindowsFormHost对于png的背景图不支持透明。如下图:

复制代码
<Grid x:Name="GdMain">
    <Image x:Name="ImgSky" Source="Sky.jpg" Stretch="Fill"/>

    <!--加载Winform的WebBrowser-->
    <WindowsFormsHost Background="White">
        <Winfrm:WebBrowser x:Name="WinFrmWebBrowser"/>
    </WindowsFormsHost>

    <WindowsFormsHost Width="182" Height="182" HorizontalAlignment="Right" 
                      x:Name="WinfrmHostOverlayer" VerticalAlignment="Top" 
                      Background="Transparent">
        <Winfrm:Panel x:Name="WinfrmPanel"/>
    </WindowsFormsHost>
</Grid>
复制代码

可以看到右上角的png边框分明(实时上我放的是一张三个角均为透明的圆形png)。 若对右上角的Winform Panel进行裁剪。

裁剪完后,下面一层的WindowsFormHost也被裁了,露出了我用Image加载的星空底图,如下图:

解决方案:将要加载的Winform控件放在一起,可以是在同一个Winform Panel下面,这时在进行裁剪就不会有问题。

复制代码
<Grid x:Name="GdMain">
    <Image x:Name="ImgSky" Source="Sky.jpg" Stretch="Fill"/>
    <WindowsFormsHost HorizontalAlignment="Right" 
                      x:Name="WinfrmHostOverlayer" VerticalAlignment="Top" 
                      Background="Transparent">
        <Winfrm:Panel x:Name="WinfrmPanel">
            <!--<Winfrm:WebBrowser x:Name="WinFrmWebBrowser"/>-->
            <!--<Winfrm:Panel x:Name="WinFrmSubPanel"/>-->
        </Winfrm:Panel>
    </WindowsFormsHost>
</Grid>
复制代码

 上文中WebBrowser我都加载的是www.baidu.com.  为了凸显效果,下图所示Demo加载的是腾讯企业邮箱主页。

 

标签:控件,裁剪,WindowsFormHost,path,WPF,加载
From: https://www.cnblogs.com/webenh/p/17541797.html

相关文章

  • WPF基础之样式设置和模板化(三)
    IsItemsHost属性在此示例中,一个必需的重要属性是IsItemsHost属性。IsItemsHost属性用于指示在ItemsControl(如处理项列表的ListBox控件)的模板中,生成的元素应放在什么位置。如果将StackPanel的这一属性设置为true,则添加到ListBox的所有项都将进入StackPanel。请注意,此......
  • WPF+Prism基础教程
    Prism框架介绍Prism是一个用于构建松耦合、可维护和可测试的XAML应用的框架,它支持所有还活着的基于XAML的平台,包括WPF、XamarinForms、WinUI和Uwp、Uno。Prism提供了一组设计模式的实现,这些模式有助于编写结构良好且可维护的XAML应用程序,包括MVVM、依赖项注入、命......
  • 数据增强之裁剪、翻转与旋转
    文章和代码已经归档至【Github仓库:<https://github.com/timerring/dive-into-AI>】或者公众号【AIShareLab】回复pytorch教程也可获取。数据增强DataAugmentation数据增强又称为数据增广,数据扩增,它是对训练集进行变换,使训练集更丰富,从而让模型更具泛化能力。技巧:debugconsole......
  • WPF Menu
     1:Menu基本使用<Menu  IsMainMenu="True">    <MenuItem Header="_File">        <MenuItem Header="Save">             <MenuItem.Icon>               <materialDesign......
  • wpf样式模板的使用
    <Windowx:Class="WpfApp1.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:x="http://schemas.micro......
  • WPF 关闭主窗口提示Application.Current为null处理
    今天发现在任务栏右键关闭应用时,窗口关闭了,但是进程仍然存在。经过定位发现然后视频在渲染时,使用了Application.Current.Dispatcher回到主线程的操作,但是主窗体Closing时,进程还没关闭Application.Current刚好被访问就会为null。方案1:一直轮询在执行的地方使用Application.Curre......
  • wpf基本布局控件 -- 01
    <Windowx:Class="WpfApp1.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.c......
  • Wpf Grid 控件常用属性
    Grid的网格布局控件,使用网格布局时候需要先确定行数列数。Grid.RowDefinitions设置行<Grid><Grid.RowDefinitions><RowDefinitionHeight="45"/><RowDefinition/><RowDefinitionHeight="20"/>......
  • WPF 在MVVM模式下应用动画
    一个简单的需求:当程序发生异常时候,在界面上动画显示异常信息。这个需求看似简单,只需要try……catch到异常,然后把异常的信息写入界面就OK了。但在MVVM时,就不是这么简单了。MVVM模式下,追求前后端的分离。然后catch到的异常,也只能在后台代码中。如果传递到前台呢?这自然就想到了Bin......
  • WPF Window 窗口 常用属性
    window窗口属性属性定义属性值注解 WindowStartupLocation 获取或设置窗口首次显示时的位置。 一个 WindowStartupLocation 值,指定窗口首次显示时的顶边/左边位置。默认值为 Manual。 将 WindowStartupLocation 属性设置为 Manual 使窗口按......