本文实例调用API函数实现,实现以下两个目的:
1.显示窗体时,具有淡出效果;
2.显示窗体后,窗体可以不被激活。
public partial class Form39 : Form
{
//取出激活窗体
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetActiveWindow();
//设置激活窗体
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr SetActiveWindow(IntPtr hwnd);
//动画窗体
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void AnimateWindow(IntPtr hwnd,int stime,int style);
private IntPtr mActHandle = IntPtr.Zero;
private bool mFirstShow = true;
public Form39()
{
//取出当前WINDOWS桌面激活的窗体句柄
mActHandle = GetActiveWindow();
InitializeComponent();
}
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
//在第一次进入本窗体的激活事件中,还原原窗体的激活状态。
if (mActHandle !=IntPtr.Zero && mFirstShow )
{
SetActiveWindow(mActHandle);
mFirstShow = false;
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//以动画的方式出现窗体,参数style的枚举方式,可百度查询
// 0x80000 /* AW_BLEND*/
AnimateWindow(this.Handle, 2000, 0x80000 /* AW_BLEND*/);
}
}