事件處理
處理鍵盤事件
按鍵 |
KeyCode |
KeyCode的常數符號 |
按鍵 |
KeyCode |
KeyCode的常數符號 |
Backspace |
8 |
VbKeyBack |
F1 |
112 |
VbKeyF1 |
Tab |
9 |
VbKeyTab |
F2 |
113 |
VbKeyF2 |
Enter |
13 |
VbKeyReturn |
F3 |
114 |
VbKeyF3 |
Esc |
27 |
VbKeyEscape |
F4 |
115 |
VbKeyF4 |
PageUp |
33 |
VbKeyPageUp |
F5 |
116 |
VbKeyF5 |
PageDown |
34 |
VbKeyPageDown |
F6 |
117 |
VbKeyF6 |
End |
35 |
VbKeyEnd |
F7 |
118 |
VbKeyF7 |
Home |
36 |
VbKeyHome |
F8 |
119 |
VbKeyF8 |
�] |
37 |
VbKeyLeft |
F9 |
120 |
VbKeyF9 |
�^ |
38 |
VbKeyUp |
F10 |
121 |
VbKeyF10 |
�_ |
39 |
VbKeyRight |
|
�` |
40 |
VbKeyDown |
Insert |
45 |
VbKeyInsert |
Delete |
46 |
VbKeyDelete |
用KeyDwon事件為實例學習:利用上下左右鍵來移動飛機
|
Private
Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight Then
Image1.Left = Image1.Left + Image1.Width
ElseIf KeyCode = vbKeyLeft Then
Image1.Left = Image1.Left - Image1.Width
ElseIf KeyCode = vbKeyDown Then
Image1.Top = Image1.Top + Image1.Height
ElseIf KeyCode = vbKeyUp Then
Image1.Top = Image1.Top - Image1.Height
ElseIf KeyCode = vbKeyEnd And Shift
= 2 Then
Unload Me '
下載表單,結束程式
End If
End Sub |
加入一Image控制元件,並將其Picture屬性設為某一圖案 |
Shift:是否按下 Shift Ctrl Alt
三種按鍵的狀態,按加Shift 1,按Ctrl加2,按Alt加4,例如按下Alt + Ctrl
參數值等於6
本例中最後一個ElseIf中的vbKeyEnd And Shift = 2就是按下End + Ctrl |
處理滑鼠事件
實例學習:簡易的畫線程式
|
Private
Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
PSet (X, Y) ' 在 (X, Y) 的位置畫一點
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Line -(X, Y) ' 從前一個 PSet 位置畫線段到這一點
End Sub |
實例研究:畫圓程式
按下滑鼠按鈕決定圓心,移動滑鼠決定半徑,放開滑鼠按鈕把圓畫出
|
Private
Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
CurrentX = X
CurrentY = Y
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As
Single, Y As Single)
R = ((X - CurrentX) ^ 2 + (Y - CurrentY) ^ 2) ^ 0.5
Circle (CurrentX, CurrentY), R
End Sub |
註:求兩點距離公式((X2-X1)^2+(Y2-Y1)^2))^0.5
進階處理滑鼠事件
滑鼠的Button引數
Button 引數 |
常數 |
說明 |
111(7) |
vbMiddleButton+vbRightButton+vbLeftButton |
三個鍵都按著 |
110(6) |
vbMiddleButton+vbRightButton |
按著中、右鍵 |
101(5) |
vbMiddleButton +vbLeftButton |
按著中、左鍵 |
100(4) |
vbMiddleButton |
按著中鍵 |
011(3) |
vbRightButton+vbLeftButton |
按著左右鍵 |
010(2) |
vbRightButton |
按著右鍵 |
001(1) |
vbLeftButton |
按著左鍵 |
000(0) |
無 |
沒有按著任何鍵 |
實列研究:左鍵畫線,右鍵畫圓
|
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X
As Single, Y As Single)
If Button
= 1 Then
PSet (X, Y) ' 在 (X,
Y) 的位置畫一點
ElseIf Button = 2 Then
CurrentX = X
CurrentY = Y
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X
As Single, Y As Single)
If Button
= 1 Then
Line -(X, Y) ' 從前一個 PSet 位置畫線段到這一點
ElseIf Button = 2 Then
R = ((X - CurrentX) ^ 2 + (Y - CurrentY) ^ 2) ^ 0.5
Circle (CurrentX, CurrentY), R
End If
End Sub |
滑鼠的拖曳(Drang and Drop)
DrangMode屬性:設為【1-自動】時,表示它(物件)可以用滑鼠來拖曳,而且在拖曳的過程中換看見一個相同大小的黑框
如果在程式敘述中可以這樣寫:物件名.DrangMode=True
DrangIcon屬性:拖曳物件時會看到黑色的框框,這是預設值,可以透過DrangIcon屬性來指定一個『拖曳圖示』
DrangDrop事件:討論DrangDrop事件有兩個名詞很重要
名詞 |
說明 |
Source |
指被拖曳的物件,【來源物件】 |
Targer |
接受『放下的物件』,【目的物件】 |
實例演練
所有的PictureBox物件外框都設為無
DrangDrop拖曳模式設為自動
|
Private Sub Form_Load()
Picture1.DragIcon
= Picture1.Picture '設定拖曳時的圖形與原物件相同
Picture2.DragIcon
= Picture2.Picture
Picture3.DragIcon = Picture3.Picture
End
Sub
Private Sub Picture4_DragDrop(Source As Control, X As Single, Y As
Single)
Source.Visible =
False '將拖曳到垃圾桶上的物件隱藏起來,形成消失的效果
End
Sub |
本網頁由昱得資訊工作室製作(C)
Copyright Valor-Studio since in 1993
|