VB使用记录知识点小结

以下教程适合VB6。

Visual Basic 6.0是微软公司开发的编程设计软件,基于Windows操作系统可视化编程环境。Visual Basic 6.0还提供了窗口编辑,可直接对窗口进行编辑和预览。

换行续写代码

换行符:vbCrLf

续写符:& _,若仅为语句结尾用下划线用就行

续写符前面不能用+号拼接字符串

若批量替换字符串换行符为vb支持方式,可使用支持正则替换文本编辑器

执行替换:

\r\n" + vbCrLf & _\r\n+"

代码格式化

可使用在线工具:http://tools.jb51.net/code/vbscodeformat/

执行shell

shell直接执行

Shell "ipconfig"

WScript执行shell

Exec

用Exec执行可读取执行结果

        Dim result As String
        Dim WshShell
        Dim oExec
        Set WshShell = CreateObject("WScript.Shell")
        Set oExec = WshShell.Exec("ipconfig")
        result = oExec.StdOut.ReadAll

Run

以上执行命令都会打开黑窗口,可用Run隐藏,但其不能返回执行结果,只会返回成功失败

Public Sub xshell(ByVal Ch As String)
    Dim WshShell
    Dim oExec
    Set WshShell = CreateObject("WScript.Shell")
    oExec = WshShell.Run(path + Ch, 0)
End Sub

具体使用详解:https://www.cnblogs.com/chulia20002001/p/6931903.html

窗口跟随

根据句柄获得某窗口左上坐标跟高宽,对跟随的form设置合适坐标即可,当然为实现实时跟随,定时器不能少。

如下示例是使安卓三大按键贴在安卓模拟器窗口下面并跟随

Option Explicit
'根据句柄获取窗口
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
    '获取窗口范围
    Private Declare Function GetWindowRect Lib "user32" _
        (ByVal hwnd As Long, lpRect As RECT) As Long
        Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Dim NotepadHwnd As Long, NotepadRect As RECT

    Private Sub Form_Load()
        Me.Height = 450
        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Timer()
        Dim oneWidth As Integer
        NotepadHwnd = FindWindow("SDL_app", vbNullString) '获取窗口句柄
        If NotepadHwnd Then '如果成功
        GetWindowRect NotepadHwnd, NotepadRect '获取窗口范围
        Me.Left = (NotepadRect.Left + 7) * Screen.TwipsPerPixelX
        Me.Top = (NotepadRect.Bottom - 7) * Screen.TwipsPerPixelY '- Me.Height
        Me.Width = (NotepadRect.Right - NotepadRect.Left - 14) * Screen.TwipsPerPixelX
        oneWidth = Me.Width / 3
        menu.Width = oneWidth
        menu.Height = Me.Height
        home.Left = oneWidth
        home.Width = oneWidth
        home.Height = Me.Height
        back.Left = Me.Width - oneWidth
        back.Width = oneWidth
        back.Height = Me.Height
        Else
            Form1.kqdhj.Value = 0
        End If
    End Sub

获取用户与程序目录

当前Windows用户目录路径,例如下面就是Windows图片目录:

Environ("userprofile") + "\Pictures\"

当前程序所在目录:

App.path

文本框只允许输入数字

以下是名为fps文本框只允许输入整数例子:

    Private Sub fps_KeyPress(KeyAscii As Integer)
        '只允许数字键,退格键,小数点进行输入的处理
        Debug.Print KeyAscii

        Select Case KeyAscii
            Case vbKey0 To vbKey9, vbKeyBack        '0 - 9,BACKSPACE处理
            Case vbKeyDelete, vbKeyDecimal          '小数点处理
            If InStr(1, Text1.Text, ".") <> 0 Then KeyAscii = 0
            Case Else
            KeyAscii = 0
        End Select

    End Sub

寻找字符串返回索引

使用InStr,第一个参数从哪开始找,第二个源参数,第三个要寻找字符

InStr(1, Text1.Text, ".")

睡眠Sleep

实现类似Java中的sleep函数

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'调用
sleep 1000

多选框选中事件

value值1为选中

    Private Sub istop_Click()
        If (istop.Value = 1) Then
            zd = " --always-on-top "
        Else
            zd = ""
        End If
    End Sub

显示Winsock控件

打开菜单:工程>部件

或直接Ctrl+T呼出部件,勾选winsock即可

image-20210221184400130

获取本地IP地址

使用Winsock控件获取

Dim aa As String
Dim strLocalIP As String
Dim winIP As Object
aa = aa & "本机电脑名称:" & Environ("computername") & vbCrLf
aa = aa & "本机用户名称:" & Environ("username") & vbCrLf
Set winIP = CreateObject("MSWinsock.Winsock")
strLocalIP = winIP.localip
MsgBox aa & "本机IP:" & strLocalIP

当然,当存在多个虚拟网卡时可能不精准,以下用命令行获取本地IP

    If (Mid(strLocalIP, 1, 3) <> "192") Then
        Dim result As String
        Dim WshShell
        Dim oExec
        Set WshShell = CreateObject("WScript.Shell")
        Set oExec = WshShell.Exec("ipconfig")
        result = oExec.StdOut.ReadAll
        Dim ex As Integer
        ex = InStr(1, result, "IPv4")
        If (ex > 0) Then
            Dim lines() As String
            Dim ipv4 As String
            lines() = Split(result, vbCrLf)
            '遍历数组
            Dim i As Integer
            Dim s As String
            For i = 0 To UBound(lines) - 1
                ex = InStr(1, lines(i), "IPv4")
                If (ex > 0) Then
                    ex = InStr(1, lines(i), ":")
                    ipv4 = Trim(Right(lines(i), Len(lines(i)) - ex))
                    If (Left(ipv4, 3) = "192") Then
                        strLocalIP = ipv4
                        Label2.Caption = Label2.Caption + vbCrLf + ipv4
                    End If
                End If
            Next i

        End If
    End If

qrmaker生成二维码

使用第三方控件生成二维码,部件下载:

http://www.opdown.com/soft/85169.html#download

安装部件

下载解压完成后将其中的Qrmaker.lic 、Qrmaker.tlb、Qrmaker.ocx 、Qrmaker.oca 和Regsvr32.exe文件拷贝到你新建的工程下,然后注册.ocx文件:打开“运行”,输入注册命令对于64位的系统。在“运行”中直接输入“regsvr32 文件路径\ Qrmaker.ocx”。例如“regsvr32 / qrmaker.ocx”出现“DllRegisterServer成功”消息确定,重启计算机即可。注册成功后再“工程”的“部件”控件中选择QRmaker.ocx控件即可。

建议将

使用非常简单,拖出一个部件代码修改InputData属性再刷新即可:

    QRmaker1.InputData = "我爱你"
    QRmaker1.Refresh
目录导航
  • 换行续写代码
  • 代码格式化
  • 执行shell
    • shell直接执行
    • WScript执行shell
  • 窗口跟随
  • 获取用户与程序目录
  • 文本框只允许输入数字
  • 寻找字符串返回索引
  • 睡眠Sleep
  • 多选框选中事件
  • 显示Winsock控件
  • 获取本地IP地址
  • qrmaker生成二维码