Professional Documents
Culture Documents
Xử Lý Su Kien Phim - Chuot Trong Winform C#
Xử Lý Su Kien Phim - Chuot Trong Winform C#
Ni dung
Keyboard
Mouse
Timer
4/14/2009
Gii thiu
Tm hiu cc thng ip c pht sinh
t bn phm hay t thit b chut vit
cc x l tng ng vi tng thit b.
4/14/2009
Keyboard - Mouse
Bn phm v chut l hai thit b nhp
liu quan trng nht ca my tnh.
Hu ht cc chc nng ca Windows
u h tr dng bn phm v chut.
4/14/2009
Keyboard - Mouse
Bn phm v chut c x l thng qua
c ch thng ip ca Windows.
Mi s kin i vi bn phm v chut
c Windows gi n chng trnh thng
qua cc thng ip.
4/14/2009
Keyboard
Khi nhn phm c th xy ra cc trng
hp sau:
Nhn mt phm k t.
Nhn mt phm iu khin (cc phm ESC, Enter, F1F12..).
Nhn Shift hoc Ctrl hoc Alt hoc mt t hp no
ca ba phm ny vi cc phm k t.
4/14/2009
Keyboard
Cc phm c nhn c phn thnh hai
nhm chnh:
Nhm cc phm h thng (system keys): l cc phm
c nhn vi phm Alt.
Nhm cc phm thng (nonsystem keys): khi phm
Alt khng c nhn.
Thng th cc phm h thng c
Windows x l v dch thnh cc s kin
tng ng.
4/14/2009
4/14/2009
4/14/2009
X l s kin bn phm
M hnh x l s kin bn phm ca
Windows
scan code,
virtual-key code
10
4/14/2009
X l s kin bn phm
Khi ngi dng nhn hoc nh mt phm
bt k t bn phm, cc driver bn phm
s nhn c m qut (scan code) ca
phm tng ng.
M qut ny s c chuyn thnh m
phm o (Virtual keycode) v mt thng
ip bn phm tng ng (bao gm c
scan code, virtual keycode v mt s
thng tin khc) s c gi n cho
System message queue.
11
4/14/2009
X l s kin bn phm
Cc s kin bn phm ch c gi n
cho ca s ang gi focus hin hnh.
H thng gi hai s kin bn phm khc
nhau khi ngi dng nhn phm v nh
phm
12
4/14/2009
X l s kin bn phm
Cc phm c nhn c chia lm 4
nhm sau:
Toggle keys: Caps Lock, Num Lock, Scroll Lock
Shift keys: Shift, Ctrl, Alt
Noncharacter keys: cc phm chc nng nh
cc phm di chuyn, Pause, Delete
Character keys: cc phm k t, phm s,
13
X l s kin bn phm
Khi nhn hoc th phm:
Event
Method
Delegate
Argument
KeyDown
OnKeyDown KeyEventHandler
KeyEventArgs
KeyUp
OnKeyUp
KeyEventArgs
4/14/2009
KeyEventHandler
14
4/14/2009
X l s kin bn phm
C th override li cc phng thc OnKeyDown v
OnKeyUp
protected override void OnKeyDown (KeyEventArgs kea)
{
.
}
protected override void OnKeyUp (KeyEventArgs kea)
{
.
}
15
X l s kin bn phm
4/14/2009
X l s kin bn phm
4/14/2009
KeyEventArgs Propeties
Type
Propety
Accesibility
Comments
Keys
KeyCode
get
Keys
Modifiers
get
Keys
KeyData
get
bool
Shift
get
bool
Control
get
bool
Alt
get
bool
Handled
get/set
int
KeyValue
get
4/14/2009
X l s kin bn phm
Mi khi phm c nhn hoc th th
pht sinh s kin, km theo mt tham
s KeyEventArgs c cc thuc tnh nh
sau:
Keycode: Cho bit phm no c nhn hoc
th, cc phm ny c th bao gm cc phm
Shift, Ctrl, Alt
Modifiers: Cho bit trng thi ca cc phm
Shift, Ctrl, Alt trong lc nhn phm hay th
phm
Keydata: Kt hp gia hai thuc tnh Keycode
v Modifiers
18
4/14/2009
X l s kin bn phm
V d: khi ngi dng nhn phm Shift
v phm D sau th phm D v phm
Shift s pht sinh lin tip cc s kin
sau:
19
Keys enumeration
Kiu Keys c nh ngha lit k
tt c cc phm. Bng lit k gi tr
26 k t Latin c m t trong bng
sau:
20
4/14/2009
Keys enumeration
Keys Enumeration (letters)
Member
Value
Member
Value
65
78
66
79
67
80
68
81
69
82
70
83
71
84
72
85
73
86
74
87
75
88
76
89
77
21
90
4/14/2009
Keys enumeration
Keys Enumeration (function keys)
Member
Value
Member
Value
F1
112
F13
124
F2
113
F14
125
F3
114
F15
126
F4
115
F16
127
F5
116
F17
128
F6
117
F18
129
F7
118
F19
130
F8
119
F20
131
F9
120
F21
132
F10
121
F22
133
F11
122
F23
134
F12
123
F24
22
135
Lp trnh mi trng windows
4/14/2009
V d
class MyForm:Form
{
public MyForm()
{
this.Text = "Test Keyboard";
this.KeyDown+=new KeyEventHandler(MyForm_KeyDown);
this.KeyUp+=new KeyEventHandler(MyForm_KeyUp);
}
void MyForm_KeyDown(Object sender, KeyEventArgs kea)
{
//if (kea.KeyCode == Keys.X) Application.Exit();
Console.WriteLine("KeyDown");
}
void MyForm_KeyUp(Object sender, KeyEventArgs kea)
{
Console.WriteLine("KeyUp");
}
}
23
4/14/2009
S kin KeyPress
S kin KeyPress pht sinh khi mt
phm k t c nhn.
24
4/14/2009
Bi tp
25
4/14/2009
X l s kin chut
V c bn Windows h tr cc loi
thit b chut c mt nt, hai v ba
nt, ngoi ra Windows cn c th
dng thit b khc nh joystick hay
bt v bt chc thit b chut.
Cc thng ip c to t chut rt
khc vi thng ip ca bn phm:
chut di chuyn qua ca s
hay kch vo trong ca s,
thm ch c trong trng hp ca s khng c
kch hot hay khng nhn c s quan tm.
26
X l s kin chut
4/14/2009
Cc s kin c bn
C 4 s kin chut c bn:
Control Events (Selection)
Event
Method
Delegate
MouseDown
OnMouseDown
MouseEventHandler
MouseEventArgs
MouseUp
OnMouseUp
MouseEventHandler
MouseEventArgs
MouseMove
OnMouseMove
MouseEventHandler
MouseEventArgs
MouseEventArgs
4/14/2009
Argument
28
MouseEventArgs
Lp MouseEventArgs c 5 thuc tnh read-only
MouseEventArgs Properties
Type
Int
get
Int
get
MouseButtons Button
get
Int
Clicks
get
int
Delta
get
4/14/2009
29
Value
None
0x00000000
Left
0x00100000
Right
0x00200000
Middle
0x00400000
XButton1
0x00800000
XButton2
0x01000000
(mea.Button
4/14/2009
== MouseButtons.Right)
Lp trnh mi trng windows
30
4/14/2009
31
S kin MouseDown
4/14/2009
32
4/14/2009
S kin MouseUp
S kin MouseUp c pht sinh khi
ngi dng nh mt nt ca chut.
x l s kin MouseUp ta override
phng thc OnMouseUp
V d:
protected override void OnMouseUp(MouseEventArgs mea)
{
MessageBox.Show("Ban vua nha chuot " + mea.Button);
}
33
S kin MouseMove
4/14/2009
Graphics g = CreateGraphics();
Pen pen = new Pen(System.Drawing.Color.Blue);
g.DrawLine(pen, 0, 0, mea.X, mea.Y);
}
34
S kin MouseMove
35
4/14/2009
4/14/2009
36
4/14/2009
S kin MouseWheel
S kin MouseWheel c pht sinh khi
ngi dng scroll chut.
else
MessageBox.Show("Ban vua scroll chuot xuong", "Thong bao");
37
4/14/2009
S kin Click
S kin Click pht sinh khi mt phm
bt k ca chut c nhn.
S kin ny pht sinh km theo tham
s EventArgs, tham s ny khng cha
thng tin v trng thi ca nt chut
c nhn cng nh v tr ca con tr
chut khi nhn.
protected override void OnClick(EventArgs ea)
{
38
4/14/2009
S kin DoubleClick
Khi DoubleClick s pht sinh mt dy cc s
kin sau:
MouseDown
Click
MouseUp
MouseMove
MouseDown
DoubleClick
MouseUp
MouseMove
}
39
4/14/2009
Bi tp
40
4/14/2009
Timer
Multitasking
Qun l v thng bo cc trng thi
Autosave
Demo version
Game
41
4/14/2009
X l s kin Timer
Theo l thuyt thng ip thi gian do
Windows cung cp l chnh xc n
mili giy nhng thc t khng hon
ton nh vy.
S chnh xc cn ph thuc vo ng
h ca h thng v cc hot ng
hin thi ca chng trnh.
42
Lp Timer
C th to i tng Timer bng cch
dng constructor mc nh nh sau:
Timer timer = new Timer();
Timer c mt s kin:
Timer Event
Event
Method
Delegate
Argument
Tick
OnTick
EventHandler
EventArgs
4/14/2009
43
4/14/2009
Lp Timer
Chng ta c th nh ngha s kin cho
timer nh sau:
void TimerOnTick(object obj, EventArgs ea)
{
.
}
ng k s kin:
Timer.Tick += new EventHandler(TimerOnTick)
44
Lp Timer
Lp Timer c 2 thuc tnh:
Timer Properties
Type Property Accessibility Description
int
Interval
bool
Enabled get/set
4/14/2009
get/set
Tick time in
milliseconds
Set to true if
timer is running
45
4/14/2009
Lp Timer
Cc phng thc ca Timer :
void Start()
void Stop()
46
4/14/2009
Lp Timer (V d 1)
using System;
using System.Drawing;
using System.Windows.Forms;
public class CloselnFive: Form
{
public static void Main()
{
Application.Run(new CloselnFive());
}
//
47
Lp Timer (V d 1)
public class CloselnFive: Form
{
public CloselnFive(){
Text = "Closing in Five Minutes";
Timer timer = new Timer();
timer.Interval = 5 * 60 * 1000;
timer.Tick += new EventHandler(TimerOnTick);
timer.Enabled = true;
}
void TimerOnTick(object obj, EventArgs ea){
Timer timer = (Timer) obj;
timer.Stop();
timer.Tick -= new EventHandler(TimerOnTick);
Close () ;
}
}
48
4/14/2009
4/14/2009
Lp Timer (V d 2)
using System;
using System.Drawing;
using System.Windows.Forms;
class RandomRectangle: Form{
public static void Main(){
Application.Run(new RandomRectangle());
}
public RandomRectangle(){
Text = "Random Rectangle";
Timer timer = new Timer();
timer.Interval = 1;
timer.Tick += new EventHandler(TimerOnTick);
timer.Start();
}
//
49
Lp Timer (V d 2)
4/14/2009