使用文本编辑器创建简单的 C WinForms 应用程序
-
打开文本编辑器(如记事本),然后键入以下代码:
using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace SampleApp { public class MainForm : Form { private Button btnHello; // The form's constructor: this initializes the form and its controls. public MainForm() { // Set the form's caption, which will appear in the title bar. this.Text = "MainForm"; // Create a button control and set its properties. btnHello = new Button(); btnHello.Location = new Point(89, 12); btnHello.Name = "btnHello"; btnHello.Size = new Size(105, 30); btnHello.Text = "Say Hello"; // Wire up an event handler to the button's "Click" event // (see the code in the btnHello_Click function below). btnHello.Click += new EventHandler(btnHello_Click); // Add the button to the form's control collection, // so that it will appear on the form. this.Controls.Add(btnHello); } // When the button is clicked, display a message. private void btnHello_Click(object sender, EventArgs e) { MessageBox.Show("Hello, World!"); } // This is the main entry point for the application. // All C# applications have one and only one of these methods. [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new MainForm()); } } }
-
将文件保存到你具有读/写访问权限的路径。在包含它的类之后命名文件是常规的 - 例如,
X:\MainForm.cs
。 -
从命令行运行 C#编译器,将路径作为参数传递给代码文件:
%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\csc.exe /target:winexe "X:\MainForm.cs"
注意: 要将 C#编译器的一个版本用于其他 .NET 框架版本,请查看路径
%WINDIR%\Microsoft.NET
并相应地修改上面的示例。有关编译 C#应用程序的更多信息,请参阅编译并运行第一个 C#程序 。 -
编译完成后,将在与代码文件相同的目录中创建名为
MainForm.exe
的应用程序。你可以从命令行运行此应用程序,也可以在资源管理器中双击它。