背水一战 Windows 10 (3) - UI: 窗口全屏, 窗口尺寸

[源码下载]

背水一战 Windows 10 (3) - UI: 窗口全屏, 窗口尺寸

作者:webabcd

介绍
背水一战 Windows 10 之 UI

  • 窗口全屏
  • 窗口尺寸

示例
1、窗口全屏
UI/FullScreen.xaml

<Page
x:Class="Windows10.UI.FullScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="0 10 0 0" /> <Button Name="btnFullScreen" Content="全屏/取消全屏" Click="btnFullScreen_Click" Margin="0 10 0 0" /> <Button Name="btnShowStandardSystemOverlays" Content="在全屏状态下,显示系统 UI,比如标题栏和任务栏" Click="btnShowStandardSystemOverlays_Click" Margin="0 10 0 0" /> <CheckBox Name="chkFullScreenSystemOverlayMode" Content="全屏状态下的,系统边缘手势的响应模式 unchecked:FullScreenSystemOverlayMode.Standard, checked:FullScreenSystemOverlayMode.Minimal" Click="chkFullScreenSystemOverlayMode_Click" Margin="0,10,0,0" /> <CheckBox Name="chkPreferredLaunchWindowingMode" Content="窗口的启动模式 unchecked:ApplicationViewWindowingMode.Auto, unchecked:ApplicationViewWindowingMode.FullScreen" Click="chkPreferredLaunchWindowingMode_Click" Margin="0,10,0,0" /> </StackPanel>
</Grid>
</Page>

UI/FullScreen.xaml.cs

/*
* 演示“窗口全屏”相关知识点
*
* ApplicationView - 用于操作窗口以及获取窗口信息
* GetForCurrentView() - 返回 ApplicationView 实例
*/ using Windows.System;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation; namespace Windows10.UI
{
public sealed partial class FullScreen : Page
{
private MainPage _rootPage; public FullScreen()
{
this.InitializeComponent(); this.Loaded += FullScreen_Loaded;
} private void FullScreen_Loaded(object sender, RoutedEventArgs e)
{
/*
* ApplicationView.PreferredLaunchWindowingMode - 窗口的启动模式
* Auto - 系统自动决定
* PreferredLaunchViewSize - 由 ApplicationView.PreferredLaunchViewSize 决定(参见:UI/ScreenSize.xaml)
* FullScreen - 全屏启动
*
* ApplicationView.GetForCurrentView().FullScreenSystemOverlayMode - 全屏状态下的,系统边缘手势的响应模式
* Standard - 标准方式。比如鼠标移动到顶端显示标题栏,移动到底端显示任务栏
* Minimal - 最小方式。比如鼠标移动到顶端显示一个小的临时 UI,移动到底端显示一个小的临时 UI,点击这个临时 UI 时再显示标题栏或任务栏
*/ // unchecked:FullScreenSystemOverlayMode.Standard, checked:FullScreenSystemOverlayMode.Minimal
chkFullScreenSystemOverlayMode.IsChecked = ApplicationView.GetForCurrentView().FullScreenSystemOverlayMode == FullScreenSystemOverlayMode.Minimal; // unchecked:ApplicationViewWindowingMode.Auto, unchecked:ApplicationViewWindowingMode.FullScreen
chkPreferredLaunchWindowingMode.IsChecked = ApplicationView.PreferredLaunchWindowingMode == ApplicationViewWindowingMode.FullScreen;
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
_rootPage = MainPage.Current;
_rootPage.KeyDown += _rootPage_KeyDown;
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
_rootPage.KeyDown -= _rootPage_KeyDown;
} private void _rootPage_KeyDown(object sender, KeyRoutedEventArgs e)
{
// 判断是否按下了 escape 键
if (e.Key == VirtualKey.Escape)
{
var view = ApplicationView.GetForCurrentView();
if (view.IsFullScreenMode)
{
// 退出全屏状态
view.ExitFullScreenMode();
}
}
} private void btnFullScreen_Click(object sender, RoutedEventArgs e)
{
ApplicationView view = ApplicationView.GetForCurrentView();
// 判断当前是否是全屏模式
if (view.IsFullScreenMode)
{
// 退出全屏模式
view.ExitFullScreenMode();
lblMsg.Text = "退出全屏模式";
}
else
{
// 尝试进入全屏模式
bool isSuccess = view.TryEnterFullScreenMode();
if (isSuccess)
{
lblMsg.Text = "进入全屏模式";
}
else
{
lblMsg.Text = "尝试进入全屏模式失败";
}
}
} private void btnShowStandardSystemOverlays_Click(object sender, RoutedEventArgs e)
{
ApplicationView view = ApplicationView.GetForCurrentView();
// 在全屏状态下,是否显示系统 UI,比如标题栏和任务栏
view.ShowStandardSystemOverlays();
} private void chkFullScreenSystemOverlayMode_Click(object sender, RoutedEventArgs e)
{
ApplicationView view = ApplicationView.GetForCurrentView();
view.FullScreenSystemOverlayMode = chkFullScreenSystemOverlayMode.IsChecked.Value ? FullScreenSystemOverlayMode.Minimal : FullScreenSystemOverlayMode.Standard;
} private void chkPreferredLaunchWindowingMode_Click(object sender, RoutedEventArgs e)
{
ApplicationView.PreferredLaunchWindowingMode = chkPreferredLaunchWindowingMode.IsChecked.Value ? ApplicationViewWindowingMode.FullScreen : ApplicationViewWindowingMode.Auto;
}
}
}

