C#作业样本⑺-- GUI Ⅲ

1 view
Skip to first unread message

bluefin

unread,
Jun 6, 2008, 4:43:49 PM6/6/08
to C# Guide
(注:以下教程适合初学者,介绍C#。下载.NET Framework 2.0 版本到你的Windows
2000 SP4 或者Windows XP SP2。)

①打开notepad.exe

②输入以下内容:

using System;
using System.Windows.Forms;
using System.Drawing;

public class Menus : Form
{
private Label myDateLabel;
private MainMenu myMainMenu;

public Menus()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.Text = "STY Menus";
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.Fixed3D;

myDateLabel = new Label(); // 建立标签

DateTime currDate = new DateTime();
currDate = DateTime.Now;
myDateLabel.Text = currDate.ToString(); //系统时钟

myDateLabel.AutoSize = true;
myDateLabel.Location = new Point( 50, 70); //窗口位置
myDateLabel.BackColor = this.BackColor;

this.Controls.Add(myDateLabel); // 置于标签

// 设定标签长短
this.Width = (myDateLabel.PreferredWidth + 100);

CreateMyMenu(); // 创建菜单
}

protected void MenuUpdate_Selection( object sender,
System.EventArgs
e)
{
DateTime currDate = new DateTime();
currDate = DateTime.Now;
this.myDateLabel.Text = currDate.ToString();
}

protected void FileExit_Selection( object sender, System.EventArgs
e)
{
this.Close();
}

protected void FileAbout_Selection( object sender, System.EventArgs
e)
{
// 显示About 框
MessageBox.Show("Have a nice day!","About...");
}

public void CreateMyMenu()
{
myMainMenu = new MainMenu(); // 建立菜单

MenuItem menuitemFile = myMainMenu.MenuItems.Add("&File");
menuitemFile.MenuItems.Add(new MenuItem("Update &Date",
new
EventHandler(this.MenuUpdate_Selection),

Shortcut.CtrlD));
menuitemFile.MenuItems.Add(new MenuItem("E&xit",
new
EventHandler(this.FileExit_Selection),
Shortcut.CtrlX));

MenuItem menuitemHelp = myMainMenu.MenuItems.Add("&Help");
menuitemHelp.MenuItems.Add(new MenuItem("&About",
new
EventHandler(this.FileAbout_Selection)))
;

this.Menu = myMainMenu;
}

public static void Main( string[] args )
{
Application.Run( new Menus() );
}
}

③储存名档为Menus.cs

④设计一个Image 名为App.ico,根据以下Property value:
Width/Height = 32/32 pixels
Resolution (Horz/Vert) = 96/96 dpi
Bith depth= 32
Frame Count= 1

⑤进入"C:\Windows\Microsoft.NET\Framework\v2.0.50727" 里头,执行csc.exe
如下:

csc.exe /t:winexe /win32icon:App.ico /nologo Menus.cs [ 回车键 ]

这时它再也不会显示以下讯息了:

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

⑥输入dir Menus.*

这时它会显示以下讯息:

Menus.cs
Menus.exe

注:若你见不到Menus.exe, 你一定要先除错Menus.cs,一直得到为止。

⑥输入Menus.exe

哗!期盼我的朋友也学会,太棒!
Reply all
Reply to author
Forward
0 new messages