平時我們編寫WinForm程序經常使用VS進行拖控件的方式,這樣做雖然簡單,但是無法深入了解WinForm程序的本質。其實,用記事本也可以編寫出VS編寫的WinForm程序。還是直接看代碼吧:
1、打開記事本,寫入以下代碼,另存為hello.cs文件
using System;
using System.Windows.Forms;
namespace Hello
{
public class Form1:Form
{
private System.Windows.Forms.Button btnClose;
public Form1()
{
this.Text = "Form1窗體";
btnClose = new System.Windows.Forms.Button();
//將窗體掛起
this.SuspendLayout();
//設置按鈕屬性
this.btnClose.Location = new System.Drawing.Point(20,20);
this.btnClose.Size = new System.Drawing.Size(100,25);
this.btnClose.Name = "btnClose";
this.btnClose.Text = "按鈕";
this.btnClose.UseVisualStyleBackColor = true;
//設置按鈕控件點擊事件
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//將構造的控件添加到窗體Controls控件集合
this.Controls.Add(btnClose);
}
//按鈕點擊事件
private void btnClose_Click(object sender,EventArgs e)
{
this.Close();
}
}
public class Program
{
//程序入口
public static void Main()
{
Application.Run(new Form1());
}
}
}
2、在Windows搜索框輸入 cmd,打開控制臺,輸入以下代碼切換目錄
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
3、目錄切換完畢后,輸入以下代碼運行
csc.exe /out:e:\hello.exe e:\hello.cs
/out:e:\hello.exe用于指定可執行文件存放的目錄和名稱
e:\hello.cs用于指定源文件的文件路徑


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。