利用Visual C# 2005制作简单动画效果

利用Visual C# 2005制作简单动画效果,第1张

一般的 Windows Form 通常是运用各种控件来显示数据 然而如果您希望在窗体中加入特殊效果来凸显数据内容 那帆滚么图形与动画将是非常不错的选择 一般来说 我们会使用 Net Framework中 的 GDI+ 函式库来制作图形与动画效果 在 GDI+ 还没有推出之前 如果要产生二维的 向量图形 影像 以及印刷样式 必须使用旧版 *** 作系统中的GDI 新的 GDI+ 是 Windows XP 的一部份 除了加入新功能之外 还最佳化现有功能以便具体改进 GDI(也就是旧版 Windows 包含的绘图装置接口)的效能 程序范例 图表 图表 图表

我们的程序范例示范了三种动画效果 分别是 眨眼效果 d跳的球 以及文字闪烁 当程序执行时会自动展示第一种眨眼效果 如图表 到 所示 运用之前「如何利用程序代码动态存取组件信息」的技巧 将组件的 A *** FQName 属性值指派给窗体的 Text 属性 并将先前已经加入项目资源的四张图片名称指派给数组 之后就使用此数组来示范眨眼效果 程序代码撰写于窗体的Load事件处理例程中 如下所示

private void Blog_DemoForm _Load(object sender EventArgs e){ AssemblyInfoClass myAssembly = new AssemblyInfoClass() this Text = myAssembly A *** FQName 陪轿团// 指派数组成员  arrImages[ ] = Properties Resources Eye  arrImages[ ] = Properties Resources Eye  arrImages[ ] = Properties Resources Eye  arrImages[ ] = Properties Resources Eye } 图表 如果您要使用 Visual C# 来制作「关于」对话框 建议先使用Visual Studio 所提供的模板芦橘来产生关于对话框窗体 然后再自订窗体所要呈现的内容(如图表 所示) 在此 我们选择将组件的相关信息填入窗体对应的控件 请于「关于」对话框窗体的 Load 事件处理例程中撰写下列程序代码 private void AboutBox_Load(object sender EventArgs e){ AssemblyInfoClass myAssembly = new AssemblyInfoClass() labelProductName Text = 产品名称 + myAssembly Product labelVersion Text = 版本 + myAssembly Version labelCopyright Text = 版权宣告 + myAssembly Copyright labelCompanyName Text = 公司名称 + myAssembly Company textBoxDescription Text = 细部描述 +  myAssembly Description}

要显示「关于」对话框 请替「说明」菜单项目的Click事件处理例程中撰写下列程序代码

private void toolStripMenuItem _Click(object sender EventArgs e){ // 显示关于对话框  AboutBox MyAboutBox = new AboutBox() // 设定关于对话框的启始位置  MyAboutBox StartPosition = FormStartPosition CenterScreen MyAboutBox Show()}当用户点选不同的选项按钮时 将会执行下列程序代码来显示不同的动画效果 这些程序代码撰写于选项按钮的 CheckedChanged 事件处理函式中 如下所列 private void RadioButtons_CheckedChanged(object sender EventArgs e){ if(optWink Checked) {tmrAnimation Interval = WINK_TIMER_INTERVAL } else if(optBall Checked) {tmrAnimation Interval = BALL_TIMER_INTERVAL } else if(optText Checked) {tmrAnimation Interval = TEXT_TIMER_INTERVAL } OnResize(EventArgs Empty)}自订函式 RadioButtons_CheckedChanged 会叫用 OnResize 函式来产生不同的图形 请大家注意 我们系使用 Graphics 类别的 FillEllipse 方法来绘制球形 程序代码如下所列 protected override void OnResize(EventArgs ea){ if (optWink Checked) {Graphics grfx = CreateGraphics()// 重绘窗体 this Refresh() } else if (optBall Checked) {Graphics grfx = CreateGraphics()grfx Clear(BackColor)double dblRadius = Math Min(ClientSize Width / grfx DpiX ClientSize Height / grfx DpiY) / intBallSizeintBallRadiusX = (int)(dblRadius * grfx DpiX)intBallRadiusY = (int)(dblRadius * grfx DpiY)intBallMoveX = (int)(Math Max( intBallRadiusX / intMoveSize))intBallMoveY = (int)(Math Max( intBallRadiusY / intMoveSize))intBitmapWidthMargin = intBallMoveXintBitmapHeightMargin = intBallMoveYintBallBitmapWidth = * (intBallRadiusX + intBitmapWidthMargin)intBallBitmapHeight = * (intBallRadiusY + intBitmapHeightMargin)bitmap = new Bitmap(intBallBitmapWidth intBallBitmapHeight)grfx = Graphics FromImage(bitmap)grfx Clear(BackColor)// 绘制球形 grfx FillEllipse(Brushes Red new Rectangle(intBallMoveX intBallMoveY * intBallRadiusX * intBallRadiusY))intBallPositionX = (int)(ClientSize Width / )intBallPositionY = (int)(ClientSize Height / ) } else if (optText Checked) {Graphics grfx = CreateGraphics()grfx Clear(BackColor) }}

最后 利用定时器将图形连续重绘于窗体上 便产生了动画效果 程序代码撰写于定时器的 Tick 事件处理例程中 如下所示

private void tmrAnimation_Tick(object sender EventArgs e){ // 眨眼效果  if(optWink Checked) {Graphics grfx = CreateGraphics()// 将数组中之图形绘制在画面上 grfx DrawImage(arrImages[intCurrentImage] (int)((ClientSize Width arrImages[intCurrentImage] Width) / ) (int)((ClientSize Height arrImages[intCurrentImage] Height) / ) arrImages[intCurrentImage] Width arrImages[intCurrentImage] Height)intCurrentImage += jif(intCurrentImage == ){ j = }else if(intCurrentImage == ){ j = } } else if(optBall Checked) // d跳的球  {Graphics grfx = CreateGraphics()// 将球绘制在画面上 grfx DrawImage(bitmap (int)(intBallPositionX intBallBitmapWidth / ) (int)(intBallPositionY intBallBitmapHeight / ) intBallBitmapWidth intBallBitmapHeight)// 移动球的位置 intBallPositionX += intBallMoveXintBallPositionY += intBallMoveY// 球碰到左右边界 if(intBallPositionX + intBallRadiusX >= ClientSize Width || intBallPositionX intBallRadiusX <= ){ intBallMoveX = intBallMoveX SystemSounds Beep Play()}// 球碰到上下边界 if(intBallPositionY + intBallRadiusY >= ClientSize Height || intBallPositionY intBallRadiusY <= ){ intBallMoveY = intBallMoveY SystemSounds Beep Play()} } else if (optText Checked) // 闪动文字  {Graphics grfx = CreateGraphics()// 设定文字的字型与大小 Font font = new Font( Microsoft Sans Serif FontStyle Bold GraphicsUnit Point)// 设定要显示的文字 string strText = 章立民研究室 SizeF sizfText = new SizeF(grfx MeasureString(strText font))// X坐标与Y坐标的配对 PointF ptfTextStart = new PointF((float)(ClientSize Width sizfText Width) / (float)(ClientSize Height sizfText Height) / )PointF ptfGradientStart = new PointF( )PointF ptfGradientEnd = new PointF(intCurrentGradientShift )// 设定笔刷 LinearGradientBrush grBrush = new LinearGradientBrush(ptfGradientStart ptfGradientEnd Color Blue BackColor)// 将文字绘制在画面上 grfx DrawString(strText font grBrush ptfTextStart)// 以不同的坐标绘制文字 造成闪动效果 intCurrentGradientShift += intGradiantStepif (intCurrentGradientShift == ){ intGradiantStep = }else if (intCurrentGradientShift == ){ intGradiantStep = } }} lishixinzhi/Article/program/net/201311/13166

1.可以考虑用Turbo C的绘图早知御函数(附加graphic.h库)或者用opengl+glut等来实现。 2.前者一般就是纯粹的画点画线。网上也能找到教程。3.主要说一下后者。可以导入图片,并且二维、三维动画都可以做,甚至是用来开发游戏。后者可以用vc6.0或者vs2005来开发。跨平台。参考教程: http://www.owlei.com/DancingWind/看你的描述要做比较偏数学的东西,那你自己得弄明白如何去实现绘制算法。要实现的动画本身而搭的基本框架不猛明会很复杂的,可能100行代码都不用。4.另外还有opencv、GDI之类的可能更适合二维图像处理库,但我自己陆岩不是很了解了,你也可以查一查用哪种绘图库比较适合你。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12241059.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存