wpf 右下角弹出窗

自己写的wpf 弹出框,欢迎拍砖,动画都写在了后台代码,前台代码不太重要,用了一下iconfont,具体样式我就不贴出来了,本次主要是后台代码的动画

需要有父级窗口才可以使用。

前台代码:

<Window x:Class="V2VReporter.Views.SystemViews.MsgPopup"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:V2VReporter.Views.SystemViews"
        xmlns:controls="clr-namespace:V2VReporter.Controls"
        mc:Ignorable="d"
        Title="系统消息" Height="230" Width="300" AllowsTransparency="True" WindowStyle="None" ShowInTaskbar="False">
    <Border BorderBrush="{StaticResource SideLevel1}" BorderThickness="1">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Background="{StaticResource MainGreen}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Column="0" Orientation="Horizontal">
                    <TextBlock Text="&#xeb66;" Style="{StaticResource IconFont}" VerticalAlignment="Center" FontSize="18" Foreground="AliceBlue" Margin="5,0,0,0"></TextBlock>
                    <TextBlock Text="系统消息" Foreground="AliceBlue" FontSize="15" VerticalAlignment="Center" Margin="2,0,0,0" ></TextBlock>
                </StackPanel>
                <StackPanel Grid.Column="1" HorizontalAlignment="Right">
                    <controls:IconButton Style="{StaticResource TitleCloseBtn}" Margin="0,5,2,0" Click="ButtonBase_OnClick"></controls:IconButton>
                </StackPanel>
            </Grid>
        </Grid>
    </Border>
</Window>

后台代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using V2VReporter.Resource;

namespace V2VReporter.Views.SystemViews
{
    /// <summary>
    /// MsgPopup.xaml 的交互逻辑
    /// </summary>
    public partial class MsgPopup:Window
    {
        /// <summary>
        /// 关闭动画
        /// </summary>
        private Storyboard sb_close=new Storyboard();
        /// <summary>
        /// 关闭标识
        /// </summary>
        private bool close_flg = false;
        /// <summary>
        /// 关闭动画
        /// </summary>
        private DoubleAnimation close=new DoubleAnimation();
        /// <summary>
        /// 窗口弹出
        /// </summary>
        private Storyboard sb_loaded=new Storyboard();
        /// <summary>
        /// 窗口弹出动画
        /// </summary>
        private DoubleAnimation load=new DoubleAnimation();
        /// <summary>
        /// 窗口加载动画
        /// </summary>
        private DoubleAnimation loadOp=new DoubleAnimation();


        public MsgPopup(Window win)
        {
            InitializeComponent();

            initedCloseStory();
            initedLoadStory(win);
            sb_loaded.Begin();
        }

        private void initedLoadStory(Window win)
        {
            this.Owner = win;
            this.Left = win.Width - this.Width;
            load.From = win.Height;
            load.To = win.Height - this.Height;
            load.Duration = new Duration(TimeSpan.FromSeconds(1.5));
            load.FillBehavior = FillBehavior.HoldEnd;
            load.AutoReverse = false;
            sb_loaded.Children.Add(load);
            Storyboard.SetTarget(load, this);
            Storyboard.SetTargetProperty(load, new PropertyPath("Top"));

            loadOp.From = 0;
            loadOp.To = 1;
            loadOp.Duration=new Duration(TimeSpan.FromSeconds(1.5));
            loadOp.FillBehavior = FillBehavior.HoldEnd;
            loadOp.AutoReverse = false;
            sb_loaded.Children.Add(loadOp);
            Storyboard.SetTarget(loadOp, this);
            Storyboard.SetTargetProperty(loadOp, new PropertyPath("Opacity"));

        }

        private void initedCloseStory()
        {
            close.From = 1;
            close.To = 0;
            close.Duration = new Duration(TimeSpan.FromSeconds(1));
            close.FillBehavior = FillBehavior.HoldEnd;
            close.AutoReverse = false;
            sb_close.Children.Add(close);
            Storyboard.SetTarget(close, this);
            Storyboard.SetTargetProperty(close, new PropertyPath("Opacity"));
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if (!close_flg)
            {
                sb_close.Completed += delegate
                {
                    close_flg = true;
                    this.Close();
                };
                sb_close.Begin();
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
            }
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

直接用的话会运行不起来,主要看后台动画代码和逻辑

本文属原创 转载请写明出处

转载于:https://www.cnblogs.com/wuyaxiansheng/p/10103839.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现这个功能,你需要按照以下步骤进行操作: 1. 创建一个 WPF 应用程序。 2. 在主口中设计一个按钮。 3. 创建一个新的 WPF 口用来显示自定义口内容。 4. 在按钮的点击事件中,实例化自定义口并显示出来。 下面是详细的步骤: 1. 创建一个 WPF 应用程序。 打开 Visual Studio,选择“文件”>“新建”>“项目”,在“新建项目”对话框中选择“WPF 应用程序”。 2. 在主口中设计一个按钮。 打开 MainWindow.xaml 文件,在口上添加一个按钮: ``` <Button Content="打开自定义口" Click="Button_Click"/> ``` 3. 创建一个新的 WPF 口用来显示自定义口内容。 在“解决方案资源管理器”中右键单击项目,选择“添加”>“新建项”,在“添加新项”对话框中选择“WPF 口”,命名为“CustomWindow.xaml”。 在 CustomWindow.xaml 中添加一些内容,如一个文本框: ``` <Grid> <TextBox Text="这是自定义口"/> </Grid> ``` 4. 在按钮的点击事件中,实例化自定义口并显示出来。 在 MainWindow.xaml.cs 文件中,添加以下代码: ``` private void Button_Click(object sender, RoutedEventArgs e) { CustomWindow customWindow = new CustomWindow(); customWindow.ShowDialog(); } ``` 这个代码在按钮的点击事件中实例化 CustomWindow 口,并通过 ShowDialog() 方法显示出来。 现在,你可以运行应用程序并点击按钮,就会弹出自定义口了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值