C#—实验9.1、9.2、9.3和9.4

/*
 * 设计电子时钟。要求:启动窗体后,在窗体的标签上显示系统当前日期和时间;单机结束按钮,则停止显示。
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "结束";
            timer1.Enabled = true;
            timer1.Interval = 1000;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Visible = false;
        }
    }
}


运行结果:


/*
 * 利用Timer和图片框控件,编写一个图片不断向左移动的小动画。
 * 要求:
 * (1)改变图片的Left值,图片向左(右)移动;改变图片的Top值,图片向下(上)左移动;同时改变图片的Left和Top值,图片斜向移动;
 * (2)利用Random类的Next方法产生一定范围的数据作为Left值和Top值,可以使图片随意移动;
 * (3)图片不要移出窗体,如果Left值或者Top值超出窗体的范围,能控制图片回到窗体的最左端或最上端。
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private int num1;
        private int num2;
        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "左右移动";
            button2.Text = "斜向移动";
            button3.Text = "随机";
            button4.Text = "结束";
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = Image.FromFile(Application.StartupPath + @"\g.png");
            pictureBox1.Left = 0;
            pictureBox1.Top = 0;
            timer1.Enabled = false;
            timer1.Interval = 50;
            timer2.Enabled = false;
            timer2.Interval = 50;
            timer3.Enabled = false;
            timer3.Interval = 50;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
            timer2.Stop();
            timer3.Stop();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer2.Start();
            timer1.Stop();
            timer3.Stop();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            timer2.Stop();
            timer3.Start();
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            timer2.Stop();
            timer3.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Left += 10;
            if (pictureBox1.Left > this.ClientSize.Width-150)
            {
                pictureBox1.Left = 1;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            pictureBox1.Left += 10;
            pictureBox1.Top += 10;
            if (pictureBox1.Left > this.ClientSize.Width - 150)
            {
                pictureBox1.Left = 1;
            }
            if (pictureBox1.Top > this.ClientSize.Height)
            {
                pictureBox1.Top = 1;
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            Random rd = new Random();
            num1 = rd.Next(0, 255);
            num2 = rd.Next(0, 255);
            pictureBox1.Left = num1;
            pictureBox1.Top = num2;

        }          
    }
}
运行结果:


/*
 * 该窗体中有一个图片框,显示一副图片。
 * 要求:
 * (1)该窗体打开时,动态加载图片;
 * (2)设置图片显示模式为根据图片框大小缩放图片;
 * (3)当鼠标留在图片框时,显示如“蛋糕”的文本提示(使用ToolTip控件);
 * (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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "加载";
            button2.Text = "结束程序";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.pictureBox1.Image = Image.FromFile("g.png");   //动态加载
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            toolTip1.SetToolTip(pictureBox1, "this is a cat");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult rs = MessageBox.Show("确定要结束程序吗?[Yes|No]", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            if (rs == DialogResult.Yes)
                Application.Exit();
        }
    }
}

运行结果:




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值