WPF弹出一个窗口,并自动关闭

最近用WPF做了一个小的demo,由于对于WPF不是很熟悉,在这个过程中遇到不少问题,这篇文章便是针对某个小需求的整理,这个需求便是:在WPF中,如何弹出一个窗口,并让它定时自动关闭

我记得我最早的思路是在主窗口中开启一个线程,这个线程每隔1s休眠一次,用这个线程来控制窗口的显示和隐藏。那个时候,我不知道WPF中,有一个叫做Timer的类。可想而知,这调试过程是多么艰难与纠结……

然而,它用Timer来实现,却是如此简单,用以下的小case来展示它的实现:主窗口上一个按钮,点击按钮,弹出一个窗口,窗口中显示一段文字,窗口3秒后自动关闭。

 

主窗口代码:

<Window x:Class="TestForWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="517" Width="842" Background="White" WindowStartupLocation="CenterScreen">
    <Grid>
        <Button Content="Pop up" Height="23" HorizontalAlignment="Left" Margin="58,38,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

后台代码:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            PopupWindow win = new PopupWindow();
            win.Show();
        }
    }


弹出窗口代码:

<Window x:Class="TestForWpf.PopupWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PopupWindow" Height="211" Width="401" ResizeMode="NoResize" WindowStyle="None" WindowStartupLocation="CenterScreen">
    <Grid Background="#dedede" Height="152" Width="359">
        <Label Margin="46,44,54,54" Height="40" Content="This is a self-closing window!" VerticalContentAlignment="Bottom" FontSize="18" FontWeight="Bold"></Label>
    </Grid>
</Window>


后台代码:

    /// <summary>
    /// Interaction logic for PopupWindow.xaml
    /// </summary>
    public partial class PopupWindow : Window
    {
        private Timer timer = new Timer(3000);
        public PopupWindow()
        {
            InitializeComponent();

            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            timer.Stop();
            Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate()
            {
                this.Hide();
            });
        }
    }


效果图:


 

    

要实现WPF弹出窗口居中,有几种方法可以实现。 一种方法是在XAML中设置WindowStartupLocation属性为CenterScreen,这将使窗口在显示时自动居中于屏幕。这可以通过在XAML文件中添加以下代码来实现: 另一种方法是在代码中使用Window类的Left和Top属性来计算窗口的位置。你可以在xaml.cs文件中实现一个名为CenterWindowOnScreen的方法,该方法通过获取主屏幕的宽度和高度以及窗口的宽度和高度,计算出窗口的左上角位置,从而使窗口居中于屏幕。你可以在构造函数或窗口加载事件中调用这个方法。 另外,如果你想要判断子窗口与父窗口是否在同一屏幕,你可以使用Screen类和Graphics类。通过获取父窗口的句柄,使用Screen.FromHandle方法获取父窗口所在的屏幕,并计算子窗口的左上角位置。如果子窗口的位置在父窗口所在的屏幕范围内,则它们在同一屏幕上。 综上所述,要实现WPF弹出窗口居中,你可以在XAML中设置WindowStartupLocation属性为CenterScreen,或者在代码中计算窗口的位置并使用Left和Top属性进行设置。同时,你可以使用Screen类和Graphics类来判断子窗口与父窗口是否在同一屏幕上。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [WPF 窗体居中](https://blog.csdn.net/MrBaymax/article/details/89810731)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [WPF 窗口居中 & 变更触发机制](https://blog.csdn.net/sD7O95O/article/details/125465679)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值