C#基础入门第十四天(MD5加密,WinForm)

1、MD5加密
MD5加密是不能够逆的,只能将字符串转为MD5值,不能将MD5值转回字符串。小程序

static void Main(string[] args)
    {
        //202cb962ac59075b964b07152d234b70
        //202cb962ac5975b964b7152d234b70   ToString x参数
        //202cb962ac59075b964b07152d234b70  ToString x2参数

        // 3244185981728979115075721453575112   ToString  没加参数
        //ToString参数须要到百度拿来用
        string s = GetMD5("123");
        Console.WriteLine(s);
        Console.ReadKey();
    }

    public static string GetMD5(string str)
    {
        //建立MD5对象
        MD5 md5 = MD5.Create();
        //开始加密
        //须要将字符串转为字节数组
        byte[] buffer = Encoding.Default.GetBytes(str);
        //返回一个加密好的字节数组
       byte[] MD5Buffer = md5.ComputeHash(buffer);

        //将字节数组转换成字符串
        //字节数组---字符串
        //1.将字节数组中每一个元素按照自定的编码格式解析成字符串
        //2.直接将数组ToString();
        //3.将字节数组中的每一个元素ToString()
        //  return Encoding.Default.GetString(MD5Buffer);
        string strNew = " ";
        for (int i = 0; i < MD5Buffer.Length; i++)
        {
            //ToString("x") 加x参数将十进制转为十六进制,属于ToString的方法
            strNew += MD5Buffer[i].ToString("x2");
        }
        return strNew;
    }
}

2、Winform应用程序简介
是一种智能客户端技术,咱们可使用winform应用程序帮助咱们得到信息或者传输信息等。数组

属性
Name:在后台要得到前台的控件对象,须要使用Name属性
Anchor:控件位置固定
BackColor:控件背景(颜色,背景图等)
ContextMenuStrip:右键菜单 配合工具栏的 菜单和工具栏 内的ContextMenuStrip使用
Cursor:光标样子
Enabled:设置控件是否可用
Visible:设置控件是否可见
FlatStyle:设置控件外观
Font:字体dom

事件:发生一件事情。
注册事件:双击控件注册的都是控件默认被选中的那个事件
触发事件:点击后触发的事情ide

在Main函数当中建立的窗体对象,称之为这个窗体应用程序的主窗体。也就意味着,当主窗体被关闭时,整个应用程序都被关闭函数

经过静态类来共有
窗体1的按钮弹出窗体2,窗体2的按钮弹出窗体3,窗体3的按钮关闭全部窗体(实际窗体3就是调用的窗体1)工具

Form1 的代码以下布局

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.Show();
    }

    /// <summary>
    /// 当加载窗体的时候,将窗体对象放到Test类中的静态字段中
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Form1_Load(object sender, EventArgs e)
    {
      Test._fr1Test = this;
    }
}
}

窗体2的代码以下字体

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form3 frm3 = new Form3();
        frm3.Show();
    }
}

经过一个静态类来达到共有窗体1this

namespace WindowsFormsApplication1
{
public static class Test
{
    public static Form1 _fr1Test;
}
}

窗体3的代码以下编码

public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
    {
        //须要得到当前主窗体的对象
        Test._fr1Test.Close();
    }

    private void Form3_Load(object sender, EventArgs e)
    {

    }
}

鼠标点不到按钮,MouseEnter事件

代码以下

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

/// <summary>
    /// 当鼠标进入按钮的可见部分的时候,给按钮一个新的坐标
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void noLove_MouseEnter(object sender, EventArgs e)
    {
        //给按钮一个新的坐标
        //按钮活动的最大宽度就是 窗体的宽度减去按钮的宽度(按钮能活动的范围)
        int x = this.ClientSize.Width - bthLove.Width;
        int y = this.ClientSize.Height - bthLove.Height;

        Random r = new Random();
        //要给按钮一个随机的坐标
        noLove.Location = new Point(r.Next(0,x+1),r.Next(0,y+1));
    }

    private void bthLove_Click(object sender, EventArgs e)
    {
        MessageBox.Show("我也爱你哟,思密达");
        this.Close();
    }

    private void noLove_Click(object sender, EventArgs e)
    {
        MessageBox.Show("仍是被你这个屌丝点到了");
        this.Close();
    }
}

3、TextBox控件和Label控件(Label控件就是用来显示文本的)
属性
WoreWrap:换行
ScrollBars:是否显示滚动条
PasswordChar:密码框那个*号的东西
事件:TextChanged:当文本框中的内容改变时触发这个事件
代码示例以下

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

