Creating Your Own Custom WPF Media Player - Full Screen Code
Auto Beauty Business Culture Dieting DIY Events Fashion Finance Food Freelancing Gardening Health Hobbies Home Internet Jobs Law Local Media Men's Health Mobile Nutrition Parenting Pets Pregnancy Products Psychology Real Estate Relationships Science Seniors Sports Technology Travel Wellness Women's Health

Creating Your Own Custom WPF Media Player - Full Screen Code

How to create the full screen subroutine/method/function for a WPF media player.

Welcome to the third part of the tutorial. This time we shall dive in and start coding the application.

Event Handlers

One of the things that sets visual programming apart from object oriented programing are the events. An event is defined by a special input action from the user. For instance, the action of clicking a button on a webpage is considered an event. The event handlers are in fact methods belonging to a class that are executed the moment an event occurs.

You can access the event list of each object on our graphical interface from the “properties” window (notice the small lightning icon near the top of the properties window).

The coding behind the MainWindow file will be code extensive and complex. At some point, we will have to switch from the MainWindow to the “Form1” window, the one in which we will browse the hard disk for the file we want to play.

Functions for full screen mode

You might wonder, why do we start with full screen first if we don’t even have the buttons for play/pause/stop done? Well, the full screen will be the most difficult feature of the media player, and it requires some very complicated coding, and this will probably take out most of this part of the tutorial.

The problem at hand…

We want the media player to enter full screen mode when we double click. However, there is a problem: the media element class does not have an event handler for double click…

So we will have to cannibalize one of the other event handlers, more precisely MouseLeftButtonUp.

The double click event does not exist in the media element class, but it does exist in the operating system (you use it to launch things). The event is contained in a dynamic link library (.dll) file called “user32.dll”. The awesome thing about C# is that it allows us to call unmanaged functions, which have already been compiled, and use them in our program. Cool isn’t it? So, we shall call forth the user32.dll file. For this, we will need to add a specific namespace which allows us to import dll files.

At the top of the file, before the namespace containing the MainWindow class, add the line

using System.Runtime.InteropServices;

This will give us access to the DllImport function.

Back in our MainWindow class, add the following lines of code

[DllImport("user32.dll")]

static extern uint GetDoubleClickTime();

System.Timers.Timer timeclick = new System.Timers.Timer((int)GetDoubleClickTime())

{

AutoReset = false

};

Actually, what we are doing here is we are getting a timer which counts have many times an object has been clicked inside the reset timer. The reset timer can be edited from the control panel of the operating system. If we set “Autoreset”=true, this object called “timeclick” will behave exactly like a single click.

Now, the function which enables fullscreen is:

void fullscreenn()

{

if (!timeclick.Enabled)

{

timeclick.Enabled = true;

return;

}

if (timeclick.Enabled)

{

if (fullscreen == false)

{

LayoutRoot.Children.Remove(media_Element);

this.Background = new SolidColorBrush(Colors.Black);

this.Content = media_Element;

this.WindowStyle = WindowStyle.None;

this.WindowState = WindowState.Maximized;

media_Element.Position = TimeSpan.FromSeconds(currentposition);

fullscreen = true;

}

else if (fullscreen == true)

{

this.Content = LayoutRoot;

LayoutRoot.Children.Add(media_Element);

this.Background = new SolidColorBrush(Colors.White);

this.WindowStyle = WindowStyle.SingleBorderWindow;

this.WindowState = WindowState.Normal;

media_Element.Position = TimeSpan.FromSeconds(currentposition);

fullscreen = false;

}

}

}

Basically, we don’t have a full screen mode for the window. So we remove the media element from the grid, and make the MainWindow itself behave like a media element. We change the content value of the MainWindow to the one media element was playing, we remove the borders and we maximize the window. When coming out of fullscreen, we do exactly the opposite.

The click event for the full screen button (optional) will look like this

private void Button_fullscreen_Click(object sender, RoutedEventArgs e)

{

if (fullscreen == false)

{

LayoutRoot.Children.Remove(media_Element);

this.Background = new SolidColorBrush(Colors.Black);

this.Content = media_Element;

this.WindowStyle = WindowStyle.None;

this.WindowState = WindowState.Maximized;

media_Element.Position = TimeSpan.FromSeconds(currentposition);

fullscreen = true;

}

else if (fullscreen == true)

{

this.Content = LayoutRoot;

LayoutRoot.Children.Add(media_Element);

this.Background = new SolidColorBrush(Colors.White);

this.WindowStyle = WindowStyle.SingleBorderWindow;

this.WindowState = WindowState.Normal;

media_Element.Position = TimeSpan.FromSeconds(currentposition);

fullscreen = false;

}

}

The event handler for double click into fullscreen mode will look like this

private void media_Element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

{

if (!timeclick.Enabled)

{

timeclick.Enabled = true;

return;

}

if (timeclick.Enabled)

{

fullscreenn();

}

}

If we hadn't set the media element stretch property to "Uniform to Fill", when we entered full screen mode, you would notice that the window does span over the entire screen, but the actual size of the media being played stays the same.

Shopping tip: See today's most popular programming tools promo codes and save money at popular retailers and brands. Score discounts at your favorite programming tools stores with exclusive offers, site-wide discount codes, and single-use codes.
Need an answer?
Get insightful answers from community-recommended
experts
in Computer Programming & Languages on Knoji.
Would you recommend this author as an expert in Computer Programming & Languages?
You have 0 recommendations remaining to grant today.
Comments (0)
ARTICLE DETAILS
RELATED ARTICLES
RELATED CATEGORIES
ARTICLE KEYWORDS