c#之利用API函數實現動畫表單的方法詳解

來源:互聯網
上載者:User

這裡主要 利用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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.