/*
* 模拟文件复制过程。
* 在窗体上放置一个按钮控件、一个ProgressBar控件。将按钮控件的Text属性设置为“模拟文件复制”。
*/
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 = "模拟文件复制";
progressBar1.Maximum = 1000000;
progressBar1.Minimum = 0;
progressBar1.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Visible = true;
for (int i = 0; i < progressBar1.Maximum * 0.9; i++)
progressBar1.Value = i;
progressBar1.Visible = false;
MessageBox.Show("文件复制完成");
}
}
}
运行结果:
/*
* Timer应用。
* 设计一个窗体,窗体上有一个Timer控件,有一个标签Label1,其文本内容为“欢迎使用Visual C#”。
* 当启动窗体后,标签上的文本随机在窗口中移动。
*/
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "欢迎使用Visual C#";
timer1.Enabled = true;
timer1.Interval = 200;
}
private byte d1;
private byte d2;
private float size = 0f;
private Random rd = new Random();
//用私有字段d1表示文字的移动方向,d2表示文体大小的变化方向,size表示变化后的字体大小
private void timer1_Tick(object sender, EventArgs e)
{
if (d1==0)
{
label1.Left += 10;
if (label1.Right > this.ClientSize.Width)
d1 = 1;
else
{
label1.Left -= 10;
if (label1.Left<0)
d1 = 0;
}
}
if (d2 == 0)
{
size = label1.Font.Size + 2;
label1.Font = new Font("宋体", size);
if (size > 50)
d2 = 1;
}
else
{
size = label1.Font.Size - 2;
label1.Font = new Font("宋体", size);
if (size < 10)
d2 = 0;
}
//rd产生0-255的随机数,再由Color类的静态方法FromArgb产生随机颜色。
label1.ForeColor = Color.FromArgb(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255));
}
}
}
//定时器控件仅包括一个Tick事件。当定时器处于运行状态时,按照设定的Interval时间间隔自动触发事件。
运行结果:
运行时,文字“欢迎使用Visual C#”在窗体中左右移动,同时变化文体大小和颜色。
/*
* 图片框应用。
* ToolTip控件应用。
*/
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "退出";
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //使图片适应控件大小
pictureBox1.Image = Image.FromFile(Application.StartupPath + @"\g.png"); //将一个图片装入图片框
//为指定的控件设置提示信息 方法格式:SetToolTip(控件名,"提示信息");
toolTip1.SetToolTip(pictureBox1, "这是一幅图片");
toolTip2.SetToolTip(button1, "退出");
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
运行结果 :
运行时,当鼠标指向图片时,显示“这是一幅图片";当鼠标指向“退出”按钮时,显示“退出”。