首页 > 代码库 > VBS脚本实例

VBS脚本实例

一、一键升级哨位台核心板程序脚本。

############################################################

Set ws=WScript.CreateObject("wscript.shell")
PATH= createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path
IP="192.168.46.73"
NAME="UpdatePacket_WJT2000-B_V3.1.0.bin"

ws.Run "C:\Users\Administrator\Desktop\1\RECUpgrade.exe"
UpdateOperate()


‘*************************************************************************

Function UpdateOperate()

WScript.Sleep 1000
ws.SendKeys IP
ws.SendKeys "{TAB 3}"
ws.SendKeys "{ENTER 2}"
WScript.Sleep 200
ws.SendKeys "{TAB 6}"
WS.SendKeys "{ENTER}"
WScript.Sleep 200
WS.SendKeys PATH&"\"&NAME
WScript.Sleep 200
‘ws.SendKeys "{ENTER}"
ws.SendKeys "{TAB 2}"
ws.SendKeys "{ENTER}"
WScript.Sleep 200
ws.SendKeys "{TAB 3}"
ws.SendKeys "{ENTER}"

End Function

‘*****************************************************************************

 

 

 

 

二、一键升级哨位台底板单片机脚本。

Set ws=WScript.CreateObject("wscript.shell")

PATH= createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path
NAME= "MCU_WJT2000-B_ShaoWei_V3.1.0.hex"

ws.Run PATH&"\"&"STC-ISP.exe"

WScript.Sleep1000
ControlMouse()
UpdateOperate()



‘*******************************************************************
Function UpdateOperate()

    ws.SendKeys"{TAB 9}"
    ws.SendKeys"{ENTER}"
    WScript.Sleep1000
    ws.SendKeys PATH&"\"&NAME
    WScript.Sleep300
    ws.SendKeys"{ENTER}"
    ws.SendKeys"{TAB 4}"
    ws.SendKeys"{ENTER}"
    
EndFunction


‘***************************************************************




‘***********************************************************************


Function ControlMouse()

‘***********************************************************************

Set mouse=New SetMouse

mouse.getpos x,y                                ‘‘获得鼠标当前位置坐标

‘MsgBox x & " " & y  


mouse.move808,584    ‘把鼠标移动到坐标
                     
WScript.Sleep200
mouse.clik"dbclick"  ‘双击

EndFunction                            

‘*****************将以下代码加入到vbs文件末就能如以上方法调用(定义SetMouse类)*******************************************************************************************

Class SetMouse
private S
private xls, wbk, module1
private reg_key, xls_code, x, y


PrivateSubClass_Initialize()
  Set xls = CreateObject("Excel.Application") 
  Set S = CreateObject("wscript.Shell")
               ‘vbs 完全控制excel
  reg_key = "HKEY_CURRENT_USER\Software\Microsoft\Office\$\Excel\Security\AccessVBOM"
  reg_key = Replace(reg_key, "$", xls.Version)
  S.RegWrite reg_key, 1, "REG_DWORD"
                ‘model 代码
  xls_code = _
   "Private Type POINTAPI : X As Long : Y As Long : End Type"                                                                                                                            & vbCrLf & _
   "Private Declare Function SetCursorPos Lib ""user32"" (ByVal x As Long, ByVal y As Long) As Long"                                                                                     & vbCrLf & _
   "Private Declare Function GetCursorPos Lib ""user32"" (lpPoint As POINTAPI) As Long"                                                                                                  & vbCrLf & _
   "Private Declare Sub mouse_event Lib ""user32"" Alias ""mouse_event"" " _
     & "(ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)" & vbCrLf & _
   "Public Function getx() As Long"                                                                                                                                             & vbCrLf & _
   "Dim pt As POINTAPI : GetCursorPos pt : getx = pt.X"                                                                                                                     & vbCrLf & _
   "End Function"                                                                                                                                                                        & vbCrLf & _
   "Public Function gety() As Long"                                                                                                                                             & vbCrLf & _
   "Dim pt As POINTAPI: GetCursorPos pt : gety = pt.Y"                                                                                                                      & vbCrLf & _
   "End Function"
  Set wbk = xls.Workbooks.Add
  Set module1 = wbk.VBProject.VBComponents.Add(1)
   module1.CodeModule.AddFromString xls_code
EndSub




‘关闭
PrivateSubClass_Terminate
  xls.DisplayAlerts = False
  wbk.Close
  xls.Quit
EndSub

‘可调用过程


PublicSub getpos( x, y) 
  x = xls.Run("getx") 
  y = xls.Run("gety") 
EndSub


PublicSub move(x,y)
  xls.Run"SetCursorPos", x, y
EndSub



PublicSub clik(keydown)
  SelectCaseUCase(keydown)
  Case"LEFT"
   xls.Run"mouse_event", &H2 + &H4, 0, 0, 0, 0
  Case"RIGHT"
   xls.Run"mouse_event", &H8 + &H10, 0, 0, 0, 0
  Case"MIDDLE"
   xls.Run"mouse_event", &H20 + &H40, 0, 0, 0, 0
  Case"DBCLICK"
    xls.Run"mouse_event", &H2 + &H4, 0, 0, 0, 0
    xls.Run"mouse_event", &H2 + &H4, 0, 0, 0, 0
  EndSelect
EndSub


EndClass


‘*************************************************************************************