浅谈Winform

一、Winform简介说明

C# 是一种面向对象的编程语言,由微软开发并作为.NET框架的主要编程语言。C# 设计时考虑了易用性,并且具有丰富的特性,如垃圾回收、异常处理、泛型、LINQ(Language Integrated Query)、异步编程等。C# 语言随着.NET框架的更新而不断发展,最新的版本通常与.NET Core或.NET 5/6等跨平台.NET实现保持一致。

二、Winform的特性

  1. 丰富的控件集:Winform提供了大量的预制控件,如按钮、文本框、标签、列表框等。

  2. 拖拽设计:开发者可以使用Visual Studio等集成开发环境(IDE)的拖拽功能来设计用户界面。

  3. 事件驱动:Winform应用程序是基于事件驱动的,这意味着应用程序的逻辑是基于用户操作(如点击按钮)触发的事件。

  4. 数据绑定:Winform支持数据绑定,允许控件与数据源直接绑定,简化数据展示和更新的逻辑。

  5. 本地化支持:Winform应用程序可以支持多语言,适应全球化市场的需求。

  6. MDI(多文档界面)支持:Winform支持创建MDI应用程序,允许在一个应用程序窗口中打开多个文档。

  7. 打印支持:Winform提供了打印支持,使得开发者可以轻松地将数据打印到纸张上。

  8. 易于部署:Winform应用程序通常以单个可执行文件的形式部署,简化了安装和更新过程。

  9. 集成的调试和诊断工具:Visual Studio提供了强大的调试工具,帮助开发者快速定位和解决问题。

三、Winform的简单代码示例

首先是C#代码,保存在Form1.cs文件中:

cs 复制代码
using System;
using System.Windows.Forms;

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

        private void buttonShowMessage_Click(object sender, EventArgs e)
        {
            // 检查文本框是否为空
            if (string.IsNullOrEmpty(textBoxInput.Text))
            {
                MessageBox.Show("Please enter some text.", "Input Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                // 显示输入的文本
                MessageBox.Show("You entered: " + textBoxInput.Text, "Message");
            }
        }
    }
}

然后是设计器生成的代码,通常保存在Form1.Designer.cs文件中:

cs 复制代码
partial class Form1
{
    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        // 注释掉的代码是设计器自动生成的控件声明和布局设置
        // this.components = new System.ComponentModel.Container();
        this.textBoxInput = new System.Windows.Forms.TextBox();
        this.buttonShowMessage = new System.Windows.Forms.Button();

        // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        // this.ClientSize = new System.Drawing.Size(800, 450);
        // this.Text = "Form1";

        this.textBoxInput.Location = new System.Drawing.Point(12, 12);
        this.textBoxInput.Name = "textBoxInput";
        this.textBoxInput.Size = new System.Drawing.Size(100, 20);
        this.textBoxInput.TabIndex = 0;

        this.buttonShowMessage.Location = new System.Drawing.Point(118, 10);
        this.buttonShowMessage.Name = "buttonShowMessage";
        this.buttonShowMessage.Size = new System.Drawing.Size(75, 23);
        this.buttonShowMessage.TabIndex = 1;
        this.buttonShowMessage.Text = "Show Message";
        this.buttonShowMessage.UseVisualStyleBackColor = true;
        this.buttonShowMessage.Click += new System.EventHandler(this.buttonShowMessage_Click);

        // this.Controls.Add(this.buttonShowMessage);
        // this.Controls.Add(this.textBoxInput);

        // this.Name = "Form1";
        // this.Text = "Simple WinForms App";
    }
}

在这个示例中:

  • Form1 类继承自 Form,是WinForms应用程序的主窗口。
  • textBoxInput 是一个文本框控件,允许用户输入文本。
  • buttonShowMessage 是一个按钮控件,当点击时会触发 buttonShowMessage_Click 事件处理程序。
  • buttonShowMessage_Click 方法检查文本框是否为空,如果不为空,则使用 MessageBox.Show 显示用户输入的文本。

四、Winform注意事项

Winform是开发传统Windows桌面应用程序的有力工具,尤其适合需要快速开发和部署的商业和企业级应用。随着技术的发展,虽然WPF等新技术提供了更丰富的UI设计能力,但Winform依然在许多场景下保持着其优势和应用价值。

相关推荐
__water4 小时前
『功能项目』回调函数处理死亡【54】
c#·回调函数·unity引擎
__water4 小时前
『功能项目』眩晕图标显示【52】
c#·unity引擎·动画事件
__water4 小时前
『功能项目』第二职业法师的平A【57】
c#·unity引擎·魔法球伤害传递
__water7 小时前
『功能项目』战士的伤害型技能【45】
c#·unity引擎·战士职业伤害型技能
君莫愁。8 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎
Lingbug8 小时前
.Net日志组件之NLog的使用和配置
后端·c#·.net·.netcore
咩咩觉主8 小时前
Unity实战案例全解析:PVZ 植物卡片状态分析
unity·c#·游戏引擎
Echo_Lee08 小时前
C#与Python脚本使用共享内存通信
开发语言·python·c#
__water16 小时前
『功能项目』QFrameWork框架重构OnGUI【63】
c#·unity引擎·重构背包框架
Crazy Struggle16 小时前
C# + WPF 音频播放器 界面优雅,体验良好
c#·wpf·音频播放器·本地播放器