2015-06-20 127 views
1

我想在全屏模式下运行WPF应用程序。对于我用下面的代码this project如何从最大化的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; 
      } 
     } 
    } 

的问题是,当我切换到全屏,我避开这个边界如下面的截图所示的窗口:

enter image description here

如何删除边界?另一个问题是在恢复到原始状态后,窗口周围出现一个细小的虚线边框。如何删除呢?

+0

你试过'拉伸= “UniformToFill”'在'MediaElement'? –

+1

为什么不只是创建一个窗口'AllowsTransparency =“True”WindowStyle =“None”'调整到屏幕坐标并将其放在屏幕的左上角? – Benj

+0

@Bahman_Aries我试着说你的话。我得到了和以前一样的结果。 –

回答

1

创建一个新的WPF的应用程序和使用下面的代码的主窗口

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

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

+0

谢谢你的答案Benj。我试过你的代码。全屏模式下没有边框。但我无法在正常状态下移动窗口。我想如何关闭窗口或调整大小?谢谢。 –

+0

要回到正常状态,您必须使用您在解决方案中引入的关键字F的关键事件处理程序。要关闭它,您可以在最小化时添加边框。等待,我将更新 – Benj

+0

窗口上没有最大化,最小化或关闭按钮。那么,如果我想最小化窗口或最大化没有全屏的窗口呢? –