WPF Enter full screen on mediaElement double clicked does not react always

11,428

The following code demonstrate how to make the window fullscreen by double clicking in the media element. Just change "path_to_file" for the appropriated path the file in the DemoWindow.xaml.cs

Importart: The Source property from MediaElement must be set to enable the MouseLeftButtonUp event. Otherwise the event handler is not called.


DemoWindow.xaml

<Window x:Class="FullscreenDemo.DemoWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DemoWindow" Height="300" Width="300">
    <Grid>
        <MediaElement x:Name="MediaPlayer"
                      MouseLeftButtonUp="MediaPlayer_MouseLeftButtonUp" />
    </Grid>
</Window>

DemoWindow.xaml.cs

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace FullscreenDemo
{
    public partial class DemoWindow : Window
    {
        private bool fullscreen = false;
        private DispatcherTimer DoubleClickTimer = new DispatcherTimer();

        public DemoWindow()
        {
            InitializeComponent();
            DoubleClickTimer.Interval = TimeSpan.FromMilliseconds(GetDoubleClickTime());
            DoubleClickTimer.Tick += (s, e) => DoubleClickTimer.Stop();

            var path = @"path_to_file";
            MediaPlayer.Source = new Uri(path);
        }

        private void MediaPlayer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!DoubleClickTimer.IsEnabled)
            {
                DoubleClickTimer.Start();
            }
            else
            {
                if (!fullscreen)
                {
                    this.WindowStyle = WindowStyle.None;
                    this.WindowState = WindowState.Maximized;
                }
                else
                {
                    this.WindowStyle = WindowStyle.SingleBorderWindow;
                    this.WindowState = WindowState.Normal;
                }

                fullscreen = !fullscreen;
            }
        }

        [DllImport("user32.dll")]
        private static extern uint GetDoubleClickTime();
    }
}

Reference

https://diptimayapatra.wordpress.com/2010/03/04/full-screen-view-for-media-element-in-wpf/

Share:
11,428
HomeMade
Author by

HomeMade

Passionate about what I do, always willing to jump into team projects.

Updated on June 05, 2022

Comments

  • HomeMade
    HomeMade almost 2 years

    I've been searching the easy way to make my window (which contains only a mediaElement) go full screen when it's double clicked. Since I'm new to WPF/C# I did it the way it was suggested here. It works, but it doesn't react always and sometimes I even have to click more than 3 times in row to get it into full screen or restored.

    Here is the event handler:

     private void mediaElement1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2 && fullscreen==false)
            {
                this.WindowStyle = WindowStyle.None;
                this.WindowState = WindowState.Maximized;
            }
            else if (e.ClickCount == 2 && fullscreen == true)
            {
    
                this.WindowStyle = WindowStyle.SingleBorderWindow;
                this.WindowState = WindowState.Normal;
            }
            fullscreen = !fullscreen;
    
        }