Option Explicit'寻找窗口列表中第一个符合指定条件的顶级窗口(在vb里使用:FindWindow最常见的一个用途是获得ThunderRTMain类的隐藏窗口的句柄;该类是所有运行中vb执行程序的一部分。获得句柄后,可用api函数GetWindowText取得这个窗口的名称;该名也是应用程序的标题) Private Declare Function FindWindow _ Lib "user32" _ Alias "FindWindowA" (ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long'在窗口列表中寻找与指定条件相符的第一个子窗口 Private Declare Function FindWindowEx _ Lib "user32" _ Alias "FindWindowExA" (ByVal hWnd1 As Long, _ ByVal hWnd2 As Long, _ ByVal lpsz1 As String, _ ByVal lpsz2 As String) As Long'取得一个窗体的标题(caption)文字,或者一个控件的内容(在vb里使用:使用vb窗体或控件的caption或text属性) Private Declare Function GetWindowText _ Lib "user32" _ Alias "GetWindowTextA" (ByVal hwnd As Long, _ ByVal lpString As String, _ ByVal cch As Long) As Long'调用一个窗口的窗口函数,将一条消息发给那个窗口。除非消息处理完毕,否则该函数不会返回。SendMessageBynum, SendMessageByString是该函数的“类型安全”声明形式 Private Declare Function SendMessage _ Lib "user32" _ Alias "SendMessageA" (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long'暂停线索 Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const BM_CLICK As Integer = &HF5 'button点击消息BM_clickPrivate Sub AutoDialer1() On Error GoTo ToExit '打开错误陷阱 '------------------------------------------------ Shell "c:\windows\system32\rasphone -d 网通", vbNormalFocus DoEvents Sleep 1000 Dim h As Long, h2 As Long h = FindWindow("#32770", "连接 网通") ' If h = 0 Then Exit Sub h2 = FindWindowEx(h, 0&, "Button", vbNullString) Dim s As String * 255, l As Long l = GetWindowText(h2, s, 255) s = Left(s, l) Do Until InStr(s, "连接") Or h2 = 0 h2 = FindWindowEx(h, h2, "Button", vbNullString) l = GetWindowText(h2, s, 255) s = Left(s, l) Loop If h2 = 0 Then Exit Sub SendMessage h2, BM_CLICK, ByVal 0&, 0& '------------------------------------------------ Exit Sub '---------------- ToExit: Resume NextEnd SubPrivate Sub AutoDialer2() On Error GoTo err1 Dim pid As Long pid = Shell("c:\windows\system32\rasphone -d 网通", vbNormalFocus) DoEvents Sleep 1000 AppActivate pid SendKeys "%c", True '按键消息必须在控件返回到过程之前加以处理 Exit Suberr1: Debug.Print Err.Description Err.ClearEnd Sub Private Sub AutoDicconnect() Shell "c:\windows\system32\rasphone -h 网通" End Sub
我博客里有一篇《用HTA编写简单的应用程序》里就有关闭和启动ADSL的VBS代码啊。'进行RAS拨号连接(网通方式) Sub RasConnect_1() Dim wsh Set wsh = CreateObject("WScript.Shell") wsh.run """D:\Program Files\racer-ccn-racerpc-ha\racer.exe""", 0, True Set wsh = Nothing Sleep 5000 End Sub'进行RAS拨号连接(铁通方式) Sub RasConnect_2() Dim wsh Set wsh = CreateObject("WScript.Shell") wsh.run "rasdial 宽带连接 39511002036 217968", 0, True Set wsh = Nothing Sleep 1000 End Sub'断开RAS拨号连接(网通方式) Sub RasDisconnect_1() strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colProcessList = objWMIService.ExecQuery("select * from Win32_Process where Name='racer.exe' ") For Each objProcess In colProcessList objProcess.Terminate Next End Sub'断开RAS拨号连接(铁通方式) Sub RasDisconnect_2() Dim wsh Set wsh = CreateObject("WScript.Shell") wsh.run "rasdial /disconnect", 0, False Set wsh = Nothing Sleep 1000 End Sub
Private Declare Function FindWindow _
Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long'在窗口列表中寻找与指定条件相符的第一个子窗口
Private Declare Function FindWindowEx _
Lib "user32" _
Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long'取得一个窗体的标题(caption)文字,或者一个控件的内容(在vb里使用:使用vb窗体或控件的caption或text属性)
Private Declare Function GetWindowText _
Lib "user32" _
Alias "GetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long'调用一个窗口的窗口函数,将一条消息发给那个窗口。除非消息处理完毕,否则该函数不会返回。SendMessageBynum, SendMessageByString是该函数的“类型安全”声明形式
Private Declare Function SendMessage _
Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long'暂停线索
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const BM_CLICK As Integer = &HF5 'button点击消息BM_clickPrivate Sub AutoDialer1() On Error GoTo ToExit '打开错误陷阱 '------------------------------------------------
Shell "c:\windows\system32\rasphone -d 网通", vbNormalFocus DoEvents
Sleep 1000 Dim h As Long, h2 As Long h = FindWindow("#32770", "连接 网通") ' If h = 0 Then Exit Sub
h2 = FindWindowEx(h, 0&, "Button", vbNullString) Dim s As String * 255, l As Long l = GetWindowText(h2, s, 255)
s = Left(s, l) Do Until InStr(s, "连接") Or h2 = 0
h2 = FindWindowEx(h, h2, "Button", vbNullString)
l = GetWindowText(h2, s, 255)
s = Left(s, l)
Loop If h2 = 0 Then Exit Sub
SendMessage h2, BM_CLICK, ByVal 0&, 0& '------------------------------------------------
Exit Sub '----------------
ToExit: Resume NextEnd SubPrivate Sub AutoDialer2() On Error GoTo err1 Dim pid As Long pid = Shell("c:\windows\system32\rasphone -d 网通", vbNormalFocus) DoEvents
Sleep 1000 AppActivate pid
SendKeys "%c", True '按键消息必须在控件返回到过程之前加以处理 Exit Suberr1:
Debug.Print Err.Description
Err.ClearEnd Sub
Private Sub AutoDicconnect()
Shell "c:\windows\system32\rasphone -h 网通"
End Sub
Sub RasConnect_1()
Dim wsh
Set wsh = CreateObject("WScript.Shell")
wsh.run """D:\Program Files\racer-ccn-racerpc-ha\racer.exe""", 0, True
Set wsh = Nothing
Sleep 5000
End Sub'进行RAS拨号连接(铁通方式)
Sub RasConnect_2()
Dim wsh
Set wsh = CreateObject("WScript.Shell")
wsh.run "rasdial 宽带连接 39511002036 217968", 0, True
Set wsh = Nothing
Sleep 1000
End Sub'断开RAS拨号连接(网通方式)
Sub RasDisconnect_1()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("select * from Win32_Process where Name='racer.exe' ")
For Each objProcess In colProcessList
objProcess.Terminate
Next
End Sub'断开RAS拨号连接(铁通方式)
Sub RasDisconnect_2()
Dim wsh
Set wsh = CreateObject("WScript.Shell")
wsh.run "rasdial /disconnect", 0, False
Set wsh = Nothing
Sleep 1000
End Sub