一、句柄hwnd
handle of window 窗体(或控件)的把柄,它是一个长整型,用来标识一个窗体或控件,同一时刻没有两个句柄是一样的。
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
1、Lib部分:来由哪个库(dll),常见如下:
Kernel32.dll windows核心函数库
User32.dll 使用者操作界面函数库
Gdi32.dll 图形外部界面函数库
Comdlg.dll 通用对话框函数库 common dialog
Shell32.dll shell界面函数库
winmm.dll 多媒体函数库 windows multimedia
Advapi.dll 高级API函数库 Advanced Windows 32 Base API DLL
Lz32.dll 压缩及解压函数库
2、Alias部分:库中名为什么。若外面名与库内名一致,此部分省略
3、参数及返回值。
二、参数
参数前未标明的是传址方式,byval是传值方式。有时类型是Any时需小心。
字符串与定长字符串的区别。
'获取计算机名 'lpBuffer 字串指针 'nSize 字串总长度 Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long Private Sub Command1_Click() Dim s As String, lngSize As Long lngSize = 255 s = String(lngSize, 0) '格式化255个空字符 GetComputerName s, lngSize s = Left$(s, lngSize) Text1.Text = s End Sub
'获取鼠标位置 'pointapi中x,y分别是屏幕像素位置 '返回值 Long,非零表示成功,零表示失败。 Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private Type POINTAPI X As Long Y As Long End Type Dim p As POINTAPI Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim a As Long GetCursorPos p Text1.Text = p.X & "," & p.Y End Sub
'取得系统目录 '长度取260 Private Declare Function GetSystemDirectory _ Lib "kernel32" _ Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _ ByVal nSize As Long) As Long Private Const MAX_PATH = 260 Private Sub Command1_Click() Dim s As String, max As Long s = String$(MAX_PATH, 0) max = GetSystemDirectory(s, MAX_PATH) Text1.Text = Left$(s, InStr(s, Chr(0)) - 1) '截取字串 End Sub
'取得windows目录 '长度取260 Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Private Const MAX_PATH = 260 Private Sub Command1_Click() Dim s As String, max As Long s = String$(MAX_PATH, 0) max = GetWindowsDirectory(s, MAX_PATH) Text1.Text = Left$(s, InStr(s, Chr(0)) - 1) '截取字串 End Sub
'取得操作系统版本,判断是什么操作系统 'Operating system Version number 'Windows 8 6.2 'Windows 7 6.1 'Windows Server 2008 R2 6.1 'Windows Server 2008 6.0 'Windows Vista 6.0 'Windows Server 2003 R2 5.2 'Windows Server 2003 5.2 'Windows XP 5.1 'Windows 2000 5.0 Private Declare Function GetVersionEx _ Lib "kernel32" _ Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long Private Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 ' Maintenance string for PSS usage End Type Private Sub Command1_Click() Dim ver As OSVERSIONINFO ver.dwOSVersionInfoSize = Len(ver) GetVersionEx ver Text1.Text = ver.dwMajorVersion & "." & ver.dwMinorVersion & "." & ver.dwBuildNumber & " " & ver.dwPlatformId End Sub