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

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

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

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

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:27:30

C#Windows应用

2009-08-14 17:36:20

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 14:17:16

C#Windows服务

2009-08-14 16:24:00

Windows服务程序

2009-08-14 14:45:03

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-24 13:30:50

C# Windows

2009-08-14 10:50:09

Windows服务介绍
点赞
收藏

51CTO技术栈公众号