wpf实现仿qq消息提示框

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangli321456/article/details/50523144

1、实现步骤

1.1 另起一个窗口作为消息提示的窗口,在主窗体中调用,先处理一下消息框的展示问题, AllowsTransparency="True"  WindowStyle="None" WindowStartupLocation="Manual",实现允许透明,隐藏自带的工具条(关闭、最大/最小化),自定义窗口位置。

1.2 修改窗体的展示位置为右下角

1.3 通过渐入动画展示窗体,通过定时器自动关闭消息框,关闭时通过渐出动画关闭窗体。

2、关键代码:

渐出动画:

<Window.Resources>
        <Storyboard x:Key="ClosedStoryboard" Storyboard.TargetName="mGrid">
            <DoubleAnimation From="0" To="1" Duration="0:0:0.2" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[1].Offset"/>
            <DoubleAnimation From="0" To="1" Duration="0:0:0" BeginTime="0:0:0.2" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Offset"/>
            <ColorAnimation To="#00000000" Duration="0" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Color"/>
        </Storyboard>
        <LinearGradientBrush x:Key="ClosedBrush" StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientStop Color="#FF000000" Offset="1"/>
            <GradientStop Color="#FF000000" Offset="1"/>
            <GradientStop Color="#FF000000" Offset="0"/>
        </LinearGradientBrush>
    </Window.Resources>


添加遮罩层:

<Grid.OpacityMask>
            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Color="#00000000" Offset="0"/>
                <GradientStop Color="#00000000" Offset="1"/>
                <GradientStop Color="#00000000" Offset="1"/>
            </LinearGradientBrush>
        </Grid.OpacityMask>


添加触发器,窗体加载完后启动渐入动画:

<Grid.Triggers>
            <EventTrigger RoutedEvent="Window.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation From="1" To="0" Duration="0:0:0.2" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[1].Offset"/>
                            <DoubleAnimation From="1" To="0" Duration="0:0:0" BeginTime="0:0:0.2" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Offset"/>
                            <ColorAnimation To="#FF000000" Duration="0" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Color"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Grid.Triggers>


关闭提示框是先启动渐出动画,再关闭窗体:

this.IsEnabled = false;

            mGrid.OpacityMask = this.Resources["ClosedBrush"] as LinearGradientBrush;
            Storyboard std = this.Resources["ClosedStoryboard"] as Storyboard;
            std.Completed += delegate { this.Close(); };

            std.Begin();


源码下载

效果:


展开阅读全文

没有更多推荐了,返回首页