You are on page 1of 51

X l cc s kin nhp liu

Ni dung

1 Keyboard

2 Mouse

3 Timer

4 Click to add Title


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.
B nh thi gian: Windows cung cp c
ch ny truyn thng vi ng dng
theo nh k.
ng dng ch cn khai bo mt b nh thi gian vi
mt khong thi gian cho trc.
Khi ng dng hot ng th h thng s truyn mt
tn hiu cho ng dng theo tng khong thi gian
nh k c khai bo.
3 Lp trnh mi trng windows
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 Lp trnh mi trng windows


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.

5 Lp trnh mi trng windows


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, F1-
F12..).
Nhn Shift hoc Ctrl hoc Alt hoc mt t hp no
ca ba phm ny vi cc phm k t.
Khi phm trn bn phm c g, nh hay
gi th cc thng ip tng ng s c
gi n ca s ang c focus.

6
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.

7 Lp trnh mi trng windows


4/14/2009

M phm o Virtual Keycode


Windows gn cho mi phm trn bn phm
mt m, gi l m phm o.

M phm o l m khng ph thuc thit b,


thay th cho m qut (scan code) ph thuc
loi bn phm v nh sn xut.

Cc m phm o c nh ngha di dng


cc macro, bt u bng VK_.
V d m phm o cho cc phm ESC, Enter, F1 hay
Alt l VK_ESCAPE, VK_RETURN, VK_F1 v
VK_ALT.
8
4/14/2009

M phm o Virtual Keycode


Cn phi phn bit gia k t nhn c khi
n phm v m phm o.

V d khi phm A c nhn, th k c th nhn c


k t a hoc A hoc khng, tu thuc vo trng thi
phm CAPSLOCK, cc phm Shift, Alt, Ctrl c c
nhn hay khng.

9
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 Lp trnh mi trng windows


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 Lp trnh mi trng windows


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 Lp trnh mi trng windows


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 Lp trnh mi trng windows


X l s kin bn phm

Khi nhn hoc th phm:

Event Method Delegate Argument


KeyDown OnKeyDown KeyEventHandler KeyEventArgs
KeyUp OnKeyUp KeyEventHandler KeyEventArgs

4/14/2009 Lp trnh mi trng windows 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 Lp trnh mi trng windows
4/14/2009

X l s kin bn phm
Cng c th x l cc s kin nhn v th
phm trn cc control bng cch nh ngha
cc phng thc tng ng.

void MyKeyDownHandler(object objSender, KeyEventArgs kea)


{}
void MyKeyUpHandler(object objSender, KeyEventArgs kea)
{}

cntl.KeyDown += new KeyEventHandler (MyKeyDownHandler);


cntl.KeyUp += new KeyEventHandler (MyKeyUpHandler);

16 Lp trnh mi trng windows


4/14/2009

X l s kin bn phm
KeyEventArgs Propeties
Type Propety Accesibility Comments
Keys KeyCode get Identifies the key
Keys Modifiers get Identifies shift states
Keys KeyData get Combination of KeyCode and
Modifies
bool Shift get Set to true if Shift key is
pressed
bool Control get Set to true if Ctrl key is pressed
bool Alt get Set to true if Alt key is pressed
bool Handled get/set Set by event handler (initially
false)
int KeyValue get Return KeyData in the form of
an integer.
17 Lp trnh mi trng windows
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 Lp trnh mi trng windows
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 Lp trnh mi trng windows


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:

Lp trnh mi trng windows 20


4/14/2009

Keys enumeration
Keys Enumeration (letters)
Member Value Member Value
A 65 N 78
B 66 O 79
C 67 P 80
D 68 Q 81
E 69 R 82
F 70 S 83
G 71 T 84
H 72 U 85
1 73 V 86
J 74 W 87
K 75 X 88
L 76 Y 89
M 77 21 Z Lp trnh mi trng windows
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 135
22 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 Lp trnh mi trng windows
4/14/2009

S kin KeyPress

S kin KeyPress pht sinh khi mt


phm k t c nhn.

protected override void OnKeyPress(KeyPressEventArgs kea)


{
if (kea.KeyChar == 'x')
Close () ;
}

24 Lp trnh mi trng windows


4/14/2009

Bi tp

25 Lp trnh mi trng windows


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 Lp trnh mi trng windows


4/14/2009

X l s kin chut
Cc s kin chut s c gi n cho:
Ca s hin ang cha con tr chut.
Hoc ca s ang capture chut.
C hai loi s kin v chut:
Client area messages
Cc s kin chut xy ra khi chut ang vng
client ca ca s.
Non-client area messages
Cc s kin chut xy ra khi chut ang cc
vng nh border, menu bar, title bar, scroll bar,
window menu, minimize button, v maximize
button.

27 Lp trnh mi trng windows


Cc s kin c bn

C 4 s kin chut c bn:


Control Events (Selection)

Event Method Delegate Argument

