参考文章: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)
{
//处理双击事件
}
}