一、窗口设计Form1:

两个文件:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}

Form1.Designer.cs

namespace test1
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(112, 113);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(37, 15);
            this.label1.TabIndex = 0;
            this.label1.Text = "你好";
            this.label1.Click += new System.EventHandler(this.label1_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(282, 253);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

		}

        #endregion

        private System.Windows.Forms.Label label1;
    }
}

二、主程序(创建类,并显示):

MyForm11.cs

//csc MyForm11.cs Form1.cs Form1.Designer.cs
using test1;

[assembly: System.Reflection.AssemblyVersion("1.0")]
namespace MyForm11
{
  public class MyForm11
  {
    public static void Main()
    {
      System.Windows.Forms.Application.EnableVisualStyles();
      System.Windows.Forms.Application.Run(new Form1());
    }
  }
}

三、编译运行:

编译环境:

cmd里运行(vs2017为例):

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat

或将vs2017的开发人员命令提示符固定在任务栏里直接打开,然后进入到源代码目录:

编译运行:

D:\prg\csharp\WinFormsAction\test1>csc MyForm11.cs Form1.cs Form1.Designer.cs
Microsoft(R) Visual C# 编译器 版本 2.10.0.0 (b9fb1610)
版权所有(C) Microsoft Corporation。保留所有权利。


D:\prg\csharp\WinFormsAction\test1>MyForm11.exe

运行截图:

C#图形界面WinForm 入门(HelloWorld)-LMLPHP

12-02 11:57