UI: 窗口全屏, 窗口尺寸

Posted ansen312

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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) - 指定窗口允许的最小尺寸
            //     最小 width 和最小 height 都不能为零,我这里测试的是最小 width 为 192,最小 height 为 48
            applicationView.SetPreferredMinSize(new Size(200, 200));

            // PreferredLaunchViewSize - 窗口启动时的初始尺寸
            // 若要使 PreferredLaunchViewSize 设置有效,需要将 ApplicationView.PreferredLaunchWindowingMode 设置为 ApplicationViewWindowingMode.PreferredLaunchViewSize
            ApplicationView.PreferredLaunchViewSize = new Size(800, 480);
            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(600, 600);

            // TryResizeView(Size value) - 尝试将窗口尺寸设置为指定的大小
            bool success = applicationView.TryResizeView(size);
            if (success)
            {
                lblMsg.Text = "尝试修改窗口尺寸成功";
            }
            else
            {
                lblMsg.Text = "尝试修改窗口尺寸失败";
            }

            // 注:怎么修改窗口的显示位置呢?暂时不知道
        }
    }
}
复制代码

以上是关于UI: 窗口全屏, 窗口尺寸的主要内容,如果未能解决你的问题,请参考以下文章

ue4调整grid缩小

qt中将窗口关闭后为啥还会显示到历史窗口

如何在Java中将窗口调整为全屏?

HTML5轻松实现全屏视频背景

UIPopoverPresentationController 始终显示全屏模式弹出窗口

Java全屏模式无法在Ubuntu上运行