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 ;
  }
}
相關文章
相關標籤/搜索