WPF应用中对WindowsFormHost内容进行裁剪
发布时间:2021-01-07 01:11 所属栏目:52 来源:网络整理
导读:
导读:问题1:? ? WPF中在使用WindowsFormsHost调用WinFrom控件时,若在WindowsFormsHost上层添加了WPF控件,该控件不会显示出来。 Grid WindowsFormsHost Background ="White" Winfrm:WebBrowser x:Name ="WinFrmWebBrowser" / / WindowsFormsHost !-- 运行时 Ell
问题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,90,90)); path.CloseFigure(); this.WinformRtx.Region = new Region(path); 我的测试效果,WPF中用Image加载了一张星空图,右上角放置了WindowsFormHost内容。我对其进行了显示区域限制。 ?3:问题三 (编辑:ASP站长网) |
相关内容
网友评论
推荐文章
热点阅读