使用C#做一个简易的解压窗体程序

利用C#计时器做一个窗体程序

类似于win7屏保的七彩泡泡(小时候可喜欢那个了)

  • 一、思路略要
    1、小球的运动轨迹:小球初始会有一个运动轨迹(即Left++,Top++)。
    2、小球运动轨迹的改变:当超出屏幕边缘后会改变其运动轨迹(Left±±,Top±±)。
    3、多个小球的轨迹:如上文所示,再新实例化一个小球的对象即可。

  • 二、思路详解
    1、小球轨迹:C#是一门面向对象的编程语言,对于小球的运动轨迹,我们可以看做是一边上下运动,一边左右运动,改变其运动的因素是是否超出边缘部分。
    即在Y轴上:

    //向下运动时
    	this.Top++;
    //向上运动时
    	this.Top--;
    

    在X轴上:

    //向右运动时:
    	this.Letf++;
    //向左运动时:
    	this.Left--;
    

    2、小球轨迹的改变:
    首先使用全局变量获取屏幕的尺寸:

    //获取屏幕宽高
        int innerWidth = Screen.PrimaryScreen.Bounds.Width;
        int innerHeight = Screen.PrimaryScreen.Bounds.Height;
    

    再使用全局变量控制小球的运动状态:

    //设置运动状态控制器
        int ycount = 0;
        int xcount = 0;
    //对应运动状态设置小球运动方向
    //竖直方向
    		    if (ycount == 0)
                {
                    this.Top ++;
                } else if (ycount == 1)
                {
                    this.Top --;
                }
    //水平方向
        		if (xcount==0)
                {
                    this.Left ++;
                }else if (xcount == 1)
                {
                    this.Left --;
                }
    
  • 三、代码部分
    全局变量

    //获取屏幕宽高
        int innerWidth = Screen.PrimaryScreen.Bounds.Width;
        int innerHeight = Screen.PrimaryScreen.Bounds.Height;
    //设置运动状态控制器
        int ycount = 0;
        int xcount = 0;
    

    窗体加载:

    //美化该窗体
    		BackColor = Color.Khaki;
            this.FormBorderStyle = FormBorderStyle.None;
            this.Height = 200;
            this.Width = 200;
            this.Location = new Point(0, 300);
    //实例化一个画笔对象
            GraphicsPath gr = new GraphicsPath();
    //绘制一个圆
            gr.AddEllipse(0, 0, this.Width, this.Height);
    //在窗体显示
            this.Region = new Region(gr);
    //开启计时器
            timer1.Start();
    

    计时器

    //竖直运动部分
    		 if (this.Top+this.Height > innerHeight)
            {
                ycount = 1;
            }else if (this.Top<0)
            {
                ycount = 0;
            }
            
            if (ycount == 0)
                {
                    this.Top ++;
                } else if (ycount == 1)
                {
                    this.Top --;
                }
    //------------------------------------------------------------------
    //水平运动部分
    		if (this.Width+this.Left>innerWidth)
            {
                xcount = 1;
            }else if (this.Left < 0)
            {
                xcount = 0;
            }
            
            if (xcount==0)
                {
                    this.Left ++;
                }else if (xcount == 1)
                {
                    this.Left --;
                }
    

    这样就可以实现单个小球在窗口里一直动起来了,如需添加第二个小球则可以实例化第二个小球,然后让其在窗体里显示出来。

以上

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值