2022年用C#设计Windows应用程序模板 .pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年用C#设计Windows应用程序模板 .pdf》由会员分享,可在线阅读,更多相关《2022年用C#设计Windows应用程序模板 .pdf(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、通常 windows 应用程序都有相似的特征:控件、菜单、工具条、状态栏等等。每次我们开始作一个新的windows 应用程序时都是以相同的事情开始:建立项目,添加控件和事件处理器。如果我们有一个模板,那么我们就可以节约大量的时间了。在介绍如何建立模板的过程中,将涉及大量的微软.net framework类库的基本知识。如果你没有使用集成开发环境那么本文介绍的模板对你将非常有用,如果你使用了visual 这样的集成开发环境你也可以从中了解控件的工作方式,这对你也是很有用的。写一个 windows 应用程序总是从下面的几个步骤开始:1、创建一个窗体2、给窗体添加控件3、添加菜单和菜单项,并绑定到窗
2、体上4、创建工具条5、添加状态栏6、添加事件处理器在 windows 应用程序开发中,你不可能完全跳过这些步骤,你可以对他作些修改,但不可能完全跳过。创建窗体在.Net FrameWork 程序设计中,窗体总是System.Windows.Forms.Form类的子类,他继承聊着各类的全部特征,你甚至不用添加任何一行代码就可以创建window 窗体了。现在我们创建一个form 类的子类myapp,需要注意的是在说明继承时使用冒号:。using System.Windows.Forms; public class MyWinApp: Form 与其他程序相似,我们需要一个main() 的主方法作
3、为应用程序的入口。在main() 中使用 System.Windows.Forms的 run 方法来开始应用程序和显示窗体。run 方法有三种重载方式,对于windows 应用程序,我们采取重载一个窗体对象的方式:public static void Main() MyWinApp form = new MyWinApp(); Application.Run(form); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 16 页 - - - - - - - - - 作为选
4、择,可以在main() 方法中将两行代码写成一行:public static void Main() Application.Run(new MyWinApp(); 设置属性一旦有了窗体,就可以设置它的属性(象高度、宽度、标题等)并指定一个象图1那样的图标, 可以通过代码或从构造器重调用一个方法来完成这个设置。在 list1中有一个方法InitializeComponent,可以将初始化代码写入其中。使用 width 和 height属性设置宽度和高度this.Width = 400; this.Height = 300; 使用窗体的text属性设置标题this.Text = My Windo
5、ws Application; 设置图标如果未作说明,windows 应用程序将显示一个缺省图标,可以修改这个图标,方法是设置窗体的icon 属性,这个属性接受一个System.Drawing.Icon对象。 Icon 类有几个可以用于初始化Icon 对象的构造函数,需要说明的是最好提供.ico文件的完全路径。this.Icon = new Icon(imageFolder + applicationLogo.ico); 这里 imageFolder是一个静态域,用于指示icon 文件的目录, imageFolder的定义如下:static String imageFolder = Image
6、s + Path.DirectorySeparatorChar.ToString(); 这告诉应用程序icon 文件在应用程序的image 目录下,这里使用了System.IO.Path 类的 DirectorySeparatorChar属性获得操作系统用于分隔目录和父目录的分隔符,在这里没有使用 /,这是因为对于windows 操作系统这是正确的,但对于linux和 unix 则不然。这也是为了证明模板对于其他操作系统也是方便的。窗体的位置名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - -
7、 第 2 页,共 16 页 - - - - - - - - - 使窗体居中时非常有用的,要达到这个目的,需要使用StartPosition属性,并将FormStartPosition 的一个成员赋给他。this.StartPosition = FormStartPosition.CenterScreen; 当然也可以用form 类的 CenterToScreen方法是窗体居中,但这个方法不能直接使用。this.CenterToScreen(); form 类还有其他一些让人感兴趣的属性和方法,这里列出了其中的部分:1、设置 Opacity 属性创建一个透明或半透明的窗体2、设置 modal 属
8、性使窗体为模式的或非模式的3、通过 BackColor 属性改变窗体的背景颜色4、将 TopMost 属性设置为true, 以确定窗体在其他所有非最顶部窗体之上给窗体添加控件windows 控件均继承自System.Windows.Forms.Control类, control类处理用户输入、安全等,他给窗体的控件提供了一个windows 句柄,以及一些重要的属性,如Name, Enabled, Text, BackColor, Left, Top, Size, Location, Visible, Width, 和 Height。System.Windows.Forms 名称空间提供了12
9、个控件, 每一个控件都有它自己的属性和特征,所以在篇文章中我们不可能全部讨论。给窗体添加控减非常容易,下面的代码给窗体添加了三个控件,分别是:Label, Button, 和 TreeView 。Label label; Button button; TreeView tree; 为了简便,可以在声明的同时实例化这些对象。Label label = new Label(); Button button = new Button(); TreeView tree = new TreeView(); 然后在 InitializeComponent方法中设置这些控件的属性,尤其是设置控件的大小和在窗
10、体中的位置, 对于大小可以使用width 和 height属性,比如 treeview控件的大小可以使用下面的属性:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 16 页 - - - - - - - - - tree.Width = 100; tree.Height = 100; 确定控件的位置可以使用控件的left和 top 属性,这两个属性决定了控件的左上角的位置,就像下面的语句决定了treeview的位置:tree.Top = 40; tree.Left = 20
11、; 当然你也可以使用更简单的Location属性, 将 System.Drawing.Point结构的实例赋给他。我们用这种方法确定Label 和 Button 的位置。label.Location = new Point(220, 40); button.Location = new Point(220, 80); 下一步就是要使控件在窗体上可见。使用Form.ControlCollection类的 add 方法将每个控件添加到窗体的ControlCollection中,ControlCollection可以使用窗体的控件属性访问。this.Controls.Add(label); this
12、.Controls.Add(button); this.Controls.Add(tree); 添加菜单和菜单项要找到没有菜单的windows 应用程序非常困难, 菜单使访问应用程序的功能变得很简单,在大多数环境下可以最小的使用控件。菜单比控件占用更少的空间,同时使应用程序显得更有组织。在 System.Windows.Forms 名称空间中,所有与菜单相关的控件都是menu类的子类。 menu 是一个抽象类,你不能直接实例化,menu类有三个子类 : ContextMenu MainMenu MenuItem ContextMenu 类表示快捷菜单,在控件或窗体区域点击鼠标右键时显示。快捷菜
13、单常用于组合窗体mainmenu类的菜单项给出应用程序的上下文,这对于用户时非常有用的。MainMenu表示传统的位于窗体顶部的菜单,你可以把它看成窗体菜单结构的容器。一个菜单是由MenuItem 表示的菜单项组成的,对于应用程序而言每一个菜单项是一个命令或其它子菜单项的父菜单。form 类都有一个menu属性,采用将mainmenu对象赋给menu属性的方式将mainmenu对象绑定到窗体。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 16 页 - - - - - -
14、 - - - 在这个模板中,我们没有使用ContextMenu 类,但我们示范了如何使用MainMenu和 MenuItem 类。我们首先需要在窗体中添加一个菜单,给窗体添加一个MainMenu对象。MainMenu mainMenu = new MainMenu(); 现在 MainMenu对象中什么都没有,下面我们给他添加一个MenuItem 对象。在List1中主菜单称为fileMenuItem,它的 text属性是 &File,& 表示他后面的字母带下划线,是该菜单的快捷键。通过使用Menu对象的 MenuItemCollection的 add 方法为 MainMenu添加一个或几个
15、MenuItem,这个集合可以通过menu类的 MenuItems 属性访问。MenuItem fileMenuItem = new MenuItem(); mainMenu.MenuItems.Add(fileMenuItem); 我们在 fileMenuItem 菜单项中还添加了其它MenuItem,这些 MenuItem 是 fileMenuItem的子菜单。你也可以给子菜单添加子菜单。图2 显示了菜单的等级结构。-图 2- 菜单项的声明如下:MenuItem fileNewMenuItem; MenuItem fileOpenMenuItem; MenuItem fileSaveMen
16、uItem; MenuItem fileSaveAsMenuItem; MenuItem fileMenuWithSubmenu; MenuItem submenuMenuItem; MenuItem fileExitMenuItem; MenuItem 类有几个构造函数,使用这些构造函数实例化你的 MenuItem 对象,并添加一个事件处理器。/ the following constructor is the same as: / menuItem fileNewMenuItem = new MenuItem(); / fileNewMenuItem.Text = &New; / file
17、NewMenuItem.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, 名师资料总结 - - -精品资料欢迎下载 - - - - - -
18、- - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 16 页 - - - - - - - - - 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,
19、 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); Event
20、 handling is discussed further in the section Adding Event Handlers. As mentioned, the menu items are added to the fileMenuItem control. fileMenuItem.MenuItems.Add(fileNewMenuItem); fileMenuItem.MenuItems.Add(fileOpenMenuItem); fileMenuItem.MenuItems.Add(fileSaveMenuItem); fileMenuItem.MenuItems.Add
21、(fileSaveAsMenuItem); fileMenuItem.MenuItems.Add(fileMenuWithSubmenu); fileMenuWithSubmenu.MenuItems.Add(submenuMenuItem); fileMenuItem.MenuItems.Add(-); / add a separator fileMenuItem.MenuItems.Add(fileExitMenuItem); 注意在菜单项之间可以创建一个分隔符,方法是使用menu类的 MenuItems 集合的 add方法。上面的例子中使用的分隔符是- 。创建工具条为了使应用程序的界面更
22、友好,可以在窗体中添加一个工具条。工具条由System.Windows.Forms.ToolBar类描述。窗体中可有多个工具条,工具条中包含了一个或多个ToolBarButton 类描述的按钮,可以在每个按钮中插入图像或图标,要达到这个目的你需要一个ImageList控件作为图像容器。ImageList imageList = new ImageList(); 对于每个图像文件首先要实例化为image 对象,然后将这些图像添加到ImageList控件名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - -
23、 - - 第 6 页,共 16 页 - - - - - - - - - 中, Image 和 Bitmap 类可以在System.Drawing名称空间中找到。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(imageF
24、older + print.gif); . . . imageList.Images.Add(newFileImage); imageList.Images.Add(openFileImage); imageList.Images.Add(saveFileImage); imageList.Images.Add(printImage); 注意你可以使用Images 集合的 add 方法将 image 对象加入到imagelist控件中。现在为将这些图加入到控件中,必须将ImageList控件赋给 ToolBar 的 ImageList属性。toolBar.ImageList = imageLi
25、st; 然后将 ImageList控件中的图像赋给工具按钮的ImageIndex 属性。newToolBarButton.ImageIndex = 0; openToolBarButton.ImageIndex = 1; saveToolBarButton.ImageIndex = 2; printToolBarButton.ImageIndex = 3; 象菜单项一样,现在必须把工具按钮加入到工具条中。toolBar.Buttons.Add(separatorToolBarButton); toolBar.Buttons.Add(newToolBarButton); toolBar.Butt
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年用C#设计Windows应用程序模板 2022 年用 C# 设计 Windows 应用程序 模板
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内