Lập trình GUI 1 CuuDuongThanCong.com https://fb.com/tailieudientucntt Lập trình GUI • User interface modeling • User interface architecture • User interface coding 2 CuuDuongThanCong.com https://fb.com/tailieudientucntt The Control class hierarchy 3 CuuDuongThanCong.com https://fb.com/tailieudientucntt Windows Forms Application Structure • A Windows Forms application has three pieces – the application itself – forms in the application – controls on the form Application MyForm Label label1 ―Hell…‖ mainForm Button button1 ―OK‖ 4 CuuDuongThanCong.com https://fb.com/tailieudientucntt GUI Tree Structure GUI Internal structure Form Form containers Button Panel Button Panel Label Label 5 CuuDuongThanCong.com https://fb.com/tailieudientucntt Cách tạo WinForm bằng Console Application 6 CuuDuongThanCong.com https://fb.com/tailieudientucntt • Project Add Reference 7 CuuDuongThanCong.com https://fb.com/tailieudientucntt Form • Một « form » là một cửa sổ màn hình - một đơn vị giao diện người dùng do Microsoft đưa ra kể từ Windows 1.0 • Một ứng dụng Windows Forms (WinForms) phải có ít nhất một cửa sổ « main form » (cửa sổ chính • Form có thể chứa các component • Form có thể có các file resource 8 CuuDuongThanCong.com https://fb.com/tailieudientucntt Ví dụ 1 class Program { static void Main(string[] args) { Form f = new Form(); Application.com https://fb.com/tailieudientucntt Ví dụ 2 class Program { static void Main(string[] args) { MessageBox.com https://fb.com/tailieudientucntt Application class Exit Stops all running message loops and closes all windows in the application. Note that this may not force the application to exit Run Starts a standard message loop on the current thread. If a Form is given, also makes that form visible. DoEvents Processes any Windows messages currently in the message queue.com https://fb.com/tailieudientucntt Ví dụ 3 public static void Main() { Form form1 = new Form(); Form form2 = new Form(); form1.Text = "Form passed to Run()"; form2.Text = "Second form"; form2.Run() has returned control back to Main.
Bye, bye!", "TwoForms"); } 12 CuuDuongThanCong.com https://fb.com/tailieudientucntt Ví dụ 3 13 CuuDuongThanCong.com https://fb.com/tailieudientucntt Form Properties Thuộc tính Kiểu Mô tả FormBorderStyle FormBorderStyle: Kiểu đường viền FixedDialog, Fixed3D… ControlBox bool Có system menu box? MaximizeBox bool MinimizeBox bool Icon Icon ShowInTaskBar bool StartPosition FormStartPosition 14 CuuDuongThanCong.com https://fb.com/tailieudientucntt Form Properties Thuộc tính Kiểu Mô tả SizeGripStyle SizeGripStyle: Show, Hide… WindowState FormWindowState: Normal, Maximized, Minimized TopMost bool Text string Size Point ForeColor color Font font Location Point 15 CuuDuongThanCong.com https://fb.com/tailieudientucntt Form Properties Thuộc tính Kiểu Mô tả AcceptButton CancelButton 16 CuuDuongThanCong.com https://fb.com/tailieudientucntt StartPosition - FormBorderStyle • CentreParent cho modal dialogs • CentreScreen cho main form hay splash screen • WindowsDefaultLocation FixedDialog : modal dialog boxes FixedSingle : main form None : splash screen Sizable 17 CuuDuongThanCong.com https://fb.com/tailieudientucntt Ví dụ 4 18 CuuDuongThanCong.com https://fb.com/tailieudientucntt Ví dụ 4 static void Main(string[] args) { Form form = new Form(); form.Text = "Form Properties"; form.MaximizeBox = false; form.com https://fb.com/tailieudientucntt Form Method • Show() • ShowDialog(); • Hide(); • Close(); 20 CuuDuongThanCong.com https://fb.com/tailieudientucntt 21 CuuDuongThanCong.com https://fb.com/tailieudientucntt Form Event • Click • DoubleClick • KeyDown • MouseHover • Paint • Resize • …… 22 CuuDuongThanCong.com https://fb.com/tailieudientucntt Sự kiên form Load class Program { static void Main(string[] args) { Form f = new Form(); f.Load += new EventHandler(f_Load); Application.com https://fb.com/tailieudientucntt Events • Một event là một đối tượng biểu diễn một hành động • Ví dụ: – The mouse is moved or button clicked – The mouse is dragged – A graphical button is clicked – A keyboard key is pressed – A timer expires • Sự kiện thường tương ứng với thao tác của người dùng • Có thể viết các bộ đáp ứng sự kiện GUI event Event message User program Control handler 24 CuuDuongThanCong.com https://fb.com/tailieudientucntt Event Handler: { Get N1 and N2 User Return N1+N2 Call the program } Program: Put N1+N2 25 CuuDuongThanCong.com https://fb.com/tailieudientucntt Call me when you Event Button are pressed Handler I have been pressed Event Button Handler Handler 1 GUI Control Handler GUI Control Handler 2 GUI Control Handler 3 calls Handler 1 for event E calls Object A raises event E Delegate for event E Handler 2 for event E Handler 3 for event26 E CuuDuongThanCong.com https://fb.com/tailieudientucntt GUI Events App1 mouse App2 click OK OK Cancel Cancel App2 code: OKbtn_click() App1 { do stuff; event } Window CancelBtn_click() loop { System do different stuff; } which App2 App2Form_click() input event app? { device loop event which do other stuff; loop control? } 27 CuuDuongThanCong.com https://fb.com/tailieudientucntt GUI program GUI program: • User input commands main() { decl data storage; • Non-linear execution initialization code; • Unpredictable order create GUI; register callbacks; • Much idle time main event loop; } Callback1() //button1 press • Event callback procs { code; } Callback2() //button2 press { code; } 28 CuuDuongThanCong.com … https://fb.com/tailieudientucntt C# WinApp C# WinApp: • ―delegates‖ = callbacks Class{ decl data storage; • Function pointers constructor(){ initialization code; create GUI controls; register callbacks; } • Listeners main(){ Run(new ) } callback1(){ do stuff; } callback2(){ do stuff; } 29 CuuDuongThanCong.com … https://fb.com/tailieudientucntt Delegates 1. Đăng ký control để nhận events • Gắn vào Control một function pointer để gọi callback function • F.Load += new EventHandler(MyLoadHandler); 2. Nhận events từ control • Control sẽ gọi function pointer • private void button1_Click(object sender, EventArgs e){ 1.Click += button1_click( ) click Button1 Button1_click() 2. button1_Click( ) callback 30 CuuDuongThanCong.com https://fb.com/tailieudientucntt Event Handler • Thông điệp gửi đi bằng cách chuyển giao.
• Bộ xử lý sự kiện(Event Handler) sẽ được gọi khi sự kiện tương ứng phát sinh void EventMethodName(Object sender, EventArgs e) 31 CuuDuongThanCong.com https://fb.com/tailieudientucntt Form - Click static void Main(string[] args) { Form f = new Form(); f.Click+=new EventHandler(f_Click); Application.com https://fb.com/tailieudientucntt Paint Event class Program { static void Main(string[] args) { Form f = new Form(); f.Click+=new EventHandler(f_Click); Application.Run(f); } static void f_Click(Object sender, EventArgs e) { Form f = (Form)sender; Graphics gx = f.DrawString("Form 1 \n Form 1\n", f.com https://fb.com/tailieudientucntt Paint Event class Program { static void Main(string[] args) { Form f = new Form(); f.Paint+=new PaintEventHandler(f_Paint); Application.Run(f); } static void f_Paint(Object sender, PaintEventArgs e) { Form f = (Form)sender; Graphics gx = f.DrawString("Form 1 \n Form 1\n", f.com https://fb.com/tailieudientucntt static void Main(string[] args) { Form f1 = new Form(); f1.Text = "2 Paint Event"; f1.Paint += new PaintEventHandler(f1_Paint1); f1.Paint += new PaintEventHandler(f1_Paint2); Application.Run(f1); } static void f1_Paint1(Object sender, PaintEventArgs pea) { Form f = (Form)sender; Graphics g = pea.DrawString("Paint 1 Event ", f.Black, 0, 0); } static void f1_Paint2(Object sender, PaintEventArgs pea) { Form f = (Form)sender; Graphics g = pea.DrawString("Paint 2 Event ", f.com https://fb.com/tailieudientucntt Kế thừa Form class Myform:System.Form { public Myform() { Text = "My Form Class"; } } static void Main(string[] args) { Myform f=new Myform(); Application.com https://fb.com/tailieudientucntt Kế thừa Form class Myform:System.Form { public Myform() { Text = "My Form Class"; } protected override void OnPaint(PaintEventArgs pea) { Graphics g=pea.DrawString("Hello World", Font, Brushes.com https://fb.com/tailieudientucntt Kế thừa Form class Program { static void Main(string[] args) { Myform f=new Myform(); f.Text = "Ke thua tu " + f.Paint+=new PaintEventHandler(f_Paint); Application.Run(f); } static void f_Paint(Object sender, PaintEventArgs pea) { Form form = (Form)sender; Graphics g = pea.DrawString("New Hello World", form.com https://fb.com/tailieudientucntt Kế thừa Form class Myform:System.Form { public Myform() { Text = "My Form Class"; } protected override void OnPaint(PaintEventArgs pea) { base.OnPaint(pea); Graphics g=pea.DrawString("Hello World", Font, Brushes.com https://fb.com/tailieudientucntt MessageBox.com https://fb.com/tailieudientucntt MessageBox Buttons Member Value Ok 0 OkCancel 1 AbortRetryIgnore 2 YesNoCancel 3 YesNo 4 RetryCancel 5 41 CuuDuongThanCong.com https://fb.com/tailieudientucntt MessageBox Icon Member Value None 0x00 Hand 0x10 Stop 0x10 Error 0x10 Question 0x20 Exclamation 0x30 Warning 0x30 Asterisk 0x40 Information 0x40 42 CuuDuongThanCong.com https://fb.com/tailieudientucntt 43 CuuDuongThanCong.com https://fb.com/tailieudientucntt Form Controls • Là đơn vị cơ sở để tạo nên giao diện người dùng trong lập trình WinForm. • Là bất kỳ đối tượng nào nằm trong vùng chứa của Container có khả năng tương tác với người sử dụng. • Là đối tượng dùng để nhận dữ liệu được nhập vào hoặc xuất dữ liệu trên window form • Các control có các đặc điểm, các phương thức và các sự kiện riêng cho control đó 44 CuuDuongThanCong.com https://fb.com/tailieudientucntt Thuộc tính chung Properties BackColor CanFocus Enabled ForeColor Name Text Visible 45 CuuDuongThanCong.com https://fb.com/tailieudientucntt Các lớp cơ sở • System.Control -chứa chức năng cơ bản của thao tác xử lý bàn phím và nhập từ chuột và xử lý tin nhắn window.ButtonBase - Lớp này hỗ trợ chức năng cơ bản của một nút • System.TextBoxBase - cung cấp chức năng và thuộc tính thông thuờng cho các lớp thừa hưởng. Cả hai lớp TextBox và RichTextBox sử dụng chức năng cung cấp bởi TextBoxBase.ScrollableControl - quản lý sự phát sinh và hiển thị của các thanh cuộn đến người dùng để truy cập đến gốc của một hiển thị.ContainerControl - Lớp này quản lý chức năng yêu cầu cho một control để hành động • System.Panel - có thể chứa các control thêm vào, nhưng khác với lớp ContainerControl, nó phân loại các control một cách đơn giản.Form - Tạo bất kỳ loại cửa sổ nào: standard, toolbox, borderless, modal dialog boxes và multi-document interfaces.UserControl - tạo một custom control đến việc được dùng trong một nơi phức tạp trong một ứng dụng hay tổ chức 46 CuuDuongThanCong.com https://fb.com/tailieudientucntt STANDARD CONTROL • Một đối tượng control kế thừa trực tiếp/ gián tiếp từ System.Control • Có các loại: – Action control: Button, Toolbar, MenuBar, ContextMenu – Value control: Label, TextBox, PictureBox – List control: ListBox, ComboBox, DataGrid, TreeView, – Container control: GroupBox, Panel, ImageList, … – Dialogs:OpenFileDialog, SaveFileDialog, PrintDialog, etc 47 CuuDuongThanCong.com https://fb.com/tailieudientucntt Buttons Control Mô tả Button Normal button for actions (e., OK or Cancel) CheckBox Yes/no selection button RadioButton Single selection from a range of choices Time and date Control Mô tả DateTimePicker UI for specifying a date or time MonthCalendar UI showing a single calendar month 48 CuuDuongThanCong.com https://fb.com/tailieudientucntt Labels and pictures Control Mô tả GroupBox Visual grouping for sets of related controls Text label, usually providing a name or description for Label some other control (e., a text box) A picture: supports various bitmap formats (BMP, ICO, PictureBox JPEG, TIFF, and PNG) and Windows metafiles Hyperlink, e., a URL; this effectively combines label- LinkLabel like and button-like behavior 49 CuuDuongThanCong.com https://fb.com/tailieudientucntt Text editing Control Mô tả TextBox An editable text field (plain text only) An editable text fields supporting text with RichTextBox formatting (based on RTF—the Rich Text Format) A text box containing a number, and an associated NumericUpDown pair of up/down buttons (often known as a spin control) Similar to a NumericUpDown, only the text box can DomainUpDown contain any string; the up and down buttons move through a list of strings 50 CuuDuongThanCong.com https://fb.