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#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#比较二个数组并找出相同或不同元素的方法