Tài liệu Windows Forms

18 338 0
Tài liệu Windows Forms

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter Programming with Windows Forms Department of Software Engineering Faculty of Information Technology Natural Sciences University Agenda Introduction Windows Forms How to handle events in Windows Forms Adding controls to forms (design-time) Dynamically adding controls to Forms (runtime) Using Complex Controls Creating GUI Components Working with Menu Creating MDI applications with Windows Forms Deploying Windows Forms Applications What is Windows Forms (a.k.a WinForms)? Windows Forms is part of the NET framework core classes in System.Windows.Forms namespace design-time support in various namespaces Windows Forms provides classes for building UIs e.g custom forms, common controls, standard dialogs Visual Studio NET provides tools for using Windows Forms templates for common starting places, and a visual designer Windows Forms Application Structure A Windows Forms application has three pieces the application itself forms in the application controls on the form Application mainForm MyForm Label label1 “Hell…” button1 Button “OK” System.Windows.Forms.Application The Application class represents the application itself no instances (all properties and methods are static) processes UI events delivered by Windows Run, DoEvents provides access to application environment ExecutablePath, StartupPath CommonAppDataPath, UserAppDataPath CommonAppDataRegistry, UserAppDataRegistry class MyApp { public static void Main() { MyForm form = new MyForm(); System.Windows.Forms.Application.Run(form); } } System.Windows.Forms.Form Instances of the Form class represent windows provide window-style services, e.g properties: Text, Size, Location, Controls methods: Show, ShowDialog, Close events: Load, Click, Closing custom forms typically derive from base Form class class MyForm : Form { public MyForm() { this.Text = "This is my form!"; this.Location = new Point(10, 10); this.Size = new Size(100, 100); } } Form appearance Various properties influence a form’s appearance Property Type Purpose Text string Text to display (if applicable) Location Point Upper/left coordinate of form Size Font Point Font Width/height of form Get/set displayed text font ForeColor Color Get/set foreground color Opacity double Get/set degree of opacity (as a %) (many more…) … … Often, changing a property results in event notification Move (Location), Resize (Size), FontChanged (Font) Controls Controls are visual components System.Windows.Forms.Control is base class for UI elements e.g Form, Button, Label, TextBox, ListBox, etc contained and arranged by parent (usually a Form) held in parent’s Controls collection public class MyForm : Form { private Button button1; } public MyForm() { button1 = new Button(); button1.Text = "Click Me!"; button1.Location = new Point(10, 10); this.Controls.Add(button1); } Control interaction (part 1) Public events are used by control “consumers” containing form/control registers for events of interest public class MyForm : Form { private Button button1; public MyForm() { button1 = new Button(); button1.Click += new EventHandler(button1_Click); } } void button1_Click( object sender, EventArgs e ) { // Handle event as needed } Control interaction (part 2) Derived controls/forms have two approaches may register for base class events may override corresponding virtual functions (if provided) Decision driven by functionality desired, not performance event versus override approach equivalent in many cases overriding provides control over when/if events are fired public EveryOtherClickButton : Button { private int clickNum = 0; } protected override void OnClick( EventArgs e ) { clickNum++; if( (clickNum % 2) == ) base.OnClick(e); // Button.OnClick fires Click event } Complex Controls Docking Controls Anchor a control to one edge of its container Make a control fill the available space in its container Splitter Windows Allow docked controls to be resized at run time by the user Panels Control Group controls together or subdivide a form into functional areas TreeView and ListView controls Controls for Navigating Data The Explorer Interface User Controls A User Control has all the basic functionality for a graphical control that will be used on a Windows Form Inherits from System.Windows.Forms.UserControl Inherited Properties can override these properties Inherited Events can override these inherited events if you need to, but it is best if you override only the inherited On method instead of directly overriding the base event, so that future controls may benefit from the standards Menu MainMenu component displays a menu at run time All submenus of the main menu and individual items are MenuItem objects ContextMenu component is used to provide users with an easily accessible Pop-up menus that appear when you right-click a form or control MDI applications MDI – Multiple Document Interface Form Properties IsMDIContainer MDIParent ActiveMDIChild MenuItem Properties MergeType & MergeOrder MDIList Arrange MDIChilds MdiLayout.Cascade, MdiLayout.TileHorizontal, or MdiLayout.TileVertical Deploying Windows Forms Applications Microsoft Windows Installer Service keeps track of every application that's installed on a computer allows to uninstall, repair, or reinstall a package based on the state of the machine allows to roll back installations Deployment Projects Templates Deploying Windows Forms Applications (cont) Creating a Windows Installer Package Setup project properties File Installation Management Registry Settings Management File Types Management User Interface Management Custom Actions Management Launch Condition Management References www.msdn.microsoft.com MS Press Microsoft Visual C Sharp Dot NET Step By Step Version.2003 - l-mcs301-200311-25 Sams Teach Yourself Visual.Studio.Dot.Net.2003.In.21Days - lstdn02-2003-7-11.rar FTP: 172.29.22.45 Username: sv Password: sv Directory: dotNET ... applications with Windows Forms Deploying Windows Forms Applications What is Windows Forms (a.k.a WinForms)? Windows Forms is part of the NET framework core classes in System .Windows. Forms namespace...Agenda Introduction Windows Forms How to handle events in Windows Forms Adding controls to forms (design-time) Dynamically adding controls to Forms (runtime) Using Complex Controls... in various namespaces Windows Forms provides classes for building UIs e.g custom forms, common controls, standard dialogs Visual Studio NET provides tools for using Windows Forms templates for

Ngày đăng: 18/01/2013, 08:50

Tài liệu cùng người dùng

Tài liệu liên quan