c#制作飘动动画窗体

  1. 先在vs里创建一个项目,项目的类型选择vc#,创建一个windows窗体应用程序如下图:

  2. 单击确定后,在窗体中设置窗体内容,从工具箱向窗体添加三个计时器,如下图:

  3. 在窗体空白处,单击鼠标右键,单击“查看代码”,在初始化函数下添加如下代码:如下图:

    代码如下:

    int ScreenWidth = SystemInformation.PrimaryMonitorMaximizedWindowSize.Width;   //屏幕的宽度

     int ScreenHeight=SystemInformation.PrimaryMonitorMaximizedWindowSize.Heigh;//屏幕的高度

  4. 双击timer1,在事件函数里添加如下代码,如下图:

    代码如下:

     Point MyPos = new Point(this.DesktopLocation.X, this.DesktopLocation.Y);       //窗体的当前位置

                if (MyPos.X + Width < ScreenWidth)

                {

                    this.DesktopLocation = new Point(MyPos.X + 1, MyPos.Y);

                }

                else

                {

                    this.DesktopLocation = new Point(0, 0);

                }

     

  5. 双击“水平飘动”,在事件函数里添加如下代码,如下图:

  6. 代码如下:

                this.timer1.Enabled = true;

                this.timer2.Enabled = false;

                this.timer3.Enabled = false;

  7. 同理双击“垂直飘动”添加代码:

                 this.timer1.Enabled  = false ;

                this.timer2.Enabled = true ;

                this.timer3.Enabled = false;

  8. 双击timer2,在事件响应函数添加如下代码,如下图:代码如下:

     Point MyPos = new Point(this.DesktopLocation.X, this.DesktopLocation.Y);       //窗体的当前位置

                if ( MyPos.Y + Height < ScreenHeight)

                {

                    this.DesktopLocation = new Point(MyPos.X, MyPos.Y + 1);

                }

                else

                {

                    this.DesktopLocation = new Point(0, 0);

                }

  9. 同理双击timer3,添加如下代码:

     Point MyPos = new Point(this.DesktopLocation.X, this.DesktopLocation.Y);       //窗体的当前位置

                if (MyPos.X + Width < ScreenWidth || MyPos.Y + Height < ScreenHeight)

                {

                    this.DesktopLocation = new Point(MyPos.X + 1, MyPos.Y + 1);

                }

                else

                {

                    this.DesktopLocation = new Point(0, 0);

                }

     

  10. 然后双击“飘动窗体”添加如下代码:

               this.timer1.Enabled = false ;

                this.timer2.Enabled = false;

                this.timer3.Enabled = true ;

  11. 同理双击停止飘动,添加如下代码:

                 this.timer1.Enabled = false ;

                this.timer2.Enabled = false;

                this.timer3.Enabled = false;

  12. 最后在窗体上空白处双击,创建Form1_Load事件,添加如下代码,如下图:

    代码如下:this.timer1.Enabled = true;

  13. 开始调试,运行结果如下图:

posted on 2017-02-10 23:05  带刀侍卫++i  阅读(1427)  评论(0编辑  收藏  举报

导航