回 帖 发 新 帖 刷新版面

主题:如何使用api函数

请问荥API函数是如何使用的??
比如你要获得系统的名称,该用什么API,如何使用?
举例行吗?
最好是详细点!多谢!

回复列表 (共10个回复)

沙发

你可以先看看WINAPI的帮助,我这有一个,你想要的话,可以给我发EMAIL,---zhanghairui@371.net,另外网上有很多,象http://www.crob.net/delphi/databank/winapi.zip,你可以先看看!

板凳

WIN32 API 是一组主要用C语言编写的函数,但都可以在VB应用程序中使用。许多VB编程人员利用WIN32 API 函数,来完成VB中不可能完成的事。API函数并不复杂,也不象有的VB编程人员想象的那么神秘。主要问题在于,API是为C语言编程人员建立的,很难用C语言的约定向VB映射函数及数据类型声明。

WINDOWS API可以分为4个功能类别。
窗口管理(USER)
图形设备接口(GUI)
系统服务(KERNEL,内核)
多媒体

下面分别举一个例子进行说明

1.窗口管理
有的应用程序不管窗口是否活动都是窗口保持在最上层,比如MICROSOFT WORD 的FIND窗口就是这样。这是通过调用SetWindowPos()API函数实现的,该函数声明如下:
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
HWnd变元是窗口的句柄,x、y是窗口左上角的坐标,cx、cy是窗口宽度和高度,
hWndInsertAfter变元是窗口清单中hWnd窗口前的窗口句柄,其值见表1。1

数值 作用
HWND_BOTTOM 把窗口放在窗口清单的底部
HWND_TOP 把窗口放在窗口清单的字符顺序的顶部
HWND_TOPMOST 把窗口放在窗口清单的顶部
HWND_NOTOPMOST 把窗口放在窗口清单的顶部,最上层窗口下


WFlags变元为整型值,它包含一个或几个标志见表1。2

标志 作用
SWP_DRAWFRAME 在窗口周围画一个方框
SWP_HIDEWINDOW 隐藏窗口
SWP_NOACTIVATE 不激活窗口
SWP_NOMOVE 保持窗口当前位置(忽视x、y变元)
SWP_NOREDRAW 窗口不自动重画
SWP_NOSIZE 保持窗口当前尺寸
SWP_NOZORDER 保持窗口在窗口清单中的当前位置
SWP_SHOWWINDOW 显示窗口

提示:这里未列出常量值,可以从API Viewer窗口复制到应用程序中,不宜在程序中强行修改常量值。
实例:
Option Explicit

Private Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long

Const HWND_TOPMOST = -1
Const SWP_SHOWWINDOW = &H40

Private Sub Form_Load()
Dim retValue As Long
retValue = SetWindowPos(Me.hwnd, HWND_TOPMOST, _
Me.CurrentX, Me.CurrentY, 300, 300, SWP_SHOWWINDOW)
End Sub     2.图形设备接口(GUI)
WIN32 API的函数BitBlt()用于从源对象向目标对象成块复制图形。函数从指定源设备描述表向目标设备描述表传送一批象素。还有一函数StretchBlt(),它可以从源矩形向目标矩形复制位图。
Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _
ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
ByVal dwRop As Long) As Long

Public Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, _
ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long)
As Long
数值 说明
BLACKNESS 用黑色填充目标矩形
DSTINVERT 逆转目标矩形
MERGECOPY 用AND运算合并图案与源矩形
MERGEPAINT 用"或"运算合并源矩形的反与目标矩形
NOTSRCERASE 复制源矩形的逆矩形到目标中
PATCOPY 用"或"运算合并源矩形和目标矩形求反
PATINVERT 将图案复制到目标矩形
PATPAINT 用XOR运算组合图案和目标矩形
用或运算组合源矩形的反与图案颜色,并将结果用或运算组合目标矩形
SRLAND 用AND运算组合源和目标矩形
SRCCOPY 复制源矩形到目标矩形
SRCERASE 将目标矩形的逆用AND运算与源矩形组合
SRCINVERT 用XOR运算组合源矩形和目标矩形
SRCPAINT 用或运算组合源和目标矩形
WHITENESS 用白色填充目标矩形

实例:
Option Explicit
Private Const WHITENESS = &HFF0062 ' (DWORD) dest = WHITE
Private Const BLACKNESS = &H42 ' (DWORD) dest = BLACK

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _
ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
ByVal dwRop As Long) As Long

Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, _
ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Private Sub Command1_Click()
Dim retValue As Long
retValue = BitBlt(Picture1.hdc, 0, 0, Picture1.Width / 50, Picture1.Height, Picture1.hdc, 0, 0, WHITENESS)
End Sub

