VB API调用计算器示例

在VB(Visual Basic)编程中,API函数是与操作系统交互的关键。通过这些函数,我们可以实现如启动计算器等操作。使用以下 Declare 语句来声明 API 函数:

Private 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

ShellExecute 函数来自 shell32.dll 库,可以用来执行系统操作,例如打开文件或启动应用程序。在本例中,我们用它来启动计算器应用程序。编写事件处理程序时,例如用户点击按钮时,调用该函数:

Private Sub Command1_Click()
    Dim RetVal As Long
    RetVal = ShellExecute(Me.hWnd, "open", "calc.exe", "", vbNormalFocus)
    If RetVal <= 32 Then
        MsgBox "启动计算器失败"
    End If
End Sub
rar 文件大小:4.4KB