VB重要资料2
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Win95系统API函数大揭秘减小字体 增大字体 作者:佚名 来源:本站整理 发布时间:2009-06-23 08:11:44
Win95以其崭新精致的外观、简便快捷的操作方式,让你尽享操作平台之乐趣。而
同属Microsoft公司的Visual Basic不仅仅是一门计算机语言,还是一个集应用程序开发、测试
、查错功能于一体的功能强大的集成式开发环境, 全球有数百万的程序员受益于此。Visual
Basic一个强大的特性是它具有调用驻留在动态链接库(DLL)文件中的函数功能,其中包括由W
indows提供和使用的所有函数。对几百个函数以及DLL所包含的其它函数的存取将Visual Bas
ic的功能进行了大大的扩充,其功能远远超出其它语言。 你可以在你的应用程序中利用WIND
OWS API提供的数百个API接口函数进行扩充,加速应用程序的建立,减少程序开发的重复性。
所谓API就是“应用程序接口”(Application Programing Interface),是一些用
C语言编写并由操作系统自身调用的函数。Windows API函数由许多“动态链接库”(DLL)组成
。在Windows 95中,核心的Windows API DLL有以下这些: gdi32.dll--------图形显示界面的API;
kernel32.dll-----处理低级任务(比如内存和任务管理)的API;
user32.dll-------处理窗口和消息(Visual Basic程序员能把其中一些当作事件访问)的API。
还不断有新的API出现,来处理新的操作系统扩展,比如E-MAIL、联网和新的外设。
由于Windows API函数不是Visual Basic内部函数,所以在使用它们之前必须正式
地加以声明。要想得到正确格式化的函数声明,可以访问WINAPI目录下的文件WIN32API.TXT。
本文只对Win95系统API函数和Win95内置功能的调用问题进行介绍。例如,我们可
以直接调用标准化的“重新启动”、“磁盘格式化”、调用并更改标准的“关于”窗口、查看
“属性”、 设置墙纸、 建立快捷键、确定内存、读写注册表、在建立状态栏图标等。 重新启动
有些应用程序安装完毕后要求重新启动一次系统以使设置生效,这可利用ExitWindowsEx函数实现(代码如下)。
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As L
ong, ByVal dwReserved As Long) As Long Private Sub Command1_Click()
ExitWindowsEx &H43, 0 End Sub 磁盘格式化
当鼠标右键单击“3.5软盘A”并选择“格式化”时,则弹出标准的“磁盘格式化
”窗口。我们也可以在应用程序中利用函数轻松调用Windows 95中标准的“磁盘格式化”窗口,以实现对磁盘相应的操作功能,该函数代码如下:
Private Sub Form_Click()
FormatFloppy End Sub
模块代码见程序1: Public Const WM_CLOSE = &H10
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lp
ClassName As Any, ByVal lpWindowName As Any) As Long
Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsD
irectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
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, ByVa
l cy As Long, ByVal wFlags As Long) As Long
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long
) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRec
t As RECT) As Long
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Type RECT Left As Long Top As Long
Right As Long Bottom As Long End Type
Type POINTAPI X As Long y As Long End Type
Const SWP_NOSIZE = &H1 Const SWP_NOZORDER = &H4
Public Sub FormatFloppy()
Dim sBuffer As String, Windir As String, Procs As String, X
Dim lResult As Long sBuffer = String$(255, 0)
lResult = GetWindowsDirectory(sBuffer, Len(sBuffer))
Windir = Trim(sBuffer)
Procs = Left(Windir, lResult) & "\rundll32.exe shell32.dll,SHFormatDr
ive" Call CenterDialog("Format - 3? Floppy (A:)")
X = Shell(Procs, 1)
Call CenterDialog("Format - 3? Floppy (A:)")
k = LockWindowUpdate(0) End Sub
Public Sub CenterDialog(WinText As String) DoEvents
On Error Resume Next Dim D3 As Long
D3 = LockWindowUpdate(GetDesktopWindow()) Dim wdth%
Dim hght% Dim Scrwdth% Dim Scrhght%
Dim lpDlgRect As RECT Dim lpdskrect As RECT
Dim hTaskBar As Long hTaskBar = FindWindow(0&, WinText)
Call GetWindowRect(hTaskBar, lpDlgRect)
wdth% = lpDlgRect.Right - lpDlgRect.Left
hght% = lpDlgRect.Bottom - lpDlgRect.Top
Call GetWindowRect(GetDesktopWindow(), lpdskrect)
Scrwdth% = lpdskrect.Right - lpdskrect.Left
Scrhght% = lpdskrect.Bottom - lpdskrect.Top
X% = (Scrwdth% - wdth%) / 2 y% = (Scrhght% - hght%) / 2
Call SetWindowPos(hTaskBar, 0, X%, y%, 0, 0, SWP_NOZORDER Or SWP_NOS
IZE) DoEvents End Sub (程序1) 调用“关于”窗口
在“帮助”菜单选择
“关于XXX”会弹出标准“关于”窗口,利用ShellAbout函数
不但可以调用标准“关于”窗口,还可以随意更改其中内容。函数代码见程序3。 ShellAbout声明如下:
Private Declare Function ShellAbout Lib "shell32.dll" Alias "ShellAbou
tA" (ByVal hWnd As Long, ByVal szApp As String, ByVal szOtherStuff As String, ByV
al hIcon As Long) As Long Private Sub Form_Load()
Call ShellAbout(hWnd, “何发武工作室!”,“调用标准的关于窗口”& vbCrL
f & “电子与电脑是我们的良师益友”, 0) End Sub
HWnd设置窗口句柄,szApp设置窗口的"Caption",szOtherStuff在“版权所有”和“使用权”之间的空白处增加额外说明。
你还可以通过对注册表信息“动手术”来改变系统的“版权信息”运行REGEDIT,
为此可依次点击HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Versio
n,就会找到你计算机中“关于”版本的通用设置,这时你可以把Version的内容改为你想要的设置,如“何发武工作室”等。 查看“属性”
Win95中增加了一个全新的概念----“属性”。每个对象都拥有自己的“属性”。
在“属性”窗口里是关于对象的详细描述,对于不同的对象“属性”窗口的说明是不同的。我
们可以用ShellExecuteEX函数直接调用“属性”标准窗口。程序4功能相当于当鼠标右键单击
根目录下的autoexec.bat并选择“属性”时弹出的标准的“属性”窗口。 窗体代码如下:
Private Sub FORM_Click() Dim r As Long
Dim FileName As String FileName = "c:\autoexec.bat"
r = ShowProperties(FileName, Me.hwnd)
If r <= 32 Then MsgBox "Error" End Sub 模块代码见程序2。
Option Explicit Type SHELLEXECUTEINFO cbSize As Long
fMask As Long hwnd As Long lpVerb As String
lpFile As String lpParameters As String
lpDirectory As String nShow As Long hInstApp As Long
lpIDList As Long lpClass As String hkeyClass As Long
dwHotKey As Long hIcon As Long hProcess As Long
End Type Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400
Declare Function ShellExecuteEX Lib "shell32.dll" Alias "ShellExecuteE
x" _ (SEI As SHELLEXECUTEINFO) As Long
Public Function ShowProperties(FileName As String, OwnerhWnd As Long)
As Long Dim SEI As SHELLEXECUTEINFO Dim r As Long
With SE
I .cbSize = Len(SEI)
.fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_INVOKEIDLIST Or SEE_MASK
_FLAG_NO_UI .hwnd = OwnerhWnd .lpVerb = "properties"
.lpFile = FileName .lpParameters = vbNullChar
.lpDirectory = vbNullChar .nShow = 0 .hInstApp = 0
.lpIDList = 0 End With r = ShellExecuteEX(SEI)
ShowProperties = SEI.hInstApp End Function (程序2)
设置墙纸 墙纸是显示在桌面上的图片或图像,是Win95的一个重要部分。你可以通过改变列
表中的文件来选择多种多姿多采的墙纸。我们可以利用SystemParametersInfo函数来揭开它的奥妙,并亲手为她营造异样的风采。
在程序窗体中增加List1控件,并加入如程序3所示代码: Option Explicit
Private Declare Function SystemParametersInfo Lib "user32" Alias "Syst
emParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam A
s Any, ByVal fuWinIni As Long) As Long Const SPI_SETDESKWALLPAPER = 20
Const SPIF_UPDATEINIFILE = &H1 ''update Win.ini Constant
Const SPIF_SENDWININICHANGE = &H2 ''update Win.ini and tell everyone
Private Sub Form_Load() Dim Temp As String
Temp = InputBox("Please Input A Directory", "Changer", "C:\WINDOWS\")
If Temp = "" Then End ''Cancel clicked
If Right$(Temp, 1) <> "\" Then Temp = Temp + "\"
List1.Tag = Temp
Temp = Temp + "*.bmp" ''Set the file filter (path + *.BMP)
Temp = Dir$(Temp) Do While Temp$ <> "" Temp = Dir$
If Temp = "" Then Exit Do List1.AddItem Temp Loop
List1.AddItem "(None)" Show List1.SetFocus
List1.ListIndex = 0 End Sub
Private Sub list1_dblclick() Dim Temp As String
Dim BMPFile As String Temp = Tag
If List1.Text = "(None)" Then BMPFile = "(none)"
Else BMPFile = Temp + (List1) End If
SystemParametersInfo SPI_SETDESKWALLPAPER, 0, ByVal BMPFile, SPIF_UPD
ATEINIFILE End Sub
Private Sub List1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then list1_dblclick End Sub (程序3)
建立快捷方式 Win95中快捷方式提供了对常用程序和文档的访问捷径,你可以为桌面或文件夹中
的任何程序、文档或打印机添加快捷方式。VB5中利用fCreateShellLink函数可以为常用程序和文档快速建立快捷方式。
fCreateShellLink的声明
为:
Private Declare Function fCreateShellLink Lib "STKIT432.DLL" (ByVal lp
strFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As St
ring, ByVal lpstrLinkArgs As String) As Long
LpstrFolderName设置快捷方式的文件夹名称,lpstrLinkName设置快捷方式的标
题名称,lpstrLinkPath设置快捷方式所指向的应用程序的目录及文件名,格式如下:
fCreateShellLink(Destinationpath, Shortcutname, SourcepathAppName, "")
程序4所示的代码在“桌面”上为d:\path\appname.exe应用程序建立名为"Shortcut Title"的快捷方式。
Private Declare Function fCreateShellLink Lib "STKIT432.DLL" (ByVal lp
strFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As St
ring, ByVal lpstrLinkArgs As String) As Long Private Sub Form_Click()
lResult = fCreateShellLink("....\WINDOWS\DESKTOP", "Shortcut Title", "
d:\path\appname.exe", "") End Sub (程序4)
如果想在“桌面”上建立快捷方式,则建立的目标目录应该为"....\WINDOWS\DE
SKTOP";如果想在“开始”菜单中建立快捷方式,则建立的目标目录应该为"....\WINDOWS\S
tart Menu"; 如果想在“程序”菜单中建立快捷方式,则建立的目标目录应该为相应的"...
.C:\WINDOWS\Start Menu\Programs"中,以此类推。 确定内存
我们经常要访问Windows管理的内存。对应用程序性能影响最大的因素是可用的内
存容量。访问系统内存在处理类似于位图文件之类的大文件时非常有用,因为程序通过交换文
件(Swap)的方法,可以获得比实际可用内存更大的内存。知道内存如何分配后,就可以读入内
存值并操作大型数字文件。可以用丰富的Win32 API函数确定Windows 的全局内存并操作数据文件,这些对于确定程序能否正常工作非常有用。
有关函数声明: dwLength 数据结构的长度 dwMemoryLoad 内存使用百分比
dwTotalPhys 实际内存总字节数 dwAvailPhys 可用的实际内存字节数
dwTotalPageFile 分页文件总字节数 dwAvailPageFile 分页文件可用字节数
dwTotalVirtual 虚拟内存的总字节数 dwAvailVirtual 可用的虚拟内存字节数
加入代码如下的模块: 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
Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATU
S) 窗体中加入代码如下:
Private Sub Form_Click()
Dim m As MEMORYSTATUS m.dwLength = Len(m)
GlobalMemoryStatus m Print "数据结构的长度", m.dwLength
Print "内存使用百分比", m.dwMemoryLoad
Print "实际内存总字节数 ", m.dwTotalPhys
Print "可用的实际内存字节数", m.dwAvailPhys
Print "分页文件总字节数", m.dwTotalPageFile
Print "分页文件可用字节数", m.dwAvailPageFile
Print "虚拟内存的总字节数", m.dwTotalVirtual
Print "可用的虚拟内存字节数", m.dwAvailVirtual End Sub 读写注册表
Win95及NT的注册表数据库(Registry)是系统中非常重要的组成部分,它设置了W
in95及NT的参数,包括用户信息、系统硬件配置和应用程序等信息。注册表系统代替了旧版W
indows中的多个INI文件(警告:如果你对注册表不熟悉,不要随意修改它。如果注册表项目出错,会使机器崩溃,甚至破坏操作系统本身)。
Win32 API中Reg函数处理对注册表数据库一般的读写过程如下:
1、使用RegOpenKey或RegCreateKey打开或创建一个键;
2、如果上一步成功,则使用RegQueryValue(或RegQueryValueEx)读取子键的值
,使用RegSetValue(或RegvSetValueEx)设置子键值,使用RegEnumKey获得所有子键,使用RegDeleteKey删除一个键;
3、完成操作后使用RegCloseKey关闭键。
下面的程序5演示了如何生成键、存放值并取得注册表数据。在HKEY_LOCAL_MACH
INE键下面生成三个子键Test\Mastering vb5、Windows Width、Windows Height,用于存放上次执行时的窗体尺寸。
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCre
ateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Lon
g Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDel
eteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegD
eleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "Reg
QueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved
As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long '' Note that if
you declare the lpData parameter as String, you must pass it By Value.
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSe
tValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Lo
ng, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long '' Note tha
t if you declare the lpData parameter as String, you must pass it By Value.
Const ERROR_S
UCCESS = 0& Const ERROR_BADDB = 1009&
Const ERROR_BADKEY = 1010& Const ERROR_CANTOPEN = 1011&
Const ERROR_CANTREAD = 1012& Const ERROR_CANTWRITE = 1013&
Const ERROR_REGISTRY_RECOVERED = 1014&
Const ERROR_REGISTRY_CORRUPT = 1015&
Const ERROR_REGISTRY_IO_FAILED = 1016&
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const regkey = "Test\Mastering vb5"
Private Sub Form_Load() Dim retValue As Long
Dim result As Long Dim keyValue As String
Dim keyId As Long Dim subKey As String
Dim bufSize As Long Label6.Caption = regkey
retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regkey, keyId)
If retValue = 0 Then subKey = "Windows Width"
retValue = RegQueryValueEx(keyId, subKey, 0&, reg_sz, 0&, bufSize)
If bufSize < 2 Then keyValue = Me.Width
retValue = RegSetValueEx(keyId, subKey, 0&, reg_sz, ByVal keyValue, L
en(keyValue) + 1) Else keyValue = String(bufSize + 1, "")
retValue = RegQueryValueEx(keyId, subKey, 0&, reg_sz, bykeyvalue, buf
Size) keyValue = Left$(keyValue, bufSize - 1)
Me.Width = keyValue End If Label4.Caption = subKey
Label5.Caption = Me.Width subKey = "Widows Height"
retValue = RegQueryValueEx(keyId, subKey, 0&, reg_sz, 0&, bufSize)
If bufSize < 2 Then keyValue = Me.Height
retValue = RegSetValueEx(keyId, subKey, 0&, reg_sz, ByVal keyValue, L
en(keyValue) + 1) Else keyValue = String(bufSize + 1, "")
retValue = RegQueryValueEx(keyId, subKey, 0&, reg_sz, ByVal keyValue,
bufSize - 1) Me.Height = keyValue End If
Label8.Caption = subKey Label7.Caption = Me.Height
End If End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim keyValue As String Dim retValue As Long
Dim keyId As Long
retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regkey, keyId)
keyValue = Me.Width
retValue = RegSetValueEx(keyId, "Windows Width", 0&, reg_sz, ByVal key
Value, Len(keyValue) + 1) keyValue = Me.Height
retValue = RegSetValueEx(keyId, "Windows Height", 0&, reg_sz, ByVal ke
yValue, Len(keyValue) + 1) End Sub (程序5) 在“状态栏”中建立图标
Win95中如“音量”、“日期”、“屏幕”等可以在Win95的状态栏(Tray)上建
立
图标。如果鼠标停留在图标上,则会显示出相应的工具提示(ToolTip),当用户在图标上单击
(或双击)鼠标左键时程序便会被激活,单击右键时会实现其简捷的功能菜单。
Tray编程比较特殊,主要包括三个主要方面:图标、工具提示和消息。它属于Sh
ell编程的一部分,主要是利用Shell API中的Shell_NotifyIcon函数完成的。Shell_NotifyIcon函数是这样声明的:
Type NOTIFYICONDATA cbSize As Long 结构所占的字节数
hwnd As Long 接受TRAY托盘图标消息的窗口句柄 uID As Long 由应用程序定义的图标识别符
uFlags As Long 标志 uCallbackMessage As Long 由应用程序定义的消息
hIcon As Long TRAY 图标句柄 szTip As String * 64 工具提示字符串
End Type
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias " Shell_Noti
fyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
这个结构中的uFlags很重要,它有三种取值:NIF_ICON、NIF_MESSAGE和NIF_TIP
,分别表示hIcon、uCallbackMessage和szTip参数有效,用来修改图标、消息和工具提示。这
三个取值可以同时使用(三个参数相或),也可以单独使用。为了实现对用户鼠标操作的响应
,需要在程序中处理uCallbackMessage所定义的消息,该消息的长参数lParam包含Win32所定
义的鼠标消息,如果有多个图标,则短参数wParam表示图标识别符。
在vb光盘中\TOOLS\UNSUPPRT\SYSTRAY目录下有SYSTRAY的工程,经编译后生成SYSTRAY.OCX控件。
现在我们应用SYSTRAY.OCX控件进行Tray编程。 首先建立一个“工程”,依次选择“工程”、“部件”(或者在“工具箱”上单
击鼠标右键选择“部件”),再复选SYSTEM TRAY CONTROL CSYSTRAY,CSYSTRAY控件即出现在“工具箱”中。
SYSTRAY.OCX控件拥有MouseDblClick、MouseDown、MouseMove、MouseUp四个事件
,响应先后顺序为MouseMove、MouseDown、MouseUp、MouseDblClick。程序6很清楚地说明了他们的响应时间次序。
Private Sub cSysTray1_MouseDblClick(Button As Integer, Id As Long)
MsgBox "Hei!You have DblClick the mouse!" End Sub
Private Sub cSysTray1_MouseDown(Button As Integer, Id As Long)
If Button = 2 Then
MsgBox "Hei!You have clicked me in right button!" End If
End Sub Private Sub cSysTray1_MouseMove(Id As Long)
MsgBox "Hello!I am here!" End Sub
Private Sub cSysTray1_MouseUp(Button As Integer, Id As Long)
MsgBox "Hei!You clicked me just now!" End Sub
Private Sub Form_Load()
cSysTray1.TrayTi
p = "Hello! I am the King of the world!"
cSysTray1.InTray = True
cSysTray1.TrayIcon = "c:\fittings\FACE.ico" End Sub
(程序6)
SYSTRAY控件还拥有InTray、Name、TrayIcon、TrayTip、Parent、Index、Objec
t、Tag属性。InTray设置是否在Win95的状态栏Tray上建立一个快捷图标;Name设置SYSTRAY控
件的名称;TrayIcon设置在Tray上所建立的快捷图标的外观;TrayTip设置了鼠标停留在图标
上时所显示的工具提示(ToolTip)内容;Tag储存程序所需要的附加数据。
通过实践发现,在使用VB开发应用程序时,灵活使用API 函数,会更大地发挥VB
的作用,使开发的软件更专业,功能更强大。本文仅仅是介绍了一下API函数在Win95中的使
用方法,以作抛砖引玉之用。对于真正的API 函数应用,还需要参考更为详尽的资料和广大爱好者更为努力的探索和研究。