.net(四) winform应用程序

winform应用程序是一种智能客户端技术,可以使用其来帮助我们获得信息或传输信息。

XAML

VS中新建窗体应用程序

 程序入口

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form3());
        }
    }
}

常用控件属性
Name :表示一个控件或者窗体的名称,在后台获得前台的控件对象,需要使用Name。

Text:控件上显示的字体。

ContextMenuScripts:右键菜单,在工具箱中找到该窗体进行设置,然后在控件属性中进行绑定。

Cursor:光标样式,如小圆圈的等待。

Visibale:是否可见。

Enabled:是否启用控件。

Anchor:的英文意思是锚定。表示在窗体重置时控件与窗体(或者父控件)的相对位置保持不变。控件变化要等到窗体重置的时候才能呈现。就是窗体拉大后控件会随这窗体的4个方向随之变化,这个窗体拉大是指没运行时对form1窗体的拉大。关键字:相对位置不变。

Dock则是停泊的意思,表示控件的某个边与窗体重合(零距离)。控件的变化则在设计的时候就能呈现。此外控件的DocK循序会影像到结果。就是表示该控件在窗体的那个位置的停靠,fill表示充满窗体或者容器。关键字:零距离。

Dock属性也就能很好的解释我什么我们的菜单栏(menustrip)加载的时候自动停靠在顶部而状态栏(statustrip)自动停靠在底部

Items属性:这个属性就是Item集合,该集合说明了总共有多少个项。

事件:发生的一件事情,我们可以决定在发生该事情做些什么。

注册事件

触发事件

 button控件

 双击Click进入Click事件的代码

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 WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World!");
        }
    }
}

结果

object sender表示触发事件的对象,这里是Button对象

EventArgs e表示执行事件所需资源

可以添加多个窗体,在Main函数中创建的窗体对象,称之为窗体应用程序的主窗体。主窗体关闭,整个应用程序关闭。

拖两个按钮到窗体

 设置按钮的Text属性,修改文本。选择事件,双击事件,进入代码

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 WindowsFormsApp1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("俺也爱你");
            this.Close(); // this指当前主窗体
        }

        // 当鼠标进入按钮的可见部分的时候,给按钮一个新的坐标
        // 移动的宽度是窗体宽度减去按钮宽度,移动的高度是窗体高度减按钮高度
        // 左上角是(0, 0),向下向右坐标递增
        private void button2_MouseEnter(object sender, EventArgs e)
        {
            int x = this.ClientSize.Width - button2.Width; // 窗体工作区大小 ClientSize
            int y = this.ClientSize.Height - button2.Height;
            Random r = new Random();
            button2.Location = new Point(r.Next(0, x+1), r.Next(0, y+1));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("扎心了,老铁");
            this.Close();
        }
    }
}

TextBox控件

点击小黑三角,选择MultiLine后可以上下扩大文本框

默认自动换行 WordWrap属性为True

ScrollBars 滚动条,可以选择滚动条方向

PasswordChar 让文本框显示一个单一的字符

默认事件 TextChanged 文本改变的时候触发

Label控件 显示文本

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 WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        // 当文本框中的内容改变的时候,赋值给label
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label1.Text = textBox1.Text;
        }
    }
}

Timer

在指定的时间间隔内做一件指定的事情。

跑马灯练习

来一个Label控件,文本为 ✞✛✜✝✙✠✚†‡◉○◌◍◎●◐◑◒◓◔◕◖◗❂☢⊗⊙◘◙

添加Timer控件 Enable属性设为True

取字符串的第一个字符放到字符串最后面,速度够快,就形成了跑马灯的效果

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 WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World!");
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0, 1);
        }
    }
}

显示时间

每隔一秒就把当前的时间赋给label

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.Media;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World!");
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0, 1);
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            label2.Text = DateTime.Now.ToString();
            // 15:32播放音乐
            if (DateTime.Now.Hour == 15 && DateTime.Now.Minute == 32 && DateTime.Now.Second == 50)
            {
                // 播放音乐
                SoundPlayer sp = new SoundPlayer();
                sp.SoundLocation = @"C:\Users\SpringRain\DeskTop\1.wav";
                sp.Play();
            }
        }
    }
}

多选和单选控件

容器 GroupBox

checkBox

radioButton 窗体里只会有一个被选中,除非放到 GroupBox 中

checked属性 是否选中

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 WindowsFormsApp1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(student.Checked || teacher.Checked)
            {
                if (student.Checked)
                {
                    string name = usertextBox.Text;
                    string pass = passtextBox.Text;
                    if(name == "student" && pass == "student")
                    {
                        MessageBox.Show("登录成功");
                    }
                    else
                    {
                        MessageBox.Show("登录失败");
                        usertextBox.Clear();
                        passtextBox.Clear();
                        usertextBox.Focus();
                    }
                }
                if (teacher.Checked)
                {
                    string name = usertextBox.Text;
                    string pass = passtextBox.Text;
                    if (name == "teacher" && pass == "teacher")
                    {
                        MessageBox.Show("登录成功");
                    }
                    else
                    {
                        MessageBox.Show("登录失败");
                        usertextBox.Clear();
                        passtextBox.Clear();
                        usertextBox.Focus();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择您的身份!");
            }
        }
    }
}

MDI窗体

  • 首先确定父窗体,IsMdiContainer属性
  • 创建子窗体,设置它们的父窗体,MdiParent属性

添加菜单栏 MenuStrip

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 WindowsFormsApp1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void 显示子窗体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 
            Form6 form6 = new Form6();
            form6.MdiParent = this;
            form6.Show();
            Form7 form7 = new Form7();
            form7.MdiParent = this;
            form7.Show();
            Form8 form8 = new Form8();
            form8.MdiParent = this;
            form8.Show();
        }

        private void 横向排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal); 
        }

        private void 纵向排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }
    }
}

图片的上一张和下一张

PictureBox

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 WindowsFormsApp1
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }


        private void Form6_Load(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = Image.FromFile(@"C:\Users\Documents\HBuilderProjects\HelloWorld\img\1.jpg");   
        }

        string[] path = Directory.GetFiles(@"C:\Users\Documents\HBuilderProjects\HelloWorld\img");
        int i;

        private void button1_Click(object sender, EventArgs e)
        {
            i --;
            if (i < 0 )
            {
                i = path.Length - 1;
            }
            pictureBox1.Image = Image.FromFile(path[i]);
        }
    

        private void button2_Click(object sender, EventArgs e)
        {
            i ++;
            if(i == path.Length)
            {
                i = 0;
            }
            pictureBox1.Image = Image.FromFile(path[i]);
        }
    }
}

线程

创建线程,Test为线程要执行的方法的名字

Thread thread = new Thread(Test);
thread.start();

start只标记线程可以被CPU执行,但具体执行时间由CPU决定。

前台线程:只有所有的前台线程都关闭才能完成程序关闭。

后台线程:只要所有的前台线程结束,后台线程自动结束。

设为后台线程

thread.IsBackground = true;

在.Net下不允许跨线程访问,可以关闭检查

Control.CheckForIllegalCrossThreadCalls = false;

3281

猜你喜欢

转载自www.cnblogs.com/aidata/p/12324471.html