WPF 在WindowsFormsHost上添加透明蒙层

参考文章:https://www.cnblogs.com/sntetwt/p/11345029.html

难点问题:
1.WindowsFormsHost不支持背景透明
2.WPF Panel.ZIndex无效,WindowsFormsHost更优先
解决方案:
1.使用Popup,解决最顶层、解决背景透明
2.使用Canvas的MouseLeftButtonDown事件模拟鼠标双击事件

Popup控件实现参考:https://www.cnblogs.com/sntetwt/p/11345072.html

1.添加用户控件命名空间引用

<UserControl 
……

             xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  

             xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

             xmlns:ctrl="clr-namespace:WStream.WPF.UC.VideoPlayer.Controls"

……

</UserControl>
 

2.XAML代码

<Grid>

    <wfi:WindowsFormsHost>

        <wf:Panel x:Name="plMain" Margin="0,0,0,0" AutoSize="True" Height="100"/>

    </wfi:WindowsFormsHost>

    <ctrl:PopupCtrl x:Name="popCtrl" VerticalOffset="-240" IsOpen="True" AllowsTransparency="True">

        <Canvas Height="240" Width="380" Background="#01000000" MouseLeftButtonDown="PopCtrl_MouseLeftButtonUp"/>

    </ctrl:PopupCtrl>

</Grid>
重点解析:
1.Grid中的两个控件 plMain 和 popCtrl 会重叠布局,并且 popCtrl 会在 plMain 之后绘制,自动置顶,解决最顶层的问题
2.关键的关键在于对Canvas的Background的值设定“#01000000”,而不能用Transparent。设置为Transparent时,Canvas完全透明,无法响应鼠标事件。而设置为#01000000表示alpha值为1,但背景完全透明,可以响应鼠标事件,也可实现在视频上显示弹幕,图片等功能。


3.PopCtrl_MouseLeftButtonUp事件模拟鼠标双击

        int clkcount = 0;

        private void PopCtrl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

        {

            clkcount++;

            //模拟鼠标双击事件

            if (clkcount % 2 == 0)

            {

                //处理双击事件

            }

        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值