WPF实现窗口从右下角垂直向上和水平向左淡出

受到腾讯弹出新闻什么的启发,才想着去实现了一下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace TestWPFPopup
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            tbPopup.FontSize = 20;
            tbPopup.Text = "**,你的上网时间是不是有点长了?\n" + "要不要给***打个电话呢?或者骑车出去转转?\n" + "或者背着相机出去拍拍?";

            timer.Interval = TimeSpan.FromMilliseconds(273);//计时器启动273毫秒后调用事件处理程序
            timer.Tick += new EventHandler(dispatcherTimer_Tick);

            /**********右下角垂直向上弹出**********/

            //this.Height是自定义窗口的高度
            //this.Width是自定义窗口的宽度

            //this.Left  获取或设置窗口左边缘相对于桌面的位置。
            //此处左边缘即为显示屏的宽度减去自定义窗口的宽度
            this.Left = SystemParameters.WorkArea.Width - this.Width;

            //this.Top  获取或设置窗口上边缘相对于桌面的位置。
            //此处上边缘即为显示屏的高度,即上边缘位于屏幕底线
            //this.Top=0的时候,自定义窗口位于屏幕顶线
            //this.Top=SystemParameters.WorkArea.Height - this.Height的时候自定义窗口的下边正好与屏幕底线重合
            //this.Top = SystemParameters.WorkArea.Height;//SystemParameters.WorkArea.Height是工作区的高度,不包括任务栏
            this.Top = SystemParameters.PrimaryScreenHeight;//整个屏幕的高度,包括任务栏

            //定义弹出窗口完全弹出时候的上边的位置,若一开始this.Top就等于stopTop,则显示为完全弹出的样子
            //完全弹出的时候下边紧贴这任务栏
            StopTop = SystemParameters.WorkArea.Height - this.Height;
            //StopTop = SystemParameters.PrimaryScreenHeight - this.Height;//完全弹出的时候任务栏会遮挡住一部分

            /**********右下角垂直向上弹出**********/


            /**********右下角水平向左弹出**********/
            //this.Left = SystemParameters.WorkArea.Width;
            //this.Top = SystemParameters.WorkArea.Height - this.Height;
            //StopLeft = SystemParameters.WorkArea.Width - this.Width;
            /**********右下角水平向左弹出**********/


            timer.Start();//启动计时器
        }

        DispatcherTimer timer = new DispatcherTimer();

        /**********右下角垂直向上弹出**********/
        public double StopTop
        {
            get;
            set;
        }
        /**********右下角垂直向上弹出**********/


        /**********右下角水平向左弹出**********/
        public double StopLeft
        {
            get;
            set;
        }
        /**********右下角水平向左弹出**********/

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            /**********右下角垂直向上弹出**********/
            while (true)
            {
                if (this.Top > StopTop)
                {
                    this.Top -= 0.00273;
                }
                else
                {
                    timer.Stop();
                    break;
                }
            }
            /**********右下角垂直向上弹出**********/


            /**********右下角水平向左弹出**********/
            //while (true)
            //{
            //    if (this.Left > StopLeft)
            //    {
            //        this.Left -= 0.00273;
            //    }
            //    else
            //    {
            //        timer.Stop();
            //        break;
            //    }
            //}
            /**********右下角水平向左弹出**********/
        }

    }
}
效果:


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值