Tải bản đầy đủ (.ppt) (138 trang)

Lập trình GUI trong môi trường window

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.1 MB, 138 trang )

1
UIT-VNUHCM
Lập trình môi trường Windows
LẬP TRÌNH MÔI TRƯỜNG
WINDOW
ThS. NGUYỄN THỊ THANH TRÚC
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
KHOA CÔNG NGHỆ PHẦN MỀM
Chương 3:LẬP TRÌNH GUI
2
UIT-VNUHCM
Lập trình môi trường Windows
Nội dung
Các control cơ bản trong window form
Các Dialog
Cách tạo Menu
Tạo window Form &
Mô tả các đặc tính của Window Form
3
UIT-VNUHCM
Lập trình môi trường Windows
1.FORM
4
UIT-VNUHCM
Lập trình môi trường Windows

Giao diện người dùng cung cấp phương tiện để
người dùng có thể tương tác được với chương trình
ứng dụng.

Có hai loại giao diện người dùng:



Giao diện người dùng dạng ký tự (Character User
Interface CUI): người dùng tương tác với chương trình
theo câu lệnh nhập từ bàn phím: ví dụ Ms Dos.

Giao diện người dùng dạng đồ họa (Graphical User
Interface GUI): người dùng tương tác với chương trình
thông qua hệ thống các window.

Giao diện người dùng đồ họa được hiện thực thông
qua Window Form và các control chứa bên trong của
nó.
Giao diện người dùng
5
UIT-VNUHCM
Lập trình môi trường Windows

Các window form dùng để hiển thị tất cả các
cửa sổ trong chương trình ứng dụng

Nhận dữ liệu vào từ người dùng và hiển thị
thông tin kết quả.

Khi tạo ra 1 project kiểu Window Application, sẽ
có 1 window form tự động được thêm vào
project.

Tất cả các Window form được thừa kế từ lớp
Form chứa trong namespace:
System.Windows.Forms


Cũng có thể kế thừa từ các window form có sẵn,
rồi thêm vào các tính năng mới.
Window Form
6
UIT-VNUHCM
Lập trình môi trường Windows

Hình sau đây là ví dụ về giao diện một cửa sổ đăng
nhập:
Window form
7
UIT-VNUHCM
Lập trình môi trường Windows
Visual Studio .Net
controls
designer
Properties,
events
8
UIT-VNUHCM
Lập trình môi trường Windows
8
9
UIT-VNUHCM
Lập trình môi trường Windows

Project  Add Reference
10
UIT-VNUHCM

Lập trình môi trường Windows
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
11
UIT-VNUHCM
Lập trình môi trường Windows
Components API

Properties (Thuộc tính)

Như các trường thành viên

Get, set

Ví dụ Button1.Text = “Press Me”

Methods (Phương thức)

Như hàm thành viên


Bảo thành phần(component) thực hiện việc nào đó

Ví dụ Button1.Show( )

Events (Sự kiện)

Như hàm được gọi (callback function)

Nhận thông báo từ thành phần

Ví dụ Button1.Click(e)
12
UIT-VNUHCM
Lập trình môi trường Windows
Ví dụ 1
class Program
{
static void Main(string[] args)
{
Form f = new Form();
Application.Run(f);
}
}
13
UIT-VNUHCM
Lập trình môi trường Windows
Ví dụ 2
class Program
{
static void Main(string[] args)

{
MessageBox.Show("Hello World");
}
}
14
UIT-VNUHCM
Lập trình môi trường Windows
Windows Forms Application Structure

A Windows Forms application has three pieces
o
the application itself
o
forms in the application
o
controls on the form
Application
mainForm
MyForm
label1
button1
Label
“Hell…”
Button
“OK”
15
UIT-VNUHCM
Lập trình môi trường Windows
GUI Tree Structure
Panel

Button
Form
Label
GUI Internal structure
containers
Panel
Button
Form
Label
16
UIT-VNUHCM
Lập trình môi trường Windows
Form Properties
Thuộc tính Kiểu Mô tả
FormBorderStyle FormBorderStyle:
FixedDialog,
Fixed3D…
Kiểu đường viền
ControlBox bool Có system menu box?
MaximizeBox bool
MinimizeBox bool
Icon Icon
ShowInTaskBar bool
StartPosition FormStartPosition
17
UIT-VNUHCM
Lập trình môi trường Windows
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
17
18
UIT-VNUHCM
Lập trình môi trường Windows
Form Properties
Thuộc tính Kiểu Mô tả
AcceptButton
CancelButton
18
19
UIT-VNUHCM
Lập trình môi trường Windows
StartPosition - FormBorderStyle
FixedDialog : modal dialog boxes
FixedSingle : main form
None : splash screen
Sizable

CentreParent cho modal dialogs

CentreScreen cho main form hay splash screen


WindowsDefaultLocation
20
UIT-VNUHCM
Lập trình môi trường Windows
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.
21
UIT-VNUHCM
Lập trình môi trường Windows
Form Method

Show()

ShowDialog();

Hide();

Close();
22
UIT-VNUHCM
Lập trình môi trường Windows
Cơ chế hoạt động Form
23
UIT-VNUHCM
Lập trình môi trường Windows


Một event là một đối tượng biểu diễn một hành động

Ví dụ:
o
The mouse is moved or button clicked
o
The mouse is dragged
o
A graphical button is clicked
o
A keyboard key is pressed
o
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
Events
GUI
Control
User
Event
handler
event
program
message
24
UIT-VNUHCM
Lập trình môi trường Windows

User
Event Handler:
{
Get N1 and N2
Return N1+N2
Call the program
}
Program:
Put N1+N2
25
UIT-VNUHCM
Lập trình môi trường Windows
Button
Event
Handler
Call me when you
are pressed
Button
Event
Handler
I have been pressed
GUI Control
Handler 1
Handler 2
Handler 3
GUI Control
GUI Control
Handler

×