1. Trang chủ
  2. » Công Nghệ Thông Tin

Chương 3: Lập trình với Windows Forms pps

18 843 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 18
Dung lượng 1,25 MB

Nội dung

Ch Ch apter apter 3 3 Programming with Windows Forms Programming with Windows Forms Department of Software Engineering Department of Software Engineering Faculty of Information Technology Faculty of Information Technology Natural Sciences University Natural Sciences University Agenda Agenda Introduction Windows Forms Introduction Windows Forms How to handle events in Windows Forms How to handle events in Windows Forms Adding 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 Components Creating GUI Components Working with Menu Working with Menu Creating MDI applications with Windows Creating MDI applications with Windows Forms Forms Deploying Windows Forms Applications Deploying Windows Forms Applications 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 framework framework core classes in System.Windows.Forms core classes in System.Windows.Forms namespace namespace design-time support in various namespaces design-time support in various namespaces Windows Forms provides classes for Windows Forms provides classes for building UIs building UIs e.g. custom forms, common controls, standard e.g. custom forms, common controls, standard dialogs dialogs Visual Studio .NET provides tools for using Visual Studio .NET provides tools for using Windows Forms Windows Forms templates for common starting places, and a templates for common starting places, and a visual designer visual designer Windows Forms Application Windows Forms Application Structure Structure A Windows Forms application has three pieces A Windows Forms application has three pieces the application itself the application itself forms in the application forms in the application controls on the form controls on the form Application mainForm MyForm label1 button1 Label “Hell…” Button “OK” System.Windows.Forms.Application System.Windows.Forms.Application The Application class represents the application The Application class represents the application itself itself no instances (all properties and methods are static) no instances (all properties and methods are static) processes UI events delivered by Windows processes UI events delivered by Windows Run Run , , DoEvents DoEvents provides access to application environment provides access to application environment ExecutablePath ExecutablePath , , StartupPath StartupPath CommonAppDataPath CommonAppDataPath , , UserAppDataPath UserAppDataPath CommonAppDataRegistry CommonAppDataRegistry , , UserAppDataRegistry UserAppDataRegistry class MyApp { public static void Main() { MyForm form = new MyForm(); System.Windows.Forms.Application.Run(form); } } System.Windows.Forms.Form System.Windows.Forms.Form Instances of the Form class represent Instances of the Form class represent windows windows provide window-style services, e.g. provide window-style services, e.g. properties: properties: Text, Size, Location, Controls Text, Size, Location, Controls methods: methods: Show, ShowDialog, Close Show, ShowDialog, Close events: events: Load, Click, Closing Load, Click, Closing custom forms typically derive from base custom forms typically derive from base Form Form class 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 Form appearance Various properties influence a form’s appearance Various properties influence a form’s appearance Often, changing a property results in event notification Often, changing a property results in event notification Move Move ( ( Location Location ), ), Resize Resize ( ( Size Size ), ), FontChanged FontChanged ( ( Font Font ) ) Property Property Type Type Purpose Purpose Text Text string string Text to display (if applicable) Text to display (if applicable) Location Location Point Point Upper/left coordinate of form Upper/left coordinate of form Size Size Point Point Width/height of form Width/height of form Font Font Font Font Get/set displayed text font Get/set displayed text font ForeColor ForeColor Color Color Get/set foreground color Get/set foreground color Opacity Opacity double double Get/set degree of opacity (as Get/set degree of opacity (as a %) a %) (many more…) (many more…) … … … … Controls Controls Controls are visual components Controls are visual components System.Windows.Forms.Control is base class for UI System.Windows.Forms.Control is base class for UI elements elements e.g. e.g. Form, Button, Label, TextBox, ListBox Form, 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 Controls Controls collection 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) 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 interest 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) Control interaction (part 2) Derived controls/forms have two approaches Derived controls/forms have two approaches may register for base class events may register for base class events may 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 performance performance event versus override approach equivalent in many cases event versus override approach equivalent in many cases overriding provides control over when/if events are fired 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) == 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 . with Windows Creating MDI applications with Windows Forms Forms Deploying Windows Forms Applications Deploying Windows Forms Applications What is Windows Forms (a.k.a. What is Windows Forms. University Agenda Agenda Introduction Windows Forms Introduction Windows Forms How to handle events in Windows Forms How to handle events in Windows Forms Adding controls to forms (design-time) Adding controls to forms (design-time) Dynamically. (a.k.a. WinForms)? WinForms)? Windows Forms is part of the .NET Windows Forms is part of the .NET framework framework core classes in System .Windows. Forms core classes in System .Windows. Forms namespace namespace design-time

Ngày đăng: 29/07/2014, 06:20

TỪ KHÓA LIÊN QUAN

w