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

本文探讨了在WPF应用中使用WindowsFormsHost遇到的问题及解决方案。问题包括:1) WPF控件无法显示在WindowsFormsHost上方;2) 如何裁剪WindowsFormsHost的内容;3) WindowsFormHost不支持png透明背景。通过使用Popup包装WPF控件、设置WinForm控件的Region属性限制显示区域以及将多个WinForm控件放在同一Panel下解决这些问题。提供了示例和源代码下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题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}"
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值