這里主要利用API函數Animate Window實現窗體左右,上下,擴展,淡入滑動或滾動動畫效果,步驟如下:
1.新建窗體,使用2個GroupBox控件。
2.在控件1中添加2個RadioButton控件,并設置Text分別為“滾動窗體”,“滑動窗體”,并使前者Checked設置為True。
3.在空間2中添加6個按鈕,Text分別為“自左向右動畫”,“自右向左動畫”,“自上向下動畫”,“自下向上動畫”,“向外擴展動畫”,“淡入動畫窗體”。
4.添加一新的Window窗體,設置Text為“動畫窗體”。設置其“BackgroundImage”屬性,導入一張要加載的圖像,然后設置其“BackgroundImageLayout”屬性為“Stretch”。
5.各按鈕事件主要代碼如下:
private void button1_Click(object sender, EventArgs e)
{
Form2 myf = new Form2();
if (radioButton1.Checked == true)
{
myf.Text = "自左向右滾動窗體動畫效果";
}
else
{
myf.Text = "自左向右滑動窗體動畫效果";
}
myf.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form2 myf = new Form2();
if (radioButton1.Checked == true)
{
myf.Text = "自右向左滾動窗體動畫效果";
}
else
{
myf.Text = "自右向左滑動窗體動畫效果";
}
myf.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 myf = new Form2();
if (radioButton1.Checked == true)
{
myf.Text = "自上向下滾動窗體動畫效果";
}
else
{
myf.Text = "自上向下滑動窗體動畫效果";
}
myf.Show();
}
private void button5_Click(object sender, EventArgs e)
{
Form2 myf = new Form2();
if (radioButton1.Checked == true)
{
myf.Text = "自下向上滾動窗體動畫效果";
}
else
{
myf.Text = "自下向上滑動窗體動畫效果";
}
myf.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form2 myf = new Form2();
myf.Text = "向外擴展窗體動畫效果";
myf.Show();
}
private void button6_Click(object sender, EventArgs e)
{
Form2 myf = new Form2();
myf.Text = "淡入窗體動畫效果";
myf.Show();
}
6.雙擊Form2窗體,進入代碼視圖。首先定義公用變量,具體代碼如下:
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;
public const Int32 AW_HIDE = 0X00010000;
public const Int32 AW_ACTIVATE = 0X00020000;
public const Int32 AW_SLIDE = 0X00040000;
public const Int32 AW_BLEND = 0X00080000;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
private static extern bool AnimateWindow(IntPtr hwnd,int dwTime,int dwFlags);
7.下面為Form2窗體添加加載事件代碼,具體如下:
private void Form2_Load(object sender, EventArgs e)
{
if (this.Text == "自左向右滾動窗體動畫效果")
{
AnimateWindow(this.Handle,2000,AW_HOR_POSITIVE);
}
if (this.Text == "自左向右滑動窗體動畫效果")
{
AnimateWindow(this.Handle, 2000, AW_SLIDE+AW_HOR_POSITIVE);
}
if (this.Text == "自右向左滾動窗體動畫效果")
{
AnimateWindow(this.Handle, 2000, AW_HOR_NEGATIVE);
}
if (this.Text == "自右向左滑動窗體動畫效果")
{
AnimateWindow(this.Handle, 2000, AW_SLIDE + AW_HOR_NEGATIVE);
}
if (this.Text == "自上向下滾動窗體動畫效果")
{
AnimateWindow(this.Handle, 2000, AW_VER_POSITIVE);
}
if (this.Text == "自上向下滑動窗體動畫效果")
{
AnimateWindow(this.Handle, 2000, AW_SLIDE + AW_VER_POSITIVE);
}
if (this.Text == "自下向上滾動窗體動畫效果")
{
AnimateWindow(this.Handle, 2000, AW_VER_NEGATIVE);
}
if (this.Text == "自下向上滑動窗體動畫效果")
{
AnimateWindow(this.Handle, 2000, AW_SLIDE + AW_VER_NEGATIVE);
}
if (this.Text == "向外擴展窗體動畫效果")
{
AnimateWindow(this.Handle, 2000, AW_SLIDE + AW_CENTER);
}
if (this.Text == "淡入窗體動畫效果")
{
AnimateWindow(this.Handle, 2000, AW_BLEND);
}
}//yinyiniao's Blog