2、窗口尺寸
UI/ScreenSize.xaml

<Page
x:Class="Windows10.UI.ScreenSize"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="0 10 0 0" /> <Button Name="btnChangeSize" Content="尝试改变窗口大小" Click="btnChangeSize_Click" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>

UI/ScreenSize.xaml.cs

/*
* 演示“窗口尺寸”相关知识点
*
* ApplicationView - 用于操作窗口以及获取窗口信息
* GetForCurrentView() - 返回 ApplicationView 实例
*/ using Windows.Foundation;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows10.UI
{
public sealed partial class ScreenSize : Page
{
public ScreenSize()
{
this.InitializeComponent(); this.Loaded += ScreenSize_Loaded;
} private void ScreenSize_Loaded(object sender, RoutedEventArgs e)
{
// Window.Current.Bounds - 当前窗口的大小(单位是有效像素,没有特别说明就都是有效像素)
// 注:窗口大小不包括标题栏,标题栏属于系统级 UI
lblMsg.Text = string.Format("window size: {0} * {1}", Window.Current.Bounds.Width, Window.Current.Bounds.Height); ApplicationView applicationView = ApplicationView.GetForCurrentView(); // SetPreferredMinSize(Size minSize) - 指定窗口允许的最小尺寸(最小:192×48,最大:500×500)
applicationView.SetPreferredMinSize(new Size(, )); // PreferredLaunchViewSize - 窗口启动时的初始尺寸
// 若要使 PreferredLaunchViewSize 设置有效,需要将 ApplicationView.PreferredLaunchWindowingMode 设置为 ApplicationViewWindowingMode.PreferredLaunchViewSize
ApplicationView.PreferredLaunchViewSize = new Size(, );
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; /*
* ApplicationView.PreferredLaunchWindowingMode - 窗口的启动模式
* Auto - 系统自动调整
* PreferredLaunchViewSize - 由 ApplicationView.PreferredLaunchViewSize 决定
* FullScreen - 全屏启动
*/
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 窗口尺寸发生变化时触发的事件
Window.Current.SizeChanged += Current_SizeChanged;
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Window.Current.SizeChanged -= Current_SizeChanged;
} private void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
lblMsg.Text = string.Format("window size: {0} * {1}", e.Size.Width, e.Size.Height);
} private void btnChangeSize_Click(object sender, RoutedEventArgs e)
{
ApplicationView applicationView = ApplicationView.GetForCurrentView(); Size size = new Size(, ); // TryResizeView(Size value) - 尝试将窗口尺寸设置为指定的大小
bool success = applicationView.TryResizeView(size);
if (success)
{
lblMsg.Text = "尝试修改窗口尺寸成功";
}
else
{
lblMsg.Text = "尝试修改窗口尺寸失败";
} // 注:怎么修改窗口的显示位置呢?暂时不知道
}
}
}

OK
[源码下载]

上一篇:讲讲亿级PV的负载均衡架构


下一篇:QT中关于窗口全屏显示与退出全屏的实现