MouseDown OnMouseDown MouseEventHandler MouseEventArgs

MouseUp OnMouseUp MouseEventHandler MouseEventArgs

MouseMove OnMouseMove MouseEventHandler MouseEventArgs

MouseWheel OnMouseWheel MouseEventHandler MouseEventArgs

4/14/2009 Lp trnh mi trng windows 28


MouseEventArgs
Lp MouseEventArgs c 5 thuc tnh read-only
MouseEventArgs Properties

Type Property Accessibility Description

Int X get The horizontal position of the


mouse
Int Y get The vertical position of the
mouse
MouseButtons Button get The muose button or buttons

Int Clicks get Returns 2 for a double-click

int Delta get Mouse wheel movement

4/14/2009 Lp trnh mi trng windows 29


Thuc tnh Button

MouseButtons Enumeration
Member Value
None 0x00000000

Left 0x00100000

Right 0x00200000

Middle 0x00400000
XButton1 0x00800000
XButton2 0x01000000

(mea.Button == MouseButtons.Right)
4/14/2009 Lp trnh mi trng windows 30
4/14/2009

V d Test Mouse Button

protected override void OnMouseClick(MouseEventArgs mea)


{
base.OnMouseClick(mea);
if (mea.Button == MouseButtons.Left)
MessageBox.Show("Nhan chuot trai");
if (mea.Button == MouseButtons.Right)
MessageBox.Show("Nhan chuot phai");
if (mea.Button == MouseButtons.Middle)
MessageBox.Show("Nhan chuot giua");

31 Lp trnh mi trng windows


4/14/2009

S kin MouseDown
S kin MouseDown c pht sinh khi
ngi dng nhn mt nt ca chut.
x l s kin MouseDown ta
override phng thc OnMouseDown
V d:
protected override void OnMouseDown(MouseEventArgs mea)
{
MessageBox.Show("Ban vua nhan chuot " + mea.Button);
}

32 Lp trnh mi trng windows


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 Lp trnh mi trng windows


4/14/2009

S kin MouseMove
S kin MouseMove c pht sinh khi ngi dng
di chuyn chut.
x l s kin MouseMove ta override phng
thc OnMouseMove
V d:

protected override void OnMouseMove(MouseEventArgs mea)


{
//Ve mot duong thang tu toa do (0,0) den toa do chuot di chuyen
Graphics g = CreateGraphics();
Pen pen = new Pen(System.Drawing.Color.Blue);
g.DrawLine(pen, 0, 0, mea.X, mea.Y);
}

34 Lp trnh mi trng windows


4/14/2009

S kin MouseMove

35 Lp trnh mi trng windows


4/14/2009

Enter Hover - Leave

36 Lp trnh mi trng windows


4/14/2009

S kin MouseWheel
S kin MouseWheel c pht sinh khi
ngi dng scroll chut.
x l s kin MouseWheel ta override
phng thc OnMouseWheel
V d:

protected override void OnMouseWheel(MouseEventArgs mea)


{
if (mea.Delta>0)
MessageBox.Show("Ban vua scroll chuot len", "Thong bao");
else
MessageBox.Show("Ban vua scroll chuot xuong", "Thong bao");
}

37 Lp trnh mi trng windows


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 Lp trnh mi trng windows


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

protected override void OnDoubleClick(EventArgs ea)


{

}

39 Lp trnh mi trng windows


4/14/2009

Bi tp

40 Lp trnh mi trng windows


4/14/2009

Timer
Multitasking
Qun l v thng bo cc trng thi
Autosave
Demo version
Game

41 Lp trnh mi trng windows


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 trnh mi trng windows


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 Lp trnh mi trng windows 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 trnh mi trng windows


Lp Timer

Lp Timer c 2 thuc tnh:

Timer Properties
Type Property Accessibility Description
int Interval get/set Tick time in
milliseconds
bool Enabled get/set Set to true if
timer is running

4/14/2009 Lp trnh mi trng windows 45


4/14/2009

Lp Timer

Cc phng thc ca Timer :


void Start()
void Stop()

46 Lp trnh mi trng windows


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 trnh mi trng windows


4/14/2009

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 Lp trnh mi trng windows
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 trnh mi trng windows
4/14/2009

Lp Timer (V d 2)
void TimerOnTick(object obj, EventArgs ea)
{
Random rand = new Random();
int x1 = rand.Next(ClientSize.Width);
int x2 = rand.Next(ClientSize.Width);
int y1 = rand.Next(ClientSize.Height);
int y2 = rand.Next(ClientSize.Height);
Color color = Color.FromArgb(rand.Next(256),
rand.Next(256), rand.Next(256));
Graphics grfx = CreateGraphics();
grfx.FillRectangle(new SolidBrush(color), Math.Min(xl,
x2), Math.Min(yl, y2), Math.Abs(x2-xl),
Math.Abs(y2-yl) );
grfx.Dispose() ;
}
}
50 Lp trnh mi trng windows

You might also like