Private Sub Command2_Click()
Dim retValue As Long
retValue = BitBlt(Picture1.hdc, 0, 0, Picture1.Width / 50, Picture1.Height, Picture1.hdc, 0, 0, BLACKNESS)
End Sub

3.系统服务(KERNEL,内核)
WIN32 API的函数GlobalMemoryStatus()可获取系统中当前内存状态的信息。

Public Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)

Public Type MEMORYSTATUS '数据结构
dwLength As Long
dwMemoryLoad As Long
dwTotalPhys As Long
dwAvailPhys As Long
dwTotalPageFile As Long
dwAvailPageFile As Long
dwTotalVirtual As Long
dwAvailVirtual As Long
End Type

说明:
必须设置表示该数据结构长度的dwLength成员之后,才能传递变元到GlobalMenorystatus()函数;其他字段由函数设置。  成员 说明
DwLength MEMORYSTATUS数据结构的长度
DwMenmoryLoad 内存使用百分比
DwTotalPhys 实际内存总字节数
DwAvaiPhys 可用的实际内存字节数
DwtotalPageFile 分页文件总字节数
DwAvailpageFile 分页文件可用字节数
DwTotalVirtal 虚拟内存的总字节数
DwAvailVirtual 可用的虚拟内存字节数
实例:
Option Explicit

Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)

Private Type MEMORYSTATUS '数据结构
dwLength As Long
dwMemoryLoad As Long
dwTotalPhys As Long
dwAvailPhys As Long
dwTotalPageFile As Long
dwAvailPageFile As Long
dwTotalVirtual As Long
dwAvailVirtual As Long
End Type

Private Sub Command1_Click()
Dim memStat As MEMORYSTATUS
memStat.dwLength = Len(memStat)
Call GlobalMemoryStatus(memStat)
Label1(0).Caption = memStat.dwMemoryLoad
Label1(1).Caption = memStat.dwTotalPhys
Label1(2).Caption = memStat.dwAvailPhys
Label1(3).Caption = memStat.dwTotalPageFile
Label1(4).Caption = memStat.dwAvailPageFile
Label1(5).Caption = memStat.dwTotalVirtual
Label1(6).Caption = memStat.dwAvailVirtual
End Sub

4.多媒体
  WIN32 API的函数micSendString()、mciExecute()用于向MCI设备发出命令。当发出的命令失败时该函数将返回一个长整型数值;其执行状态的返回值,0表示操作成功,正数表示操作失败的原因。返回的错误代码(数字)为把它转换成实际的错误信息用另一个API函数micGetErrorString()进行转换。
Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Public Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" _
(ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long

Public Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long

命令 说明 命令 说明
Capability 请求设备能力的信息 Record 开始录制
Close 关闭设备 Resume 恢复暂停的播放或录制
Info 请求设备的信息 Seek 改变媒介中的当前位置
Open 打开和初始化所用的设备 Set 改变设备的控件设置
Pause 暂停设备的播放或录制 Status 请求关于设备状态的信息
Play 设备播放开始 Stop 停止设备的播放或录制

打开设备命令(OPEN)
变元:
alias别名 用于指定该设备的别名
shareable 允许应用程序公享公用设备
type设备名 设备为媒体元素而不是MCI设备名时,它表示MCI设备名
播放文件(PLAY)

变元:
from 播放段的开始位置(以毫秒为单位时间计算)
to 播放段的结束位置
wait 使MCI设备等待播放结束后才将控制权交回应用程序
说明:如不用WAIT变元,另一个PLAY命令将会中断当前正在播放的文件。

实例:(举例说明mciExecute函数)
Option Explicit

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" _
(ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long

Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long

Private Sub Play_Click()
Dim errorCode As Long
Dim strError As String
On Error GoTo error
errorCode = mciExecute("open waveaudio")
errorCode = mciExecute("play c:\winnt\media\ringin.wav")
errorCode = mciExecute("close waveaudio")
Exit Sub
error:
MsgBox mciGetErrorString(errorCode, strError, 255)
End Sub

Private Sub Stop_Click()
End
End Sub

3 楼

好东东,谢谢!

4 楼

如何写调用打开*.bat文件的命令呢

5 楼

set myshell=createobject("wscript.shell")
myshell.run("盘符:\跟文件所在的地方")

6 楼

一直困惑我的问题,谢谢仁兄

7 楼

使我获益非浅,谢了

8 楼

各位好,我找不到“提交问题”的地方,请指点。

9 楼

珍贵的材料啊,收了收了。谢谢楼主的引导和楼下诸位高手的鼎力相帮!

10 楼

请问读取光标处字符的API函数是什么?

我来回复

您尚未登录,请登录后再回复。点此登录或注册