练习
任务
1、绘制一圆,其圆心坐标为(150,100),半径为40。要求用蓝色且宽度为1画笔。
2、绘制一圆,其圆心坐标为(200,200),半径为100。要求用红色画实心画刷(SolidBrush类的一个实例)。
3、利用颜色对话框将窗体的背景颜色修改为颜色对话框所选的颜色。
4、利用字体对话框将窗体的上的文本框textBox1的字体修改为字体对话框所选字体。
5、利用保存文件对话框,利用FileStream和StreamWriter将1-100内的100个数字按顺序写入到保存文件对话框所指定的文件中,每个数字写一行。
6、利用打开文件对话框,利用FileStream和StreamReader读取所选文件的内容(如第5题中的文件),把文件的内容显示到窗体的ListBox1中。
7、基础点绘制。
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace FuXiText
{
public partial class Form1 : Form
{
int i = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics g = CreateGraphics();
Pen p1 = new Pen(Color.Blue, 1);
Point point = new Point(150, 100);
Size size = new Size(40, 40);
Rectangle r1=new Rectangle(point, size);
g.DrawEllipse(p1, r1);
g.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
Graphics g = CreateGraphics();
Brush brush = new SolidBrush(Color.Red);
Point p = new Point(200, 200);
Size size = new Size(100, 100);
Rectangle rec = new Rectangle(p, size);
g.FillEllipse(brush, rec);
g.Dispose();
}
private void button3_Click(object sender, EventArgs e)
{
ColorDialog cd=new ColorDialog();
DialogResult dr=cd.ShowDialog();
if (dr == DialogResult.OK)
{
MessageBox.Show("设置成功!!!");
BackColor = cd.Color;
}
}
private void button4_Click(object sender, EventArgs e)
{
DialogResult cd=MessageBox.Show("危险!!!","确定要刷新吗?",MessageBoxButtons.OKCancel);
if (cd == DialogResult.OK)
{
this.Controls.Clear();//刷新控件,控件会消失,可以重新调用函数重新渲染
MessageBox.Show("憨批");
MessageBox.Show("拜拜了您嘞");
}
else {
MessageBox.Show("可惜~~~");
using (Graphics g = this.CreateGraphics())
{
g.Clear(this.BackColor);
}//GDI+画图内容刷新
BackColor = Color.White;
}
}
private void button5_Click(object sender, EventArgs e)
{
FontDialog fd= new FontDialog();
DialogResult dr=fd.ShowDialog();
if (dr == DialogResult.OK)
{
MessageBox.Show("设置成功!!!");
textBox1.Font= fd.Font;
textBox1.Text = "骗你的~~~";
}
}
private void button6_Click(object sender, EventArgs e)
{
SaveFileDialog sfd= new SaveFileDialog();
sfd.Filter = "文本文件(txt)|*.txt";
sfd.Title = "保存文件";
DialogResult dr=sfd.ShowDialog();
if (dr == DialogResult.OK)
{ //创建文本流
FileStream fs=new FileStream(sfd.FileName, FileMode.OpenOrCreate);
//创建读写器
StreamWriter sw=new StreamWriter(fs);
for (int i = 1; i <= 100; i++)
{
//执行读写操作
sw.WriteLine(i.ToString());
}
textBox1.Text = "存储完毕~~~";
sw.Close();
sfd.Dispose();
}
}
private void button7_Click(object sender, EventArgs e)
{
OpenFileDialog ofd= new OpenFileDialog();
ofd.Filter = "文本txt|*.txt";
ofd.Title = "打开文件";
DialogResult dr= ofd.ShowDialog();
if (dr == DialogResult.OK)
{
FileStream fs = new FileStream(ofd.FileName, FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(fs);
string line;
while ((line=sr.ReadLine())!=null)
{
listBox1.Items.Add(line);
}
sr.Close();
ofd.Dispose();
}
else {
MessageBox.Show("打开失败");
}
textBox1.Text = "读取成功~~~";
}
private void button8_Click(object sender, EventArgs e)
{
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Brush brush = new SolidBrush(Color.Pink);
Graphics g=CreateGraphics();
if (e.Button == MouseButtons.Left)
{
g.FillEllipse(brush, e.X, e.Y, 10, 10);
textBox1.Text = ++i + "个点";
}
}
}
}