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 nfilenumDo While Not EOF(nfilenum) '循环直到文件尾Line Input #nfilenum, strline '每次读取一行存放在strline变量中strall = strall & strline & vbCrLf
LoopLabel1.Caption = Label1.Caption & strall '显示得到的全部内容 End Sub2按键和鼠标键的事件跟踪 form的MouseDown事件
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
shifttest = Shift And 7Select Case shifttestCase 1Print "您按下了shift键。"Case 2Print "您按下了ctrl键。"Case 3Print "您按下了shift和ctrl键。"Case 4Print "您按下了alt键。"Case 5Print "您按下了alt和shift键。"Case 6Print "您按下了ctrl和alt键。"Case 7Print "您按下了ctrl,shift和alt键。"End Select End Sub3 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 keycodeCase 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 Sub4 KeyDown KeyPreview 属性必须为true,否则键盘效果失效
Private Sub Form_KeyDown(Button As Integer, Shift As Integer)
shiftkey = Shift And 7Select Case shiftkey Case 1Print "您按下了shift键。"Case 2Print "您按下了ctrl键。"Case 3Print "您按下了shift和ctrl键。"Case 4Print "您按下了alt键。"Case 5Print "您按下了alt和shift键。"Case 6Print "您按下了ctrl和alt键。"Case 7Print "您按下了ctrl,shift和alt键。"End Select End Sub5 确定键和取消键
在command的属性窗口中如果default属性为true,则按键按下enter会触发改按钮控件,
如果cancel的属性为true,则按下esc按键会触发该按钮控件
6 鼠标苗虚线框与实线矩形框
Dim xp1 As Single, yp1 As Single
Dim xp2 As Single, yp2 As SingleDim drawing As BooleanPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)If Not drawing Thenxp1 = X: yp1 = Yxp2 = X: yp2 = Ydrawing = TrueEnd IfEnd Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If drawing ThenDrawStyle = 2 '表示虚线DrawMode = vbInvert '表示反色覆盖原来的内容Line (xp1, yp1)-(xp2, yp2), , BLine (xp1, yp1)-(X, Y), , Bxp2 = X: yp2 = YEnd IfEnd Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If drawing ThenDrawMode = vbBlacknessDrawStyle = 0 '表示实线Line (xp1, yp1)-(X, Y), , Bdrawing = FalseEnd IfEnd 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 vbBeginDragEnd Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Drag vbEndDragEnd Sub