C#学习笔记——WinForm开发
一、WinForm应用程序
- WinForm应用程序时一种智能客户端技术,我们可以使用winform应用程序帮助我们获取信息或者传输信息等。
1、VS创建WinForm桌面应用程序
1>新建窗体
2>添加新的窗体
- 注意:
再Main函数当中创建的窗体对象,我们称之为这个窗体应用程序的主窗体。也就意味着,当你将主窗体关闭后,整个应用程序就关闭了。
3>窗体
- 窗体的左上角为坐标原点,水平向右为X轴正方向,竖直向下为Y轴正方向
2、VS各图标表示的含义
3、VS各种细节
- 快速切换到前台
- 解决方案下程序分类
- 拖动控件创建新对象
- 快速进入控件属性
二、控件
1、Button
1>属性
- Name:在后台要获得前台的控件对象,需要使用Name属性
- Anchor:放大缩小时固定哪端(默认上、左)
- 背景
- 右键显示
1、先找到菜单与工具栏的ContextMenuStrip,拖到前台界面,在控件中输入信息
2、在属性中找到右键菜单栏,选择编辑好的添加到控件中
- 鼠标
- visible:指示一个控件是否可见
- Enable:指示一个控件是否可用
2>事件
- Button的默认事件为Click
- 注册事件:
1、方式一:在属性栏中找到事件、然后再找到你想要完成的事件(以点击事件为例),双击此事件,则会自动进入后台。
2、方式2:若向注册控件的默认事件,只需双击控件,即可直接进入后台(仅默认事件)
- 触发事件:
2、Label
- 文本标签,一般配合TextBox使用
3、TextBox
- 文本框,可单行和多行输入
1>属性
- WordWarp:指示文本框是否自动换行(默认为True)
- ScrollBars:滚动条
- PassWordChar:将任意输入的文本按你规定的字符显示输出
2>事件
- TestBox的默认事件是TextChanged
当文本框中的内容发生改变的时候触发这个事件(可配合Label使用)
4、Timer
- 计时器:通常用于在规定时间干指定的事
1>属性
- Interval:在指定的时间内发生一次指定事件(默认以毫秒为单位)
5、RadioButton
- 单选按钮,一般结合容器(GroupBox)使用
1>属性
- Checked:指示这个控件是否被选中(默认false,不被选中)
- 提示:在一个界面中如果有多个RadioButton,如果没有进行分组,那么程运行时这些按钮都将被分为一个组,此时,可以借助GroupBox来将不同的按钮进行分组
6、GroupBox
- 容器:用来对控件进行分组管理(将需要分组的控件拖到相应的容器即可)
7、CheckBox
- 多选按钮
1>属性
- Checked:指示这个控件是否被选中(默认false,不被选中)
8、Picture Box
1>属性
- SizeMode:更改导入的图片在框中的位置
- 选择图像:在控件的右上角有个三角形,点击就可以看到选择图像和更改图片位置两个选项,当然也可以直接在属性栏中导入和修改
三、MDI窗体设计
- 步骤:
1、首先创建多个窗体,然后选择一个窗体,将其属性中的IsMdiContainer设置为true
2、添加菜单栏,并为每个菜单栏添加事件
3、创建子窗体,并设置它们的父窗体
四、案例代码
1、记事本登录
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 记事本
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 程序加载的时候
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
btnWordWrap.Visible = false;
btnSave.Visible = false;
txtWords.Visible = false;
//取消文本框的自动换行
txtWords.WordWrap = false;
}
/// <summary>
/// 点击登录的时候
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogin_Click(object sender, EventArgs e)
{
//首先获得用户输入的用户名
string strName = txtUser.Text;
//再获得用户输入的密码
string strPwd = txtPwd.Text;
if(strName =="admin" && strPwd =="admin")
{
MessageBox.Show("登陆成功");
//登录成功后,让两个文本框、两个标签和登录按钮都不可见
UserName.Visible = false;
UserPwd.Visible = false;
txtUser.Visible = false;
txtPwd.Visible = false;
btnLogin.Visible = false;
//换行按钮、保存按钮和文本框出现
btnWordWrap.Visible = true;
btnSave.Visible = true;
txtWords.Visible = true;
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入");
//如果登陆失败,清空两个文本框的内容
txtUser.Clear();
txtPwd.Clear();
//让文本框对象获得焦点
txtUser.Focus();
}
}
/// <summary>
/// 自动换行的点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnWordWrap_Click(object sender, EventArgs e)
{
if(btnWordWrap.Text=="自动换行")
{
txtWords.WordWrap = true;
btnWordWrap.Text = "取消自动换行";
}
else// if(btnWordWrap.Text=="取消自动换行")
{
txtWords.WordWrap = false;
btnWordWrap.Text = "自动换行";
}
}
private void btnSave_Click(object sender, EventArgs e)
{
using(FileStream fsWrite = new FileStream(@"C:\Users\Administrator\Desktop\3.txt",FileMode.OpenOrCreate,FileAccess.Write))
{
string str = txtWords.Text;
byte[] buffer = Encoding.Default.GetBytes(str);
fsWrite.Write(buffer, 0, buffer.Length);
MessageBox.Show("保存成功");
}
}
}
}
2、爱与不爱
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;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 当鼠标进入按钮的可见部分的时候, 给控件一个新的坐标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_MouseEnter(object sender, EventArgs e)
{
//给按钮一个新的坐标
//给按钮获得的最大宽度就是 窗体的快读减去按钮的宽度
int x = this.ClientSize.Width - btUnlove.Width;
int y = this.ClientSize.Height - btUnlove.Height;
Random r = new Random();
//要给按钮一个随机的坐标
btUnlove.Location = new Point(r.Next(0, x+1), r.Next(0, y+1));
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("我也爱你哦思密达~");
this.Close();//关闭主窗体
}
private void btUnlove_Click(object sender, EventArgs e)
{
MessageBox.Show("真讨厌!还是被你给点到了~");
this.Close();
}
}
}
3、跑马灯+闹钟
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Timer_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
//MessageBox.Show("你的电脑中病毒啦~");
lalTxt.Text = lalTxt.Text.Substring(1) + lalTxt.Text.Substring(0, 1);
}
/// <summary>
/// 每隔一秒就把当前的时间赋值给label
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer2_Tick(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString();
//18:00 播放音乐
if(DateTime.Now.Hour==18&&DateTime.Now.Minute==0)
{
//播放音乐
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @"C:\Users\Administrator\Desktop\1.wav";
sp.Play();
}
}
private void Form1_Load(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString();
}
}
}
4、老师学生登录
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;
namespace RedioButtob
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 点击登录的时候进行判断
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bntLogin_Click(object sender, EventArgs e)
{
if(rbStu.Checked==true)
{
//用户名去空格
if(txtName.Text.Trim()=="student"&&txtPwd.Text=="student")
{
MessageBox.Show("登录成功");
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入");
txtName.Clear();
txtPwd.Clear();
txtName.Focus();
}
}
else
{
if (txtName.Text.Trim() == "teacher" && txtPwd.Text == "teacher")
{
MessageBox.Show("登录成功");
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入");
txtName.Clear();
txtPwd.Clear();
txtName.Focus();
}
}
}
}
}
5、MDI窗体设计
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;
namespace 抖动
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 显示子窗体ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();//创建子窗体
frm2.MdiParent = this;//选择父窗体
frm2.Show();
Form3 frm3 = new Form3();
frm3.MdiParent = this;
frm3.Show();
Form4 frm4 = new Form4();
frm4.MdiParent = this;
frm4.Show();
}
private void 横向排列ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileHorizontal);
}
private void 纵向排列ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileVertical);
}
}
}
6、图片翻页
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 PictureBox实例
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//获取整个目录下的文件名
string[] strPath = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Picture");
//定义图片索引
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
//改变图片显示格式
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//导入图片
pictureBox1.Image = Image.FromFile(strPath[0]);
}
/// <summary>
/// 点击更换下一张
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
i++;
if (i == strPath.Length)
i = 0;
pictureBox1.Image = Image.FromFile(strPath[i]);
}
/// <summary>
/// 点击更换上一张
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
i--;
if (i < 0)
i = strPath.Length-1;
pictureBox1.Image = Image.FromFile(strPath[i]);
}
}
}