| | | 用C#设计Windows应用程序模板 | | 2002-05-28·
·Crystall编译··yesky
| 上一页 1 2 3 4 5 6 下一页 创建工具条
为了使应用程序的界面更友好,可以在窗体中添加一个工具条。工具条由System.Windows.Forms.ToolBar类描述。窗体中可有多个工具条,工具条中包含了一个或多个ToolBarButton类描述的按钮,可以在每个按钮中插入图像或图标,要达到这个目的你需要一个ImageList控件作为图像容器。
| ImageList imageList = new ImageList(); | 对于每个图像文件首先要实例化为image对象,然后将这些图像添加到ImageList控件中,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(imageFolder + "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 = imageList; | 然后将ImageList控件中的图像赋给工具按钮的ImageIndex属性。
newToolBarButton.ImageIndex = 0; openToolBarButton.ImageIndex = 1; saveToolBarButton.ImageIndex = 2; printToolBarButton.ImageIndex = 3;
| 象菜单项一样,现在必须把工具按钮加入到工具条中。
toolBar.Buttons.Add(separatorToolBarButton); toolBar.Buttons.Add(newToolBarButton); toolBar.Buttons.Add(openToolBarButton); toolBar.Buttons.Add(saveToolBarButton); toolBar.Buttons.Add(separatorToolBarButton); toolBar.Buttons.Add(printToolBarButton); | 最后将工具条加入到窗体中。
| this.Controls.Add(toolBar); | 添加状态条
状态条由System.Windows.Forms.StatusBar描述,它提供了定制控件的外观的属性,状态条由StatusBarPanel对象组成,在我们的模板中状态条有两个嵌套板:
StatusBar statusBar = new StatusBar(); StatusBarPanel statusBarPanel1 = new StatusBarPanel(); StatusBarPanel statusBarPanel2 = new StatusBarPanel();
| 状态条和状态跳上的嵌套板由下面的代码设置:
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.Controls.Add(statusBar); | 事件处理器
在windows程序设计中添加事件处理器是最重要的任务。事件处理器保证了程序与用户交互,同时完成其他重要的功能。在c#中你可以给控件和菜单事件添加事件处理器以俘获你想处理的事件,下面的代码给Button控件的click事件设计了一个事件处理器:
button.Click += new System.EventHandler(this.button_Click);
button_Click事件处理器必须被处理:
private void button_Click(Object sender, System.EventArgs e) { MessageBox.Show("Thank you.", "The Event Information"); }
| MenuItem 对象在实例化的同时可以给赋以一个事件处理器:
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)); | 你不能给工具按钮指派一个事件处理器,但可以给工具条指派一个事件处理器:
toolBar.ButtonClick += new
ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
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; } }
| 例子中也给窗体的close事件设计了一个事件处理器,通过重载OnClosing方法你可以接收用户点击窗体的X按钮,这样你可以取消关闭事件:
protected override void OnClosing(CancelEventArgs e) { MessageBox.Show("Exit now.", "The Event Information"); }
| 现在我们的模板就完成了,你可以使用他开始你的WINDOWS应用程序设计。
上一页 1 2 3 4 5 6 下一页 | | | 感谢
访问天极网,如果您觉得该文章涉及版权问题,请看这里!
|
|