[前言]
项目中往往需要很多展示的提示信息,通用的最简单的方法就是使用MessageBox弹窗提示,弹窗不处理的话需要点击确认操作,非必要警告信息老是弹窗体验又不太好,特参考web的信息展示方法,在窗体内展示提示信息,再自动关闭提示页面.本文动画部分参考:https://www.cnblogs.com/xvqm00/archive/2009/02/16/1391313.html
[样式展示]

[功能原理]
1.窗体的自动定时关闭使用Timer控制;
2.动画部分引入user32.dll,user32.dll是Windows用户界面相关应用程序接口,用于包括Windows处理,基本用户界面等特性,如创建窗口和发送消息。
[提示框Form设置]
1.关闭Form的Border–> FormBorderStyle=None
2.设置Form显示位置–>StartPosition=Manual
3.设置Form显示最上层–>TopMost=True
3.透明度可自由设置–>Opacity=60%
4.任务栏和图标显示关闭
设置如图


[代码]
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace AnimationPromptForm
{
public partial class AnimationPromptForm2 : Form
{
#region
public const Int32 AW_HOR_POSITIVE = 0x00000001; // 从左到右打开窗口
public const Int32 AW_HOR_NEGATIVE = 0x00000002; // 从右到左打开窗口
public const Int32 AW_VER_POSITIVE = 0x00000004; // 从上到下打开窗口
public const Int32 AW_VER_NEGATIVE = 0x00000008; // 从下到上打开窗口
public const Int32 AW_CENTER = 0x00000010; //若使用了AW_HIDE标志,则使窗口向内重叠;若未使用AW_HIDE标志,则使窗口向外扩展。
public const Int32 AW_HIDE = 0x00010000; //隐藏窗口,缺省则显示窗口。
public const Int32 AW_ACTIVATE = 0x00020000; //激活窗口。在使用了AW_HIDE标志后不要使用这个标志。
public const Int32 AW_SLIDE = 0x00040000; //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略。
public const Int32 AW_BLEND = 0x00080000; //使用淡出效果。只有当hWnd为顶层窗口的时候才可以使用此标志。
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool AnimateWindow(
IntPtr hwnd, // handle to window
int dwTime, // duration of animation
int dwFlags // animation type
);
#endregion
/// <summary>
///
/// </summary>
/// <param name="point">父窗体Location位置</param>
/// <param name="width">父窗体宽度</param>
/// <param name="messageText">展示信息字符串</param>
public AnimationPromptForm2(Point point, int width, string messageText)
{
InitializeComponent();
this.Location = new Point(((width - this.Width) / 2) + point.X, point.Y);//设置出现位置
lbPrompt.Text = messageText;
this.Load += Form_Load;
this.FormClosing += Form_FormClosing;
timer1.Tick += timer1_Tick;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Close();
}
/*从上往下显示*/
private void Form_Load(object sender, EventArgs e)
{
lbPrompt.Location = new Point((this.Width - lbPrompt.Width) / 2, (this.Height - lbPrompt.Height) / 2);//提示信息居中显示
timer1.Interval = 888;//显示时间
timer1.Start();
AnimateWindow(this.Handle, 500, AW_VER_POSITIVE);
}
/*从下往上关闭*/
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
AnimateWindow(this.Handle, 500, AW_SLIDE | AW_HIDE | AW_VER_NEGATIVE);
}
}
}
[后语]
本章源码地址:https://download.csdn.net/download/qq_38469552/15685241
由于工作原因,没有过多时间来排版和优化控件,控件较为粗糙,内部bug可能存在很多,各位朋友同仁如发现异常请轻喷并与我及时联系,以免误导他人.
本章完
本文介绍了一种在窗体中实现动态提示信息的方法,利用.NET Framework中的Timer组件结合user32.dll进行动画效果处理,实现了提示信息的自动显示与隐藏。
483

被折叠的 条评论
为什么被折叠?



