您的位置:首页 > 编程语言 > C#

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

2016-05-27 19:19 417 查看
/*
* 设计电子时钟。要求:启动窗体后,在窗体的标签上显示系统当前日期和时间;单机结束按钮,则停止显示。
*/
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();
}
}
}

运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#