1 读取文件内容 必须现在程序目录下(vb98文件夹下)建一个hero.txt
Private Sub Command1_Click()
Dim strfilename As String '文件名
Dim nfilenum As Long '文件句柄
Dim strall As String '所读取的文本文件的所有内容
Dim strline As String '在循环中存放每行的内容
'strfilename = "D:\hero.txt"
strfilename = App.Path & "\hero.txt" 'app.表示程序所在的目录下
nfilenum = FreeFile() '获取文件的句柄
Open strfilename For Input As nfilenum
Do While Not EOF(nfilenum) '循环直到文件尾
Line Input #nfilenum, strline '每次读取一行存放在strline变量中
strall = strall & strline & vbCrLf
Loop
Label1.Caption = Label1.Caption & strall
'显示得到的全部内容
End Sub
2按键和鼠标键的事件跟踪 form的MouseDown事件
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
shifttest = Shift And 7
Select Case shifttest
Case 1
Print "您按下了shift键。"
Case 2
Print "您按下了ctrl键。"
Case 3
Print "您按下了shift和ctrl键。"
Case 4
Print "您按下了alt键。"
Case 5
Print "您按下了alt和shift键。"
Case 6
Print "您按下了ctrl和alt键。"
Case 7
Print "您按下了ctrl,shift和alt键。"
End Select
End Sub
3 keydown和keyup
(1)
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyA Then Magbox"您按下了A键。"
End Sub
(2)
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Select Case keycode
Case Asc("A")
Label(0).BackColor = RGB(0, 0, 255)
Case Asc("B")
Label1(1).BackColor = RGB(0, 0, 255)
Case Asc("C")
Label(2).BackColor = RGB(0, 0, 255)
End Select
End Sub
4 KeyDown KeyPreview 属性必须为true,否则键盘效果失效
Private Sub Form_KeyDown(Button As Integer, Shift As Integer)
shiftkey = Shift And 7
Select Case shiftkey
Case 1
Print "您按下了shift键。"
Case 2
Print "您按下了ctrl键。"
Case 3
Print "您按下了shift和ctrl键。"
Case 4
Print "您按下了alt键。"
Case 5
Print "您按下了alt和shift键。"
Case 6
Print "您按下了ctrl和alt键。"
Case 7
Print "您按下了ctrl,shift和alt键。"
End Select
End Sub
5 确定键和取消键
在command的属性窗口中如果default属性为true,则按键按下enter会触发改按钮控件,
如果cancel的属性为true,则按下esc按键会触发该按钮控件
6 鼠标苗虚线框与实线矩形框
Dim xp1 As Single, yp1 As Single
Dim xp2 As Single, yp2 As Single
Dim drawing As Boolean
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not drawing Then
xp1 = X: yp1 = Y
xp2 = X: yp2 = Y
drawing = True
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If drawing Then
DrawStyle = 2 '表示虚线
DrawMode = vbInvert '表示反色覆盖原来的内容
Line (xp1, yp1)-(xp2, yp2), , B
Line (xp1, yp1)-(X, Y), , B
xp2 = X: yp2 = Y
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If drawing Then
DrawMode = vbBlackness
DrawStyle = 0 '表示实线
Line (xp1, yp1)-(X, Y), , B
drawing = False
End If
End Sub
7 实现自动拖拽界面的图片 '将图片的DrawMode属性设为1
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Source.Move (X - Source.Width / 2), (Y - Source.Height / 2)
End Sub
8 手动拖拽界面的图片
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Source.Move (X - Source.Width / 2), (Y - Source.Height / 2)
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Drag vbBeginDrag
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Drag vbEndDrag
End Sub