WPF 全屏显示实现(无标题栏按钮 + 自定义退出按钮)

WPF 全屏显示实现(无标题栏按钮 + 自定义退出按钮)

完整实现代码

MainWindow.xaml

xml 复制代码
<Window x:Class="FullScreenApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="全屏应用" WindowState="Maximized" WindowStyle="None"
        ResizeMode="NoResize" Background="Black">
    
    <Grid>
        <!-- 主内容区域 -->
        <TextBlock Text="全屏应用演示" 
                  HorizontalAlignment="Center" 
                  VerticalAlignment="Center"
                  Foreground="White" FontSize="36"/>
        
        <!-- 自定义退出按钮 -->
        <Button x:Name="ExitButton" 
                Content="退出程序" 
                Width="100" Height="40"
                HorizontalAlignment="Right" 
                VerticalAlignment="Bottom"
                Margin="20"
                Click="ExitButton_Click"/>
    </Grid>
</Window>

MainWindow.xaml.cs

csharp 复制代码
using System.Windows;

namespace FullScreenApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
            // 确保窗口全屏
            this.WindowState = WindowState.Maximized;
            this.WindowStyle = WindowStyle.None;
            
            // 可选:防止其他窗口覆盖
            this.Topmost = true;
        }
        
        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            // 退出应用程序
            Application.Current.Shutdown();
        }
        
        // 可选:响应ESC键退出
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                Application.Current.Shutdown();
            }
            base.OnKeyDown(e);
        }
    }
}

进阶功能

1. 添加淡入淡出动画效果

xml 复制代码
<Window.Resources>
    <Storyboard x:Key="FadeOut">
        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                         From="1" To="0" Duration="0:0:0.3"/>
    </Storyboard>
</Window.Resources>

然后在退出按钮点击事件中:

csharp 复制代码
private async void ExitButton_Click(object sender, RoutedEventArgs e)
{
    var storyboard = (Storyboard)FindResource("FadeOut");
    storyboard.Begin(this);
    
    await Task.Delay(300); // 等待动画完成
    Application.Current.Shutdown();
}

2. 防止误操作退出(添加确认对话框)

csharp 复制代码
private void ExitButton_Click(object sender, RoutedEventArgs e)
{
    var result = MessageBox.Show("确定要退出程序吗?", "确认退出", 
                               MessageBoxButton.YesNo, MessageBoxImage.Question);
    
    if (result == MessageBoxResult.Yes)
    {
        Application.Current.Shutdown();
    }
}

3. 多显示器支持(在主显示器全屏)

csharp 复制代码
public MainWindow()
{
    InitializeComponent();
    
    // 获取主显示器信息
    var screen = System.Windows.Forms.Screen.PrimaryScreen;
    
    // 设置窗口位置和大小
    this.Left = screen.Bounds.Left;
    this.Top = screen.Bounds.Top;
    this.Width = screen.Bounds.Width;
    this.Height = screen.Bounds.Height;
    this.WindowStyle = WindowStyle.None;
    this.WindowState = WindowState.Normal; // 必须设置为Normal才能自定义大小
}

注意事项

  1. 窗口样式WindowStyle="None" 会移除所有窗口装饰,包括标题栏和边框
  2. 调整大小ResizeMode="NoResize" 防止用户调整窗口大小
  3. 任务栏:全屏窗口默认会覆盖任务栏,如需显示任务栏,请调整窗口大小
  4. 快捷键:添加ESC键退出功能可以提升用户体验
  5. 性能:全屏应用通常需要优化渲染性能,特别是包含动画或视频时
相关推荐
我是苏苏18 小时前
WPF开发01:基础控件及常用模板篇
开发语言·c#·html·wpf
dalong101 天前
WPF:3D顶点共享与法线设置对光照效果的影响
3d·wpf
柔蘭2 天前
WPF中的CommunityToolkit.Mvvm框架
wpf
aGdF8E3gQ3 天前
【Application Insights】采样率对Function App日志收集的影响和解决方法
python·flask·wpf
程序员-Benothing3 天前
如何实现高并发系统订单30分钟自动关闭?
开发语言·c#·wpf
一水4 天前
Redis实战:一个AI工作流系统里的五个应用场景,从传参到限流的完整链路
数据库·redis·wpf
dalong104 天前
WPF:3D甜甜圈
3d·c#·wpf·甜甜圈
别催小唐敲代码4 天前
ROS2从入门到实战:去中心化机器人操作系统详解
wpf·ros2
脚踏实地皮皮晨5 天前
003006001_WrapPanel控件
开发语言·c#·.net·wpf·visual studio
脚踏实地皮皮晨5 天前
003005002_WPF StackPanel 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio