簡體   English   中英

如何從最大化的WPF窗口中刪除邊框?

[英]How to remove border from a maximized WPF window?

我想以全屏模式運行WPF應用程序。 為此,我使用了該項目中的以下代碼:

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

問題是,當我切換到全屏模式時,會在窗口周圍得到該邊框,如下面的屏幕截圖所示:

在此處輸入圖片說明

如何刪除邊框? 另一個問題是還原到其原始狀態后,窗口周圍的細虛線邊框。 怎么也刪除呢?

創建一個新的WPF應用程序,並對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;
            }
        }
    }
}

窗口的行為如下:

全屏模式下的窗口

窗口模式下的窗口

兩者都是我整個屏幕的截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM