Professional Documents
Culture Documents
Ni dung
Keyboard
2 3
4
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 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
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
KeyDown KeyUp
Method
Delegate
Argument
KeyEventArgs KeyEventArgs
4/14/2009
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
X l s kin bn phm
4/14/2009
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) {}
X l s kin bn phm
KeyEventArgs Propeties
4/14/2009
Type
Keys Keys Keys
Propety
KeyCode Modifiers KeyData
Accesibility
get get get
Comments
Identifies the key Identifies shift states Combination of KeyCode and Modifies
bool
bool
Shift
Control
get
get
bool
bool int
Alt
Handled KeyValue
get
get/set get
17
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
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 A
B C D E F G
Value 65
66 67 68 69 70 71
Member N
O P Q R S T
Value 78
79 80 81 82 83 84
H 1 J
K L M
72 73 74
75 76 77
21
U V W
X Y Z
85 86 87
88 89
Lp trnh mi trng windows
90
4/14/2009
Keys enumeration
Keys Enumeration (function keys) Member F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 Value 112 113 114 115 116 117 118 119 120 121 Member F13 F14 F15 F16 F17 F18 F19 F20 F21 F22 Value 124 125 126 127 128 129 130 131 132 133
F11
F12
122
123
22
F23
F24
134
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 Lp trnh mi trng windows
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 Lp trnh mi trng windows
X l s kin chut
4/14/2009
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 MouseDown Method OnMouseDown Delegate MouseEventHandler Argument MouseEventArgs
MouseUp MouseMove
OnMouseUp OnMouseMove
MouseEventHandler MouseEventHandler
MouseEventArgs MouseEventArgs
MouseEventArgs
4/14/2009
28
MouseEventArgs
Lp MouseEventArgs c 5 thuc tnh read-only
MouseEventArgs Properties Type Int Int Property Accessibility Description X Y get get get The horizontal position of the mouse The vertical position of the mouse The muose button or buttons
MouseButtons Button
Int int
4/14/2009
Clicks Delta
get get
None Left
Right Middle XButton1
0x00000000 0x00100000
0x00200000 0x00400000 0x00800000
XButton2
(mea.Button
4/14/2009
0x01000000
== MouseButtons.Right)
Lp trnh mi trng windows 30
4/14/2009
31
S kin MouseDown
4/14/2009
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
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
x l s kin MouseMove ta override phng thc OnMouseMove V d:
4/14/2009
}
34 Lp trnh mi trng windows
S kin MouseMove
4/14/2009
35
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) { }
Lp trnh mi trng windows
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
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
4/14/2009
EventHandler
Lp trnh mi trng windows
EventArgs
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 get/set Tick time in milliseconds
bool
Enabled get/set
4/14/2009
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 trnh mi trng windows
Lp Timer (V d 2)
4/14/2009
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