|
API函数全名(Application Programming Interface), win32 API 也就是MicrosoftWindows32 位平台的应用程序编程接口.能用到API函数有很多的,下面介绍一个简单的,来简单认识下API函数的应用,对于,窗体右下角经常弹出的小框框(Popup)我们是经常见到的,写出来跟大家分享下.一起学习下API函数
1.创建一个Popup窗体
添加 using System.Runtime.InteropServices; 引用(这里我给窗体添加的是张背景图,未涉及传值)
[code=csharp] public partial class Popup : Form
{
#region 定义标识窗体移动状态的枚举值
/// <summary>
/// 定义标识窗体移动状态的枚举值
/// </summary>
protected enum FormState
{
Hide = 0,//隐藏窗体
Display = 1,//显示窗体
Displaying = 2,//显示窗体中
Hiding = 3 //隐藏窗体中
}
#endregion
#region 声明的变量
private System.Drawing.Rectangle Rect;//定义一个存储矩形框的数组
private FormState InfoStyle = FormState.Hide;//定义变量为隐藏
static private Popup dropDownForm = new Popup();//实例化当前窗体
private static int AW_HIDE = 0x00010000; //该变量表示动画隐藏窗体
private static int AW_SLIDE = 0x00040000;//该变量表示出现滑行效果的窗体
private static int AW_VER_NEGATIVE = 0x00000008;//该变量表示从下向上开屏
private static int AW_VER_POSITIVE = 0x00000004;//该变量表示从上向下开屏
#endregion
#region 定义标识窗体移动状态的枚举值
#endregion
#region 用属性标识当前状态
/// <summary>
/// 用属性标识当前状态
/// </summary>
protected FormState FormNowState
{
get { return this.InfoStyle; } //返回窗体的当前状态
set { this.InfoStyle = value; } //设定窗体当前状态的值
}
#endregion
#region 调用API函数显示窗体
[DllImportAttribute("user32.dll")]
private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
#endregion
public Popup()
{
InitializeComponent();
//初始化工作区大小
System.Drawing.Rectangle rect = System.Windows.Forms.Screen.GetWorkingArea(this);//实例化一个当前窗口的对象
this.Rect = new System.Drawing.Rectangle(rect.Right - this.Width - 1, rect.Bottom - this.Height - 1, this.Width, this.Height);//为实例化的对象创建工作区域
}
#region 显示窗体
public void ShowForm()
{
switch (this.FormNowState)
{
case FormState.Hide:
if (this.Height <= this.Rect.Height - 192)//当窗体没有完全显示时
{
this.SetBounds(Rect.X, this.Top - 192, Rect.Width, this.Height + 192);//使窗体不断上移
}
else
{
this.SetBounds(Rect.X, Rect.Y, Rect.Width, Rect.Height);//设置当前窗体的边界
}
AnimateWindow(this.Handle, 800, AW_SLIDE + AW_VER_NEGATIVE);//动态显示本窗体
break;
}
}
#endregion
#region 关闭窗体
public void CloseForm()
{
AnimateWindow(this.Handle, 800, AW_SLIDE + AW_VER_POSITIVE + AW_HIDE);//动画隐藏窗体
this.FormNowState = FormState.Hide;//设定当前窗体的状态为隐藏
}
#endregion
#region 返回当前窗体的实例化对象
static public Popup Instance()
{
return dropDownForm; //返回当前窗体的实例化对象
}
#endregion
}[/code]2.创建一个登陆的窗体包含2个按钮,一个是显示(button1),一个关闭(button2) [code=csharp]/// <summary>
/// 弹出动画窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1 _Tick(object sender, EventArgs e)
{
Popup.Instance().ShowForm();
}
/// <summary>
/// 关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1 _Tick(object sender, EventArgs e)
{
Popup.Instance().CloseForm();
}[/code]
|
|