黑马程序员_窗体应用程序练习之图片飞飞

---------------------- Windows Phone 7手机开发 .Net培训、期待与您交流! ----------------------

图片飞飞就是图片在屏幕上上下左右不停的动,就和屏保类似,下面是具体操作。

1、新建窗体应用程序

2、添加PictureBox控件,设定相关属性:

        在父容器中停靠;

        缩放模式为stretchImage(拉伸并填充整个窗体);

        Image属性用来添加图片(需要图片资源),语法为:Image.FromFile(图片的完全路径)

        例如:

 private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(@"F:\练习\我的第一个应用程序\图片飞飞0808\bin\Debug\pp\0.jpg");
        }

补充:Timer的使用

找到Timer组件,拖入Timer控件,设定属性:

      启动Timer,Enable设为True

      Interval执行时间间隔(每隔多少毫秒执行一次);

      添加运行代码,

      例如:

private void timer1_Tick(object sender, EventArgs e)
        {
                     pictureBox1.Image = Image.FromFile(@"F:\练习\我的第一个应用程序\图片飞飞0808\bin\Debug\pp\0.jpg");
        }

3、现在让图片每隔两秒执行一次:

结合PictureBoxTimer控件,Timer控件的Interval设为2000,实现图片切换:

       int index = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(@"F:\练习\我的第一个应用程序\图片飞飞0808\bin\Debug\pp\" + index + ".jpg");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            index = index +1;
            if (index > 4)
            {
                index = 0;
            }
            pictureBox1.Image = Image.FromFile(@"F:\练习\我的第一个应用程序\图片飞飞0808\bin\Debug\pp\" + index + ".jpg");
        }    

 4、实现窗体可以移动

     Point类型,使用Location属性可以为窗体设定位置,this.Location=new Point(x,y);

     考虑窗体飞到外面去了,需要让它弹回来,得到屏幕的范围:Screen.GetWorkingArea(new Point(0,0));

     返回一个Rectangle类型的变量,可以点出width和height得到屏幕的宽高;

        int xSpeed = 2;
        int ySpeed = 3;
        private void timer2_Tick(object sender, EventArgs e)
        {
            Rectangle screenRectangle = Screen.GetWorkingArea(new Point(0, 0));
            if (this.Location.X > screenRectangle.Width - this.Width)
            {
                xSpeed = -xSpeed;
            }
            if (this.Location.X < 0)
            {
                xSpeed = -xSpeed;
            }
            if (this.Location.Y > screenRectangle.Height - this.Height || this.Location.Y < 0)
            {
                ySpeed = -ySpeed;
            }

            this.Location = new Point(this.Location.X + xSpeed, this.Location.Y+ySpeed);

        }

     不想窗体有最大化、关闭等,和鼠标可拖动的功能,选中窗体,属性中FromBorderStyle设为None;

     那么现在程序关不了,此时需要用到系统托盘:

     添加一个系统托盘,拖入ContextMenuScript控件,添加菜单项和事件,Application.Exit()表示结束应用程序

     拖入一个NotifyIncon控件,设定图标,并和菜单控件绑定;

     在任务栏中去掉,选择From窗体,属性中ShowInTaskbar设为False;

    去掉窗体边框---->设定窗体在前面---->不在任务栏显示---->调整时间 实现了优化;

    补充:

       this.close();//结束一个窗体

       Application.Exit();//结束整个窗体

 

----------------------Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

详细请查看:http://net.itheima.com/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值