如何从最大化的WPF窗口中删除边框?

时间:2015-06-20 08:06:17

标签: c# .net wpf

我想以全屏模式运行WPF应用程序。为此,我使用了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);
    }
}

在主.cs文件中,我编写了以下代码:

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

问题是,当我切换到全屏时,我会在窗口周围看到这个边框,如下面的屏幕截图所示:

enter image description here

如何删除该边框?另一个问题是在恢复到原始状态后窗口周围有一个细的虚线边框。如何删除它?

1 个答案:

答案 0 :(得分:1)

创建一个新的WPF-Application并对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>

代码背后

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

窗口的行为如下:

Window in Fullscreen-Mode

Window in Windowed-Mode

两者都是我整个屏幕的截图