-2

I'm building a small .NET desktop app with C#. I have a video playing on topmost window (System.Windows) where we can control transparency of the Media Element and Window so we can see all windows below of this window.

My goal is to get mouse and keyboard events through this topmost window for all windows below. User like to play the video and work with other window same time.

We are making our Mac OS version for Windows users.

I can do that on Mac OS by this property.

self.ignoresMouseEvents = true;

This is my video player window.

<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"/>

Can anybody help?

Here is the Mac OS answer App window on top of all windows including others app windows

Nafiz
  • 462
  • 4
  • 17
  • 1
    The critical pieces are `WM_NCHITTEST` and `HTTRANSPARENT`. Using them from WPF is not the easiest. – Ben Voigt Jul 26 '18 at 17:12
  • at least with windows forms...youd put a transparent panel on top so the clicking wont hit the video player – Ctznkane525 Jul 26 '18 at 17:14
  • I'm not using WPF anyway. Also I'm not experienced on .NET framework. – Nafiz Jul 26 '18 at 17:16
  • It is automagic on Windows, a window that is transparent to the user's eyes is transparent to the mouse as well. But it is quite hard to imagine how a transparent video could be useful, do explain that detail. – Hans Passant Jul 26 '18 at 17:17
  • The goal is to watch the video with transparency and work on other app same time. So on this window I need to pass all the events to other windows (including other app windows). – Nafiz Jul 26 '18 at 17:22
  • 1
    What are you using, if not WPF? Because *that looks like WPF XAML to me*. – Ben Voigt Jul 26 '18 at 17:24
  • You are right. I did not get you there. – Nafiz Jul 26 '18 at 17:26

1 Answers1

0

I found the piece of code I needed

   [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);
    }

And Window must have SourceInitialized event. The DLL import and entry point will work for both 32-bit and 64-bit.

Now my MediaElement which is a child of the Window, both passing all the mouse and keyboard events (did not test touch event) to all windows and app below to my Window.

Nafiz
  • 462
  • 4
  • 17