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

1、窗口设计Form1:ide

两个文件:字体

Form1.csui

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.csthis

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;
    }
}

2、主程序(建立类,并显示):spa

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());
    }
  }
}

3、编译运行:code

编译环境:component

cmd里运行(vs2017为例):orm

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

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

编译运行:

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

运行截图:

4、一个透明的WinForms

// csc /t:winexe secondWinForm.cs

using System ;
using System.Windows.Forms ;
using System.Drawing ;
public class Form2 : Form
{
	public static void Main( ){
	  Application.Run( new Form2( ) );
	}
	
	public Form2( ){
	  this.Location = new System.Drawing.Point( 100 , 100 ) ;
	  this.Cursor = System.Windows.Forms.Cursors.Hand;
	  // 定义在窗体上,光标显示为手形
	  this.Text = "透明的WinForm窗体!";
	  // 定义窗体的标题名称
	  this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
	  // 定义窗体的开始显示位置是屏幕的中间
	  this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
	  // 窗体的边界是Fixed3D类型
	  this.ForeColor = System.Drawing.SystemColors.Desktop;
	  //以桌面的前景色做为窗体的前景色
	  this.Font = new System.Drawing.Font ( "宋体", 9 ) ;
	  // 定义字体类型,大小
	  this.BackColor = System.Drawing.Color.Blue;
	  // 定义背景色为蓝色
	  this.ClientSize = new System.Drawing.Size( 440 , 170 ) ;
	  // 设置窗体的大小
	  // Opacity属性设立窗体的透明程度,只对于视窗2000有效
	  this.Opacity = 0.60 ;
  }
}
相关文章
相关标签/搜索