zoukankan      html  css  js  c++  java
  • 【C#/WPF】窗体定时自动关闭

    需求:打开WPF项目后,展示3秒钟产品Logo后,进入主界面MainWindow。(类似于安卓应用打开时的闪屏页SplashPage)

    思路:在进入MainWindow后新建一个Window窗体,窗体的背景设置为Logo图片,窗体设置为最大化、Z轴置顶、不可调整宽高、不显示状态栏。设置一个DispatcherTimer定时器,3秒后关闭窗体。

    MainWindow.xaml.cs 后台代码的主要逻辑:

    private Window window;
    
    public MainWindow()
    {
        InitializeComponent();
        // 界面加载完成后触发的事件
        this.Loaded += new RoutedEventHandler(ShowLoginWindow);
    }
    
    private void ShowLoginWindow(object sender, RoutedEventArgs e)
    {
        // 弹窗显示Logo 3秒后关闭该弹窗自动关闭
        window = new Window();
    
        // 经测试,以下设置无法做到窗体跟屏幕一样大,窗体四周会留下几个像素的边距
        //double screenHeight = SystemParameters.FullPrimaryScreenHeight;
        //double screenWidth = SystemParameters.FullPrimaryScreenWidth;
        //window.Width = screenWidth;
        //window.Height = screenHeight;
        //window.Top = (screenHeight - window.Height) / 2;
        //window.Left = (screenWidth - window.Width) / 2;
    
        window.Topmost = true; // 置顶
        window.WindowStartupLocation = WindowStartupLocation.CenterScreen; // 屏幕中心
        window.WindowState = WindowState.Maximized; // 最大化
        window.ResizeMode = ResizeMode.NoResize; // 不能调宽高
        window.WindowStyle = WindowStyle.None;   // 无窗体样式,即可不显示状态栏
    
        // 背景是Logo图片
        ImageBrush brush = new ImageBrush();
        brush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/HomeDecorationPSD;component/Presentation/Resources/Images/app_logo.jpg")); // 这是绝对路径
        window.Background = brush;
    
        // 设置窗体3秒后自动关闭
        StartCloseTimer();
    
        window.ShowDialog();
    }
    
    private void StartCloseTimer()
    {
        DispatcherTimer timer = new DispatcherTimer();
        //timer.Interval = TimeSpan.FromSeconds(3); // 3秒
        // 为了方便测试,可以把这个秒数写到App.config配置文件中
        double t = double.Parse(ConfigurationManager.AppSettings["LOGO_WINDOW_AUTO_CLOSE_TIMER"]);
        timer.Tick += TimerTick; // 注册计时器到点后触发的回调
        timer.Start();
    }
    
    private void TimerTick(object sender, EventArgs e)
    {
        DispatcherTimer timer = (DispatcherTimer)sender;
        timer.Stop();
        timer.Tick -= TimerTick; // 取消注册
        window.Close();
    }
    
    private void CloseLogoWindow(object state)
    {
        // 关闭Logo窗体
        window.Close();
    }

    配置文件App.config 添加内容

    <appSettings>
        ...
        <!-- 打开软件时Logo弹窗自动关闭倒计时秒数 类似闪屏页SplashPage -->
        <add key="LOGO_WINDOW_AUTO_CLOSE_TIMER" value="3" />
    </appSettings>

    重要参考:

    http://stackoverflow.com/questions/11719283/how-to-close-auto-hide-wpf-window-after-10-sec-using-timer

  • 相关阅读:
    设计模式:解释器模式???
    设计模式:访问者模式(Visitor)
    设计模式:享元模式(Flyweight)
    NHibernate
    设计模式:中介者模式(Mediator)
    设计模式:职责链模式(Chain Of Responsibility)
    反射
    设计模式:命令模式(Command)
    设计模式:桥连模式(Bridge)
    设计模式:组合模式(Composite)
  • 原文地址:https://www.cnblogs.com/guxin/p/csharp-wpf-window-how-to-auto-close.html
Copyright © 2011-2022 走看看