1) 用API函数keybd_event可以模拟任何按键: 在模块中定义Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal Scan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
例如{Windows]+"R"的组合 Call keybd_event(91, 0, 0, 0) Call keybd_event(82, 0, 0, 0) ({Windows]键码为91,"R"键码为82,注意第二句也不能用SENDKEYS语句!) ===================================================================== 2) '声明API函数 '引用了未公开的DLL文件shell32 Private Declare Function SHRunDialog Lib "shell32" _ Alias "#61" (ByVal hOwner As Long, ByVal UnknownP1 _ As Long, ByVal UnknownP2 As Long, ByVal szTitle _ As String, ByVal szPrompt As String, ByVal uFlags _ As Long) As Long '通用调用运行对话框过程 '参数Title表示对话框的标题,参数Description表示对话框中的描述文字 Public Sub ShowRunDialog(ByRef CallingForm As Form, _ Optional Title As String, _ Optional Description As String) If Title = "" Then Title = "Run" If Description = "" Then Description = _ "Type the name of a program to open, " & _ "then click OK when finished." SHRunDialog CallingForm.hWnd, 0, 0, _ Title, Description, 0 End '调用例程 Call ShowRunDialog(Me, "Run Dialog", "Choose a program and press OK") ============================================================================== 3) shell "c:\a.exe" ============================================================================== 4) Declare Function ShellExecute Lib "shell32.dll" Alias _ "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _ As String, ByVal lpFile As String, ByVal lpParameters _ As String, ByVal lpDirectory As String, ByVal nShowCmd _ As Long) As Long =============================================================================== 5) createprocessOption ExplicitPrivate Type STARTUPINFO cb As Long lpReserved As String lpDesktop As String lpTitle As String dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As Long End TypePrivate Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessID As Long dwThreadID As Long End TypePrivate Declare Function dcWaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Declare Function dcCreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal _ lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _ lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _ ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _ ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _ lpStartupInfo As STARTUPINFO, lpProcessInformation As _ PROCESS_INFORMATION) As LongPrivate Declare Function dcCloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As LongPrivate Declare Function dcGetExitCodeProcess Lib "kernel32" Alias "GetExitCodeProcess" (ByVal hProcess As Long, lpExitCode As Long) As LongPrivate Const NORMAL_PRIORITY_CLASS = &H20& Private Const INFINITE = -1& Public Function ExecCmd(cmdline$) Dim proc As PROCESS_INFORMATION Dim start As STARTUPINFO Dim ret As Long
就是 shell 函数直接用就可以shell 后面加程序路径
thank u.
Shell 函数:
执行一个可执行文件,返回一个 Variant (Double),如果成功的话,代表这个程序的任务 ID,若不成功,则会返回 0。
语法
Shell(pathname[,windowstyle])
Shell 函数的语法含有下面这些命名参数:
部分 描述
pathname 必要参数。Variant (String),要执行的程序名,以及任何必需的参数或命令行变量,可能还包括目录或文件夹,以及驱动器。
Windowstyle 可选参数。Variant (Integer),表示在程序运行时窗口的样式。如果 windowstyle 省略,则程序是以具有焦点的最小化窗口来执行的。
windowstyle 命名参数有以下这些值:
常量 值 描述
vbHide 0 窗口被隐藏,且焦点会移到隐式窗口。
VbNormalFocus 1 窗口具有焦点,且会还原到它原来的大小和位置。
VbMinimizedFocus 2 窗口会以一个具有焦点的图标来显示。
VbMaximizedFocus 3 窗口是一个具有焦点的最大化窗口。
VbNormalNoFocus 4 窗口会被还原到最近使用的大小和位置,而当前活动的窗口仍然保持活动。
VbMinimizedNoFocus 6 窗口会以一个图标来显示。而当前活动的的窗口仍然保持活动。
说明
如果 Shell 函数成功地执行了所要执行的文件,则它会返回程序的任务 ID。任务 ID 是一个唯一的数值,用来指明正在运行的程序。如果 Shell 函数不能打开命名的程序,则会产生错误。
注意 缺省情况下,Shell 函数是以异步方式来执行其它程序的。也就是说,用 Shell 启动的程序可能还没有完成执行过程,就已经执行到 Shell 函数之后的语句。
用API函数keybd_event可以模拟任何按键:
在模块中定义Public Declare Sub keybd_event Lib
"user32" (ByVal bVk As Byte, ByVal Scan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
例如{Windows]+"R"的组合
Call keybd_event(91, 0, 0, 0)
Call keybd_event(82, 0, 0, 0)
({Windows]键码为91,"R"键码为82,注意第二句也不能用SENDKEYS语句!)
=====================================================================
2)
'声明API函数 '引用了未公开的DLL文件shell32 Private Declare Function SHRunDialog Lib "shell32" _ Alias "#61" (ByVal hOwner As Long, ByVal UnknownP1 _ As Long, ByVal UnknownP2 As Long, ByVal szTitle _ As String, ByVal szPrompt As String, ByVal uFlags _ As Long) As Long '通用调用运行对话框过程 '参数Title表示对话框的标题,参数Description表示对话框中的描述文字 Public Sub ShowRunDialog(ByRef CallingForm As Form, _ Optional Title As String, _ Optional Description As String) If Title = "" Then Title = "Run" If Description = "" Then Description = _ "Type the name of a program to open, " & _ "then click OK when finished." SHRunDialog CallingForm.hWnd, 0, 0, _ Title, Description, 0 End '调用例程 Call ShowRunDialog(Me, "Run Dialog", "Choose a program and press OK")
==============================================================================
3)
shell "c:\a.exe"
==============================================================================
4)
Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters _
As String, ByVal lpDirectory As String, ByVal nShowCmd _
As Long) As Long
===============================================================================
5)
createprocessOption ExplicitPrivate Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End TypePrivate Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End TypePrivate Declare Function dcWaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Declare Function dcCreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As LongPrivate Declare Function dcCloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As LongPrivate Declare Function dcGetExitCodeProcess Lib "kernel32" Alias "GetExitCodeProcess" (ByVal hProcess As Long, lpExitCode As Long) As LongPrivate Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
On Error GoTo errExit
start.cb = Len(start)
ret = dcCreateProcess(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
ExecCmd = 0&
Exit Function
errExit:
End Function
键入“c:\1.txt”,则打开计事本显示1.txt的内容