Professional Documents
Culture Documents
4 KeyboardMouseTimer
4 KeyboardMouseTimer
Ni dung
1 Keyboard
2 Mouse
3 Timer
Gii thiu
Keyboard - Mouse
Keyboard - Mouse
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.
9
4/14/2009
X l s kin bn phm
scan code,
virtual-key code
X l s kin bn phm
X l s kin bn phm
Cc s kin bn phm ch c gi n
cho ca s ang gi focus hin hnh.
X l s kin bn phm
X l s kin bn phm
C th override li cc phng thc OnKeyDown v
OnKeyUp
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.
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
X l s kin bn phm
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
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
Bi tp
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.
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.
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
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);
}
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);
}
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:
S kin MouseMove
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:
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.
S kin DoubleClick
Khi DoubleClick s pht sinh mt dy cc s
kin sau:
MouseDown
Click
MouseUp
MouseMove
MouseDown
DoubleClick
MouseUp
MouseMove
Bi tp
Timer
Multitasking
Qun l v thng bo cc trng thi
Autosave
Demo version
Game
X l s kin Timer
Timer Event
Event Method Delegate Argument
Tick OnTick EventHandler EventArgs
Lp Timer
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
Lp Timer
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());
}
//
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