/// <summary>
    /// 当文本框中的内容发生改变的时候,将值赋值给Label
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void textWords_TextChanged(object sender, EventArgs e)
    {
        lbl.Text = textWords.Text;
    }
}

跑马灯练习(Timer的用法)
指定事件发生的频率,在指定的时间间隔内作一件指定的事情
代码以下

private void timer1_Tick(object sender, EventArgs e)
    {
        //截取字符串abcde
        //label1.Text拿到字符串,label1.Text.Substring(1)截取bcde
        //label1.Text.Substring(0, 1) 截取a,放到bcde后面
        label1.Text = label1.Text.Substring(1)+ label1.Text.Substring(0, 1);
    }

简单记事本(textBox)
所需属性
WordWrap:指示多行编辑控件是否自动换行
Visible:可见属性包含两个值
True使用此选项-文本框在父控件窗体上可见
False 使用此方法-文本框能够隐藏在父控件窗体上
TextBox.Text.Trim():接收用户输入,不能接收用户输入的空格(开头和结尾的空格)
TextBox.Focus :为文本框设置焦点

因为这个记事本有登录功能,都在一个FORM上,因此在登录时候须要隐藏掉一些按钮及须要隐藏的内容,为整个Form1设置Load属性

private void Form1_Load(object sender, EventArgs e)
    {
        //第一步:因为这个记事本有登录功能,都在一个FORM上,因此在登录时候须要隐藏掉一些按钮及须要隐藏的内容
        //txtWords.WordWrap = false;
        btnSave.Visible = false;
        btnWordWrap.Visible = false;
        txtWords.Visible = false;
    }

隐藏掉按钮以后,开始作登录判断的功能,为登录按钮注册设置事件

/// <summary>
    /// 登录按钮判断是否登录成功
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnLogin_Click(object sender, EventArgs e)
    {
        //接收用户输入的用户名和密码,不能接收用户输入的空格(开头和结尾的空格)
        string name = txtName.Text.Trim();
        string pwd = txtPwd.Text.Trim();
        //判断用户名和密码是否正确,正确或者错误该干的事
        if (name == "admin" && pwd == "admin")
        {
            MessageBox.Show("欢迎进入记事本");
            //若是成功将第二个界面隐藏了的该出现的放出来,第一界面该隐藏的隐藏掉
            txtWords.Visible = true;
            btnWordWrap.Visible = true;
            btnSave.Visible = true;

            label1.Visible = false;
            label2.Visible = false;
            txtName.Visible = false;
            txtPwd.Visible = false;
            btnLogin.Visible = false;
            btnRest.Visible = false;
        }
        else
        {
            //不成功则提示错误
            MessageBox.Show("用户名或密码错误,请从新输入!");
            //而后清空文本框内错误的内容
            txtName.Clear();
            txtPwd.Clear();
            //用户名文本框得到焦点
            txtName.Focus();
        }
    }

开始作重置按钮,重置按钮需触发清空两个文本框及焦点在name文本框的焦点

/// <summary>
    /// 为重置按钮注册事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnRest_Click(object sender, EventArgs e)
    {
        //重置按钮需触发清空两个文本框及焦点在name文本框的焦点
        txtName.Clear();
        txtPwd.Clear();
        txtName.Focus();
    }

如今第一个界面及登录、重置的功能完成,开始作第二界面换行切换,保存。

换行按钮注册事件

/// <summary>
    /// 自动换行
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnWordWrap_Click(object sender, EventArgs e)
    {
        //判断当前是否自动换行
        //txtWors.WordWrap = true;
        //取消自动换行
        if (btnWordWrap.Text == "自动换行")
        {
            //取消自动换行
            txtWords.WordWrap = true;
            btnWordWrap.Text = "取消自动换行";
        }
        else if (btnWordWrap.Text == "取消自动换行")
        {
            txtWords.WordWrap = false;
            btnWordWrap.Text = "自动换行";
        }
    }

保存文本到指定的路劲下,为保存按钮注册事件

/// <summary>
    /// 将文本保存到指定位置
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnSave_Click(object sender, EventArgs e)
    {
        using (FileStream fsWrite = new FileStream("new.txt", FileMode.OpenOrCreate, FileAccess.Write))
        {
            string str = txtWords.Text.Trim();
            byte[] buffer = System.Text.Encoding.Default.GetBytes(str);
            fsWrite.Write(buffer, 0, buffer.Length);
        }
        MessageBox.Show("保存成功");
    }

