1

I want to run WPF application in fullscreen mode. For that I used the following code from this project:

class WinApi
{
    [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
    public static extern int GetSystemMetrics(int which);

    [DllImport("user32.dll")]
    public static extern void
        SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                     int X, int Y, int width, int height, uint flags);        

    private const int SM_CXSCREEN = 0;
    private const int SM_CYSCREEN = 1;
    private static IntPtr HWND_TOP = IntPtr.Zero;
    private const int SWP_SHOWWINDOW = 64; // 0×0040

    public static int ScreenX
    {
        get { return GetSystemMetrics(SM_CXSCREEN);}
    }

    public static int ScreenY
    {
        get { return GetSystemMetrics(SM_CYSCREEN);}
    }

    public static void SetWinFullScreen(IntPtr hwnd)
    {
        SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
    }
}

In main .cs file I wrote the following code:

private void window1_KeyUp(object sender, KeyEventArgs e)
    {

        if(e.Key == Key.F)
        {
            if(!isFullScreen)
            {
                height = mePlayer.Height;
                width = mePlayer.Width;
                this.BorderThickness = new Thickness(0.0);
                this.Background = new SolidColorBrush(Colors.Black);
                this.WindowStyle = WindowStyle.None;
                this.WindowState = WindowState.Maximized;
                this.Topmost = true;
                WinApi.SetWinFullScreen(new WindowInteropHelper(this).Handle);
                isFullScreen = !isFullScreen;
            }
            else
            {
                this.Topmost = false;
                this.Background = new SolidColorBrush(Colors.White);
                this.WindowStyle = WindowStyle.SingleBorderWindow;
                isFullScreen = !isFullScreen;
            }
        }
    }

The problem is that when I switch to fullscreen, I get this border around the window as shown in the screenshot below:

enter image description here

How to remove that border? Another issue is a thin dotted border around the window after restore to its original state. How to remove that also?

4
  • 1
    Have you tried Stretch="UniformToFill" in your MediaElement? Jun 20, 2015 at 8:42
  • 1
    why not just create a Window AllowsTransparency="True" WindowStyle="None" resize to to screen-coordinates and place it top left of the screen?
    – Benj
    Jun 20, 2015 at 8:42
  • @Bahman_Aries I tried what you said. I am getting the same results as before. Jun 20, 2015 at 8:52
  • @Benj When i set AllowsTransparency="True" WindowStyle="None" Application starts in fullscreen mode and the border is no longer is there. But i don't want application to run in fullscreen at startup. Jun 20, 2015 at 8:57

1 Answer 1

2

Create a new WPF-Application and use the following code for MainWindow

XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"

        KeyDown="KeyHandler_F"
        AllowsTransparency="True" WindowStyle="None"
        >
    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="200" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <TextBlock
            Grid.Row="0"
            VerticalAlignment="Center"
            HorizontalAlignment="Center">

            Press <Run FontSize="60" Foreground="Red">F</Run> to toggle between Fullscreen and Window-Screen

        </TextBlock>

        <TextBlock Grid.Row="1" Margin="10" TextWrapping="Wrap">You will need to create your own Title-Bar and Minimize/ Maximize/ Close Buttons on the Window as seen in Microsoft Visual Studio 2013. Alternatively you can create a new Window when switching to FullScreen</TextBlock>

    </Grid>
</Window>

Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.ToggleWindow();
        }

        private void KeyHandler_F(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.F)
            {
                this.ToggleWindow();
            }
        }

        private void ToggleWindow()
        {
            switch (this.WindowState)
            {
                case (WindowState.Maximized):
                    {
                        this.WindowState = WindowState.Normal;
                    }
                    break;

                default:
                    {
                        this.WindowState = WindowState.Maximized;
                    }
                    break;
            }
        }
    }
}

The Window will behave like this:

Window in Fullscreen-Mode

Window in Windowed-Mode

Both are screenshots of my entire screen

4
  • Thanks for the answer Benj. I tried your code. There is no border in fullscreen mode. But i can not able to move window in normal state. How am i suppose to close that window or resize? Thank you. Jun 20, 2015 at 9:20
  • to move back to normal state you would have to use the key-event-handler for key F that you introduced in your solution. to close it, you can add the border when minimizing. wait, I will update
    – Benj
    Jun 20, 2015 at 9:22
  • There is no maximize, minimize or close button on window. So what if i want to minimize the window or maximize the window without fullscreen? Jun 20, 2015 at 9:31
  • you would have to add this feature by yourself (similar to visual studio 2013). (create buttons, add event-handler and maximize the window)
    – Benj
    Jun 20, 2015 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.