忽略“最上方”窗口的鼠标和键盘事件

时间:2018-07-26 17:08:01

标签: c# .net windows

我正在使用C#构建一个小型.NET桌面应用程序。我在最上方的窗口(System.Windows)上播放了一个视频,在这里我们可以控制媒体元素和窗口的透明度,因此我们可以看到该窗口下面的所有窗口。

我的目标是通过此顶部所有下面的窗口中的鼠标和键盘事件。用户喜欢同时播放视频并与其他窗口一起使用。

我们正在为Windows用户制作Mac OS版本。

我可以通过此属性在Mac OS上做到这一点。

self.ignoresMouseEvents = true;

这是我的视频播放器窗口。

<Window x:Class="VideoPlayerWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SSAPlayerApp"
    mc:Ignorable="d" 
    Title="VideoPlayerWindow" Height="450" Width="800"
    WindowStartupLocation="CenterScreen" AllowsTransparency="True" 
    WindowStyle="None" Topmost="True" ShowActivated="False" MouseDown="Window_MouseDown" KeyDown="Window_KeyDown">

<MediaElement x:Name="player" Width="450" Height="250" MouseDown="Window_MouseDown" KeyDown="Window_KeyDown" LoadedBehavior="Manual" MediaOpened="player_MediaOpened" MediaEnded="player_MediaEnded" MediaFailed="player_MediaFailed"/>

有人可以帮忙吗?

这是Mac OS的答案App window on top of all windows including others app windows

1 个答案:

答案 0 :(得分:-1)

我找到了所需的代码

   [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
    private static extern int GetWindowLongPtr(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
    private static extern int SetWindowLongPtr(IntPtr hWnd, int nIndex, int dwNewLong);

    const int GWL_EXSTYLE = -20;
    const int WS_EX_Transparent = (int) 0x00000020L;

    private void Window_SourceInitialized(object sender, EventArgs e)
    {
        IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
        int extendedStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);

        SetWindowLongPtr(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_Transparent);
    }

并且Window必须具有SourceInitialized事件。 DLL导入和入口点将同时适用于32位和64位。

现在,我的MediaElement是Window的子级,将所有鼠标和键盘事件(未测试触摸事件)都传递给所有窗口,并将下面的应用程序传递给我的Window。