C#Windows应用程序模板代码实现

开发 后端
C#Windows应用程序模板代码实现是如何的呢?C#Windows应用程序模板的每个版块的具体实现如何的呢?那么本文就向你介绍这方面的情况。

C#Windows应用程序开发之应用程序模板实现

/*   C#Windows应用程序模板代码实现
to compile this source file, type   
csc MyWinApp.cs   
*/   
 
using System;   
using System.Windows.Forms;   
using System.Drawing;   
using System.IO;   
using System.ComponentModel;   
 
public class MyWinApp: Form {   
 
Label label = new Label();   
Button button = new Button();   
TreeView tree = new TreeView();   
ImageList imageList = new ImageList();   
static String imageFolder = "Images" +   
 
Path.DirectorySeparatorChar.ToString();   
 
// --- Images declarations C#Windows应用程序模板代码实现-----   
Image newFileImage = new Bitmap(imageFolder + "newFile.bmp");   
Image openFileImage = new Bitmap(imageFolder + "openFile.gif");   
Image saveFileImage = new Bitmap(imageFolder + "saveFile.bmp");   
Image printImage = new Bitmap(imageFolder + "print.gif");   
 
// --C#Windows应用程序模板代码实现 End of Images declaration   
 
---- 
 
// -------------- menu -C#Windows应用程序模板------   
MainMenu mainMenu = new MainMenu();   
 
MenuItem fileMenuItem = new MenuItem();   
MenuItem fileNewMenuItem;   
MenuItem fileOpenMenuItem;   
MenuItem fileSaveMenuItem;   
MenuItem fileSaveAsMenuItem;   
MenuItem fileMenuWithSubmenu;   
MenuItem submenuMenuItem;   
MenuItem fileExitMenuItem;   
 
// -------------- End of menu C#Windows应用程序模板---------------------   
 
// -------------- Toolbar C#Windows应用程序模板-----------------   
ToolBar toolBar = new ToolBar();   
ToolBarButton separatorToolBarButton = new ToolBarButton();   
ToolBarButton newToolBarButton = new ToolBarButton();   
ToolBarButton openToolBarButton = new ToolBarButton();   
ToolBarButton saveToolBarButton = new ToolBarButton();   
ToolBarButton printToolBarButton = new ToolBarButton();   
 
// -------------- End of Toolbar --------------   
 
// -------------- StatusBar -C#Windows应用程序模板---------   
StatusBar statusBar = new StatusBar();   
 
StatusBarPanel statusBarPanel1 = new StatusBarPanel();   
StatusBarPanel statusBarPanel2 = new StatusBarPanel();   
 
// -------------- End of StatusBar ----------   
 
 
public MyWinApp() {   
InitializeComponent();   
}   
 
private void InitializeComponent() {   
this.Text = "My Windows Application";   
this.Icon = new Icon(imageFolder + "applicationLogo.ico");   
this.Width = 400;   
this.Height = 300;   
this.StartPosition = FormStartPosition.CenterScreen;   
 
imageList.Images.Add(newFileImage);   
imageList.Images.Add(openFileImage);   
imageList.Images.Add(saveFileImage);   
imageList.Images.Add(printImage);   
 
 
// menu   
fileMenuItem.Text = "&File";   
 
// the following constructor is the same as:   
// menuItem fileNewMenuItem = new MenuItem();   
// fileNewMenuItem.Text = "&New";   
// fileNewMenuItem.Shortcut = Shortcut.CtrlN;   
// fileNewMenuItem.Click += new   
 
System.EventHandler(this.fileNewMenuItem_Click);   
fileNewMenuItem = new MenuItem("&New",   
new System.EventHandler(this.fileNewMenuItem_Click), Shortcut.CtrlN);   
 
fileOpenMenuItem = new MenuItem("&Open",   
new System.EventHandler(this.fileOpenMenuItem_Click), Shortcut.CtrlO);   
 
fileSaveMenuItem = new MenuItem("&Save",   
new System.EventHandler(this.fileSaveMenuItem_Click), Shortcut.CtrlS);   
 
fileSaveAsMenuItem = new MenuItem("Save &As",   
new System.EventHandler(this.fileSaveAsMenuItem_Click));   
 
fileMenuWithSubmenu = new MenuItem("&With Submenu");   
 
submenuMenuItem = new MenuItem("Su&bmenu",   
new System.EventHandler(this.submenuMenuItem_Click));   
 
fileExitMenuItem = new MenuItem("E&xit",   
new System.EventHandler(this.fileExitMenuItem_Click));   
 
 
mainMenu.MenuItems.Add(fileMenuItem);   
fileOpenMenuItem.Checked = true;   
fileMenuItem.MenuItems.Add(fileNewMenuItem);   
fileMenuItem.MenuItems.Add(fileOpenMenuItem);   
fileMenuItem.MenuItems.Add(fileSaveMenuItem);   
fileMenuItem.MenuItems.Add(fileSaveAsMenuItem);   
fileMenuItem.MenuItems.Add(fileMenuWithSubmenu);   
fileMenuWithSubmenu.MenuItems.Add(submenuMenuItem);   
fileMenuItem.MenuItems.Add("-"); // add a separator   
fileMenuItem.MenuItems.Add(fileExitMenuItem);   
 
 
toolBar.Appearance = ToolBarAppearance.Normal;   
//toolBar.Appearance = ToolBarAppearance.Flat;   
toolBar.ImageList = imageList;   
toolBar.ButtonSize = new Size(14, 6);   
 
separatorToolBarButton.Style = ToolBarButtonStyle.Separator;   
newToolBarButton.ToolTipText = "New Document";   
newToolBarButton.ImageIndex = 0;   
openToolBarButton.ToolTipText = "Open Document";   
openToolBarButton.ImageIndex = 1;   
saveToolBarButton.ToolTipText = "Save";   
saveToolBarButton.ImageIndex = 2;   
printToolBarButton.ToolTipText = "Print";   
printToolBarButton.ImageIndex = 3;   
 
toolBar.ButtonClick += new   
 
ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);   
 
toolBar.Buttons.Add(separatorToolBarButton);   
toolBar.Buttons.Add(newToolBarButton);   
toolBar.Buttons.Add(openToolBarButton);   
toolBar.Buttons.Add(saveToolBarButton);   
toolBar.Buttons.Add(separatorToolBarButton);   
toolBar.Buttons.Add(printToolBarButton);   
 
tree.Top = 40;   
tree.Left = 20;   
tree.Width = 100;   
tree.Height = 100;   
 
label.Location = new Point(220, 40);   
label.Size = new Size(160, 30);   
label.Text = "Yes, click the button";   
 
button.Location = new Point(220, 80);   
button.Size = new Size(100, 30);   
button.Text = "Click this";   
button.Click += new System.EventHandler(this.button_Click);   
 
statusBarPanel1.BorderStyle = StatusBarPanelBorderStyle.Sunken;   
statusBarPanel1.Text = "Press F1 for Help";   
statusBarPanel1.AutoSize = StatusBarPanelAutoSize.Spring;   
statusBarPanel2.BorderStyle = StatusBarPanelBorderStyle.Raised;   
statusBarPanel2.ToolTipText = System.DateTime.Now.ToShortTimeString();   
statusBarPanel2.Text = System.DateTime.Today.ToLongDateString();   
statusBarPanel2.AutoSize = StatusBarPanelAutoSize.Contents;   
statusBar.ShowPanels = true;   
statusBar.Panels.Add(statusBarPanel1);   
statusBar.Panels.Add(statusBarPanel2);   
 
 
this.Menu = mainMenu;   
this.Controls.Add(toolBar);   
this.Controls.Add(tree);   
this.Controls.Add(label);   
this.Controls.Add(button);   
this.Controls.Add(statusBar);   
}   
 
 
// --- Event Handlers -C#Windows应用程序模板-------   
 
