欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

C#简单的动画效果_.NET WinForm窗体或控件动画实例

程序员文章站 2021-12-13 20:39:29
...

介绍

目前有很多的简单的技术可以让你的Windows应用程序实现动画效果、在这里、我可以解释一个简单的动画(移动特效)技术给你的Windows窗体或任何其他控件


源代码分析

请按照下列步骤操作:

1、创建一个解决方案(如:SimpleAnimation)


2、将您的控件拖放在你的Windows窗体就可以了

例如:

Split - scnrMain

Buttons - btnAnimation1,btnAnimation2,btnReset,btnQuit

Panel - pnlMain


3、添加一个计时器控件(如:tmrMain)


4、编写代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleAnimation
{
public partial class frmMain : Form
{
	int scnrWidth;
	int scnrHeight;
	int pnlWidth;
	int pnlHeight;

	public frmMain()
	{
		InitializeComponent();
	}

	public void frmMain_Load(object sender, EventArgs e)
	{
		pnlMain.Width = 0;
		pnlMain.Height = 0;

		scnrWidth = scnrMain.Panel2.Width;
		scnrHeight = scnrMain.Panel2.Height;
		pnlWidth = pnlMain.Width;
		pnlHeight = pnlMain.Height;
	}

	private void btnShowPanel_Click(object sender, EventArgs e)
	{
		tmrMain.Enabled = true;
		btnAnimation1.Enabled = false;
	}

	private void tmrMain_Tick(object sender, EventArgs e)
	{
		while(pnlWidth < scnrWidth || pnlHeight < scnrHeight)
		{
			if (pnlMain.Width < scnrWidth)
			{
				pnlMain.Width = pnlWidth   1;
				pnlWidth  ;
			}
			if (pnlMain.Height < scnrHeight)
			{
				pnlMain.Height = pnlHeight   1;
				pnlHeight  ;
			}
			break;
		}
	}

	private void btnShowAnotherPanel_Click(object sender, EventArgs e)
	{
		pnlMain.Height = scnrHeight;
		while (pnlMain.Width < scnrWidth)
		{
			pnlMain.Width  ;
		}
		btnAnimation2.Enabled = false;
	}

	private void btnReset_Click(object sender, EventArgs e)
	{
		pnlMain.Width = 0;
		pnlMain.Height = 0;

		scnrWidth = scnrMain.Panel2.Width;
		scnrHeight = scnrMain.Panel2.Height;
		pnlWidth = pnlMain.Width;
		pnlHeight = pnlMain.Height;
		btnAnimation1.Enabled = true;
		btnAnimation2.Enabled = true;
		tmrMain.Enabled = false;
	}

	private void btnQuitApp_Click(object sender, EventArgs e)
	{
		while (this.Height>60)
		{
			this.Height--;
		}
		Application.Exit();
	}
}
}

最后给大家献上源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1dD0AjZV 密码: r47y