4、单选和多选
公共空间里面的:checkBox(多选) radioButton(单选)
checkBox:多选框Checked属性默认是否选中
radioButton:单选有一个问题,一个窗体中不管有多少单选只能选一个,这样若是一个窗体中既有性别选择,又有婚姻关系选择就不能实现。这时候最简单的办法就是给单选框分组。
分组:容器--GroupBox

单选和多选的登陆练习

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnLogon_Click(object sender, EventArgs e)
    {
        if (radStudent.Checked || radTeachar.Checked)
        {
            string name = textName.Text.Trim();
            string pwd = textPwd.Text.Trim();
            //判断是学生登陆仍是老师
            if (radStudent.Checked)
            {
                if (name == "student" && pwd == "student")
                {
                    MessageBox.Show("登陆成功");
                }
                else
                {
                    MessageBox.Show("登陆失败");
                    textName.Clear();
                    textPwd.Clear();
                    textName.Focus();
                }
            }
            else
            {
                if (name == "teachar" && pwd == "teachar")
                {
                    MessageBox.Show("登陆成功");
                }
                else
                {
                    MessageBox.Show("登陆失败");
                    textName.Clear();
                    textPwd.Clear();
                    textName.Focus();
                }
            }
        }
        else
        {
            MessageBox.Show("请先选学生或者老师");
        }
    }
}

5、MDI窗体的设计
frm4.MdiParent :标记子窗体属于父窗体
LayoutMdi():横纵向排列
一、首选肯定一个父窗体(新建父窗体)
二、新建须要的子窗体,能够在父窗体中排列(新建对应数量的子窗体)
三、标记父窗体,窗体属性:IsMdiContainer设置为true
四、在父窗口上放菜单栏:菜单和工具,MenuStrip
五、建立子窗体,而且设置他们的父窗体 frm4.MdiParent
六、针对子窗体作横向排列 LayoutMdi(MdiLayout.TileHorizontal);
七、针对子窗体作纵向排列 LayoutMdi(MdiLayout.TileVertical);

代码以下

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、图片(pictureBox)
属性
SizeMode:处理图像的位置
得到指定文件夹的全部文件的全路径
string[] path = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Poth");

上一张和下一张图片切换的练习

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
    {
        //设置图片显示布局
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        //初始显示的图片
        pictureBox1.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Poth\30536870.JPG");
    }
    int i = 0;
    //得到指定文件夹的全部文件的全路径
    string[] path = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Poth");
    /// <summary>
    /// 点击更换下一张图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button2_Click(object sender, EventArgs e)
    {
        i++;
        if (i == path.Length)
        {
            i = 0;
        }
        pictureBox1.Image = Image.FromFile(path[i]);
    }

    /// <summary>
    /// 上一张的图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {
        i--;
        if (i < 0)
        {
            i = path.Length - 1;
        }
        pictureBox1.Image = Image.FromFile(path[i]);
    }
}

pictureBox和Timer的小程序
实现多图自动切换,并播放音乐

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //播放音乐
        SoundPlayer sp = new SoundPlayer();
        sp.SoundLocation = @"C:\Users\Administrator\Desktop\Poth\fire4-1.wav";
        sp.Play();
        //设置图片显示的布局
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
        //窗体加载的时候给每个PictyreBox都加载一张图片
        pictureBox1.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Poth\30536870.JPG");
        pictureBox2.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Poth\30536870.JPG");
        pictureBox3.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Poth\30536870.JPG");
        pictureBox4.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Poth\30536870.JPG");
    }

    //拿到图片文件夹路径放到数组里面
    string[] path = System.IO.Directory.GetFiles(@"C:\Users\Administrator\Desktop\Poth");
    //图片起始的播放位置
    int i = 0;
    Random r = new Random();
    private void timer1_Tick(object sender, EventArgs e)
    {
        //让图片下一张播放
        i++;
        //判断图片文件夹里面的数组索引,超过了最大索引就将i变为第一张图片
        if (i == path.Length)
        {
            i = 0;
        }
        //给pictureBox赋值
        pictureBox1.Image = Image.FromFile(path[r.Next(0,path.Length)]);
        pictureBox2.Image = Image.FromFile(path[r.Next(0, path.Length)]);
        pictureBox3.Image = Image.FromFile(path[r.Next(0, path.Length)]);
        pictureBox4.Image = Image.FromFile(path[r.Next(0, path.Length)]);
    }
}
相关文章
相关标签/搜索