1. Trang chủ
  2. » Tất cả

Tài liệu Windows Forms

18 156 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

ChChapterapter 3 3 Programming with Windows FormsProgramming with Windows FormsDepartment of Software EngineeringDepartment of Software EngineeringFaculty of Information TechnologyFaculty of Information TechnologyNatural Sciences UniversityNatural Sciences University AgendaAgendaAgendaAgendaIntroduction Windows FormsIntroduction Windows FormsHow to handle events in Windows FormsHow to handle events in Windows FormsAdding controls to forms (design-time)Adding controls to forms (design-time)Dynamically adding controls to Forms Dynamically adding controls to Forms (runtime)(runtime)Using Complex Controls Using Complex Controls Creating GUI ComponentsCreating GUI ComponentsWorking with MenuWorking with MenuCreating MDI applications with Windows Creating MDI applications with Windows FormsFormsDeploying Windows Forms ApplicationsDeploying Windows Forms Applications What is Windows Forms (a.k.a. What is Windows Forms (a.k.a. WinForms)?WinForms)?What is Windows Forms (a.k.a. What is Windows Forms (a.k.a. WinForms)?WinForms)?Windows Forms is part of the .NET Windows Forms is part of the .NET frameworkframeworkcore classes in System.Windows.Forms core classes in System.Windows.Forms namespacenamespacedesign-time support in various namespacesdesign-time support in various namespacesWindows Forms provides classes for Windows Forms provides classes for building UIsbuilding UIse.g. custom forms, common controls, standard e.g. custom forms, common controls, standard dialogsdialogsVisual Studio .NET provides tools for using Visual Studio .NET provides tools for using Windows FormsWindows Formstemplates for common starting places, and a templates for common starting places, and a visual designervisual designer Windows Forms Application StructureWindows Forms Application StructureWindows Forms Application StructureWindows Forms Application StructureA Windows Forms application has three piecesA Windows Forms application has three piecesthe application itselfthe application itselfforms in the applicationforms in the applicationcontrols on the formcontrols on the formApplicationmainFormMyFormlabel1button1Label“Hell…”Button“OK” System.Windows.Forms.ApplicationSystem.Windows.Forms.ApplicationSystem.Windows.Forms.ApplicationSystem.Windows.Forms.ApplicationThe Application class represents the application itselfThe Application class represents the application itselfno instances (all properties and methods are static)no instances (all properties and methods are static)processes UI events delivered by Windowsprocesses UI events delivered by WindowsRunRun, , DoEventsDoEventsprovides access to application environmentprovides access to application environmentExecutablePathExecutablePath, , StartupPathStartupPathCommonAppDataPathCommonAppDataPath, , UserAppDataPathUserAppDataPathCommonAppDataRegistryCommonAppDataRegistry, , UserAppDataRegistryUserAppDataRegistryclass MyApp { public static void Main() { MyForm form = new MyForm(); System.Windows.Forms.Application.Run(form); }} System.Windows.Forms.FormSystem.Windows.Forms.FormSystem.Windows.Forms.FormSystem.Windows.Forms.FormInstances of the Form class represent Instances of the Form class represent windowswindowsprovide window-style services, e.g.provide window-style services, e.g.properties: properties: Text, Size, Location, ControlsText, Size, Location, Controlsmethods: methods: Show, ShowDialog, CloseShow, ShowDialog, Closeevents: events: Load, Click, ClosingLoad, Click, Closingcustom forms typically derive from base custom forms typically derive from base FormForm classclassclass MyForm : Form { public MyForm() { this.Text = "This is my form!"; this.Location = new Point(10, 10); this.Size = new Size(100, 100); }} Form appearanceForm appearanceForm appearanceForm appearanceVarious properties influence a form’s appearanceVarious properties influence a form’s appearanceOften, changing a property results in event notificationOften, changing a property results in event notificationMoveMove ( (LocationLocation), ), ResizeResize ( (SizeSize), ), FontChangedFontChanged ( (FontFont))Property Type PurposeText string Text to display (if applicable)Location Point Upper/left coordinate of formSize Point Width/height of formFont Font Get/set displayed text fontForeColor Color Get/set foreground colorOpacity double Get/set degree of opacity (as a %)(many more…) … … ControlsControlsControlsControlsControls are visual componentsControls are visual componentsSystem.Windows.Forms.Control is base class for UI System.Windows.Forms.Control is base class for UI elementselementse.g. e.g. Form, Button, Label, TextBox, ListBoxForm, Button, Label, TextBox, ListBox, etc., etc.contained and arranged by parent (usually a Form)contained and arranged by parent (usually a Form)held in parent’s held in parent’s ControlsControls collection collectionpublic 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)Control interaction (part 1)Control interaction (part 1)Control interaction (part 1)Public events are used by control Public events are used by control “consumers”“consumers”containing form/control registers for events of containing form/control registers for events of interestinterestpublic 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)Control interaction (part 2)Control interaction (part 2)Control interaction (part 2)Derived controls/forms have two approachesDerived controls/forms have two approachesmay register for base class eventsmay register for base class eventsmay override corresponding virtual functions (if provided)may override corresponding virtual functions (if provided)Decision driven by functionality desired, not Decision driven by functionality desired, not performanceperformanceevent versus override approach equivalent in many casesevent versus override approach equivalent in many casesoverriding provides control over when/if events are firedoverriding provides control over when/if events are firedpublic EveryOtherClickButton : Button{ private int clickNum = 0; protected override void OnClick( EventArgs e ) { clickNum++; if( (clickNum % 2) == 0 ) base.OnClick(e); // Button.OnClick fires Click event }} [...]... 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... 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 . System .Windows. Forms. Application.Run(form); }} System .Windows. Forms. FormSystem .Windows. Forms. FormSystem .Windows. Forms. FormSystem .Windows. Forms. FormInstances. (a.k.a. What is Windows Forms (a.k.a. WinForms)?WinForms)?What is Windows Forms (a.k.a. What is Windows Forms (a.k.a. WinForms)?WinForms) ?Windows Forms is part

Ngày đăng: 18/01/2013, 09:11

Xem thêm: TĂ i liệu Windows Forms

w