博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VB随笔3
阅读量:6687 次
发布时间:2019-06-25

本文共 3161 字,大约阅读时间需要 10 分钟。

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

 

转载于:https://www.cnblogs.com/jsnoway/p/3721480.html

你可能感兴趣的文章
怎么把图片转成JPG格式
查看>>
教你如何将网页上的视频下载到手机
查看>>
字节码学院|编程是未来社会的基本能力
查看>>
JDK 1.8.0_144 集合框架之LinkedHashMap
查看>>
MySQL学习之如何快速扩展数量
查看>>
细说智能指针
查看>>
ssh: Could not resolve hostname guard.: Temporary
查看>>
跨主机网络概述 - 每天5分钟玩转 Docker 容器技术(48)
查看>>
批量把本机的ssh密钥同步到远程设备以ssh无密码登入
查看>>
lmp+heartbeat+drbd
查看>>
2013年中国域名商报告:易名中国净增11.6万域名
查看>>
android ndk 的简单使用
查看>>
7月19日28家中国域名商六类国际域名注册保有量统计
查看>>
7月国内电脑分辨率TOP10 :1366*768跌破13%
查看>>
11月9日全球域名商解析新增量TOP10 :西部数码第七
查看>>
11月28日全球域名商保有量及市场份额排行榜TOP16
查看>>
.top域名总量15强:中国数据被赶超 阿里云晋身五强
查看>>
我的友情链接
查看>>
关于linux系统内核参数
查看>>
LoadRunner打印重定向状态码
查看>>