C#WinForm窗口程序控件
提示:这里只是对较难和常用控件进行展示,其他可上网搜索
代码 (包含小项目)
控件
对话框
- 打开文件
private void openBtn_Click(object sender, EventArgs e)
{
//文本对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
//设置标题
openFileDialog.Title = "这是文本对话框";
//设置文件可多选
openFileDialog.Multiselect = true;
//设置默认打开的文件对话框路径
openFileDialog.InitialDirectory = @"E:\测试";
//设置文件可选的类型
openFileDialog.Filter = @"文本文件|*.txt|视频文件|*.mp4|图片文件|*.jpg|所有文件|*.*";
//显示对话框
openFileDialog.ShowDialog();
//获取选择的文件名
string fileNamePath = openFileDialog.FileName;
if (fileNamePath == "")
{
return;
}
//读取文件,显示在textBox中
using (FileStream fileStream = new FileStream(fileNamePath,FileMode.Open, FileAccess.Read))
{
byte[] buff = new byte[1024*1024*5];
int r = fileStream.Read(buff, 0, buff.Length);
textBox.Text = Encoding.UTF8.GetString(buff, 0, r);
}
}
- 保存文件
private void saveBtn_Click(object sender, EventArgs e)
{
//文本对话框
SaveFileDialog openFileDialog = new SaveFileDialog();
//设置标题
openFileDialog.Title = "这是文本对话框";
//设置默认打开的文件对话框路径
openFileDialog.InitialDirectory = @"E:\测试";
//设置文件可选的类型
openFileDialog.Filter = @"文本文件|*.txt|视频文件|*.mp4|图片文件|*.jpg|所有文件|*.*";
//显示对话框
openFileDialog.ShowDialog();
//保存,这里的名字是你输入的名字
string fileNamePath = openFileDialog.FileName;
if (fileNamePath == "")
{
return;
}
//读取文件,显示在textBox中
using (FileStream fileStream = new FileStream(fileNamePath, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] msgBuff = Encoding.UTF8.GetBytes(textBox.Text);
fileStream.Write(msgBuff, 0, msgBuff.Length);
}
MessageBox.Show("保存成功");
}
字体颜色对话框
private void textBtn_Click(object sender, EventArgs e)
{
//字体对话框
FontDialog fontDialog = new FontDialog();
fontDialog.ShowDialog();
textBox.Font = fontDialog.Font;
}
private void colorBtn_Click(object sender, EventArgs e)
{
//颜色对话框
ColorDialog colorDialog = new ColorDialog();
colorDialog.ShowDialog();
textBox.ForeColor = colorDialog.Color;
}
多线程
Thead
public Form1()
{
InitializeComponent();
}
Thread thread = null;
//按钮点击事件
private void threadBtn_Click(object sender, EventArgs e)
{
//创建线程
thread = new Thread(test);
//设置后台进程,前台进程结束,后天进程也会结束
thread.IsBackground = true;
thread.Start();
}
public void test()
{
for(int i = 0; i < 10000; i++)
{
//这里是自己开辟的线程运行,当运行窗口的时候是不允许跨线程访问,需要设置
textBox1.Text = i.ToString() ;
}
}
//窗体加载事件
private void Form1_Load(object sender, EventArgs e)
{
//设置允许跨线程访问
Control.CheckForIllegalCrossThreadCalls = false;
}
但是虽然允许跨线程访问,当我们点击窗体关闭时,由于textbox控件不在,而thread线程又用到了控件,所以会导致报错,因此我们可以添加窗体关闭事件来关闭线程
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//窗口关闭的时候,关闭线程
if (thread != null)
{
thread.Abort();
}
}
线程执行的函数如果有参数的话,参数的类型必须是object类型,然后在Start方法中传入参数即可
Process
这个是和进程相关的类,可以通过进程去打开指定的文件
摇号抽奖机
- 地址(gitee):7_23_Project 10摇号抽奖机
GDI绘制
绘制直线
由于窗体每次移动,操作系统都是重新删掉窗体重画一次,而GDI不会,所以需要在窗体重画事件中重画
bool flag = false;
//画直线按钮
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();//创建图纸
//创建笔,设置颜色
Pen pen = new Pen(Brushes.Red);
//创建两个点
Point p1 = new Point(11, 22);
Point p2 = new Point(250, 250);
g.DrawLine(pen, p1, p2);
flag = true;
}
int i = 0;
//由于窗体每次移动,操作系统都是重新删掉窗体重画一次,而GDI不会,所以需要在窗体重画事件中重画
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (flag)
{
label1.Text ="重画次数:"+ i++.ToString();
Graphics g = this.CreateGraphics();//创建图纸
//创建笔,设置颜色
Pen pen = new Pen(Brushes.Red);
//创建两个点
Point p1 = new Point(11, 22);
Point p2 = new Point(250, 250);
g.DrawLine(pen, p1, p2);
}
}
画其他自己摸索
画矩形
//画矩形
private void button2_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();//创建图纸
Pen pen = new Pen(Brushes.Red);
Size size = new System.Drawing.Size(180, 170);//矩形尺寸
Rectangle rectangle = new Rectangle(new Point(50,50), size);//矩形
g.DrawRectangle(pen, rectangle);
}
画验证码
- 地址(gitee):7_23_Project 12画验证码
//点击刷新验证码
private void pictureBox1_Click(object sender, EventArgs e)
{
//生成字符串数字
Random random = new Random();
string codeStr = "";
for(int i = 0; i < 5; i++)
{
codeStr += random.Next(0, 10);
}
//创建位图画布
Bitmap bitmap = new Bitmap(150, 40);//位图尺寸
Graphics g = Graphics.FromImage(bitmap);
//画数字
for(int i = 0; i < 5; i++)
{
//生成位置
Point point = new Point(i * 20, 0);//相对于bitmap的位置
//随机颜色
Color[] colors = { Color.Yellow, Color.Green, Color.Black, Color.Red };
//随机字体
string[] fonts = { "微软雅黑","宋体","黑体","隶书","仿宋" };
//画字体
g.DrawString(codeStr[i].ToString(), new Font(fonts[random.Next(0, 5)]
,15,FontStyle.Italic),new SolidBrush(colors[random.Next(0,4)]),point);
}
//画线条
for(int i = 0; i < 20; i++)
{
//随机设置起点终点,但不超过位图
Point point1 = new Point(random.Next(0, bitmap.Width)
, random.Next(0, bitmap.Height));
Point point2 = new Point(random.Next(0, bitmap.Width)
, random.Next(0, bitmap.Height));
g.DrawLine(new Pen(Brushes.Green), point1, point2);
}
//添加像素颗粒
for(int i = 0; i < 500; i++)
{
Point point = new Point(random.Next(0, bitmap.Width)
, random.Next(0, bitmap.Height));
bitmap.SetPixel(point.X, point.Y, Color.Black);
}
//将位图画布赋值给pictureBox1
pictureBox1.Image = bitmap;
}