How to remove border from a maximized WPF window?

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?


Source: .net

Leave a Reply