private void fileNewMenuItem_Click(Object sender, EventArgs e) {   
MessageBox.Show("You clicked the File -- New menu.", "The Event   
 
Information");   
}   
 
private void fileOpenMenuItem_Click(Object sender, EventArgs e) {   
MessageBox.Show("You clicked the File -- Open menu.", "The Event   
 
Information");   
}   
 
private void fileSaveMenuItem_Click(Object sender, EventArgs e) {   
MessageBox.Show("You clicked the File -- Save menu.", "The Event   
 
Information");   
}   
 
private void fileSaveAsMenuItem_Click(Object sender, EventArgs e) {   
MessageBox.Show("You clicked the File -- Save As menu.", "The Event   
 
Information");   
}   
 
private void fileExitMenuItem_Click(Object sender, EventArgs e) {   
MessageBox.Show("You clicked the File -- Exit As menu.", "The Event   
Information");   
}   
private void submenuMenuItem_Click(Object sender, EventArgs e) {   
MessageBox.Show("You clicked the submenu.""The Event Information");   
}   
 
protected void toolBar_ButtonClick(Object sender,   
 
ToolBarButtonClickEventArgs e) {   
 
// Evaluate the Button property to determine which button was clicked.   
switch (toolBar.Buttons.IndexOf(e.Button)) {   
case 1:   
MessageBox.Show("Second button.""The Event Information");   
break;   
case 2:   
MessageBox.Show("third button""The Event Information");   
break;   
case 3:   
MessageBox.Show("fourth button.""The Event Information");   
break;   
}   
}   
protected override void OnClosing(CancelEventArgs e) {   
MessageBox.Show("Exit now.""The Event Information");   
}   
private void button_Click(Object sender, System.EventArgs e) {   
MessageBox.Show("Thank you.""The Event Information");   
}   
 
// ---- End of Event Handlers -------   
 
public static void Main() {   
MyWinApp form = new MyWinApp();   
Application.Run(form);   
} }  //C#Windows应用程序模板
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.

C#Windows应用程序模板的代码实现就向你介绍到这里,希望对你学习和了解C#Windows应用程序模板有所帮助。

【编辑推荐】

  1. C#Windows应用程序开发之创建窗体
  2. C#Windows应用程序开发之窗体控件
  3. C#Windows应用程序开发之添加菜单
  4. C#Windows应用程序开发之添加状态条
  5. C#Windows应用程序开发之事件处理器
责任编辑:仲衡 来源: builder.com.cn
相关推荐

2009-08-14 17:36:20

C#Windows应用

2009-08-14 17:27:30

C#Windows应用

2009-08-14 17:55:52

C#Windows应用

2009-08-14 17:43:20

C#Windows应用

2009-08-14 18:00:22

C#Windows应用

2009-08-14 17:51:32

C#Windows应用

2009-08-14 14:25:09

Windows服务程序

2009-08-14 15:06:08

Windows服务程序

2009-08-14 15:47:18

C#Windows服务

2009-08-14 14:53:55

WINDOWS服务交互

2009-08-14 15:54:50

Windows服务程序C#Windows服务

2009-08-14 16:24:00

Windows服务程序

2009-08-14 14:45:03

C#Windows服务

2009-08-14 14:17:16

C#Windows服务

2009-08-14 16:48:39

C#Windows服务

2009-08-24 14:19:27

C# Windows应

2009-08-14 15:19:38

Windows服务程序Windows服务

2009-08-14 13:41:13

C#Windows服务

2009-08-14 10:50:09

Windows服务介绍

2009-08-24 13:40:58

C# Windows
点赞
收藏

51CTO技术栈公众号