• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

Win95系统API函数大揭秘

发布: 2007-7-14 20:28 | 作者: 佚名    | 来源: 网络转载     | 查看: 75次 | 进入软件测试论坛讨论

领测软件测试网
    Win95以其崭新精致的外观、简便快捷的操作方式,让你尽享操作平台之乐趣。同属Microsoft公
司的Visual Basic不仅仅是一门计算机语言,还是一个集应用程序开发测试、查错功能于一体的功能
强大的集成式开发环境, 数百万的程序员受益于此。
Visual Basic一个强大的特性是它具有调用驻留在动态连接库(DLL)文件中的函数功能,其中包
括由Windows提供和使用的所有函数。对几百个函数以及DLL所包含的其它函数的存取将Visual Basic
的功能进行了大大的扩充,其功能远远超出其它语言,真有种“信手拈来”之神韵,毕竟Visual Basic
是Microsoft公司的“嫡系部队”。 你可以在你的应用程序中利用WINDOWS API提供的数百个API接口
函数进行扩充,加速应用程序的建立,减少程序开发的重复性(比尔.盖茨找你打版权官司可不关我事!
——喔喔!)
所谓API就是“应用程序接口”(Application Programing Interface),是一些用C语言编写由操作系统
自身调用的函数。Windows API函数由许多“动态连接库”或DLL组成。在32位Windows中,核心的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 Long, 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
加入代码如下的模块:
Public Const WM_CLOSE = &H10
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal
lpWindowName As Any) As Long
Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (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, ByVal 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, lpRect 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,SHFormatDrive"
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_NOSIZE)
DoEvents
End Sub
? 调用“关于”窗口
在“帮助”菜单选择“关于XXX”会弹出标准“关于”窗口,利用ShellAbout函数不但可以调用
标准“关于”窗口,还可以随意更改其中内容呢!
ShellAbout声明如下:
HWnd设置窗口句柄,szApp设置窗口的“Caption”, szOtherStuff 在“版权所有”和“使用权”
之间的空白处增加额外说明。
Private Declare Function ShellAbout Lib "shell32.dll" Alias "ShellAboutA" (ByVal hWnd As Long,
ByVal szApp As String, ByVal szOtherStuff As String, ByVal hIcon As Long) As Long
Private Sub Form_Load()
Call ShellAbout(hWnd, "何发武天使工作室!", "调用标准的关于窗口" & vbCrLf & "上帝与你同在!
阿弥陀佛!", 0)
End Sub
    你还可以通过对注册表信息“动手术”来改变系统的“版权信息”运行REGEDIT,按照如下路径:
HKEY_LOCAL_MACHINE →SOFTWARE → Microsoft→Windows→ CurrentVersion →Version,就会找到
你计算机中“关于”版本的通用设置,你可以把Version的内容改为你心仪的设置,如“何发武天使工
作室”、“ 何发武野狼工作室”等。
? 查看“属性”
    Win95中增加了一个全新的概念——“属性”,每个对象都拥有自己的“属性”,在“属性”窗口里
是关于对象的详细描述, 并且不同的对象“属性”窗口的说明是不同的。我们可以用ShellExecuteEX函
数直接调用“属性”标准窗口。下述程序功能相当于当鼠标右键单击根目录下的“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
加入代码如下的模块:
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 "ShellExecuteEx" _
(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 SEI
.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
? 设置墙纸
墙纸是显示在桌面的图片或图像,是Win95的一个重要窗口。你可以通过改变列表中的文件来选择多
姿多采的墙纸。墙纸为Win95蒙上了一披美丽的面纱,我们可以利用SystemParametersInfo函数来揭开
它的“神秘面纱”并亲手为她营造异样的风采。
在窗体中增加List1控件,窗体加入如下代码:
Option Explicit
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA"
(ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As 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_UPDATEINIFILE
End Sub

Private Sub List1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then list1_dblclick
End Sub
马上试试,当场把你的墙纸改变啦(不变不收钱)!爽吧!
? 建立快捷键
Win95中快捷方式提供了对常用程序和文档的访问捷径,你可以为桌面或文件夹中的任何程序、文档
或打印机添加快捷方式。VB5中利用fCreateShellLink函数可以为常用程序和文档快速建立建立快捷键。
fCreateShellLink的声明为:
Private Declare Function fCreateShellLink Lib "STKIT432.DLL" (ByVal lpstrFolderName As String,
ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArgs As String)
As Long
LpstrFolderName设置快捷方式的文件夹名称,lpstrLinkName设置快捷方式的标题名称,lpstrLinkPath
设置快捷方式所指向的应用程序的目录及文件名。简而言之,如下格式:
fCreateShellLink(Destinationpath, Shortcutname, SourcepathAppName, "")
    如下代码在“桌面”上为“d:\path\appname.exe”应用程序建立名为"Shortcut Title"的快捷方
式。
Private Declare Function fCreateShellLink Lib "STKIT432.DLL" (ByVal lpstrFolderName As String,
ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArgs As String)
As Long
Private Sub Form_Click()
lResult = fCreateShellLink("....\WINDOWS\DESKTOP", "Shortcut Title", "d:\path\appname.exe",
"")
End Sub
技巧:
如果想在“桌面”上建立快捷方式,则建立目标目录应该为“....\WINDOWS\DESKTOP”; 如果想在
“开始”菜单中建立快捷方式,则建立目标目录应该为“....\WINDOWS\Start 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 MEMORYSTATUS)
窗体中加入如下代码:
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)是系统中非常重要的组成部分,它设置了Win95及NT的参数,
包括用户信息、系统硬件配置和应用程序等信息。注册表系统代替了旧版Windows中的多个INI文件。(警
告:如果你对注册表不熟悉,不要随意修改它。如果注册表项目出错,会使机器崩溃,甚至破坏操作系
统本身。)
Win32 API中Reg函数处理对注册表数据库一般的读写过程如下:
1、使用RegOpenKey或RegCreateKey打开或创建一个键;
2、如果上一步成功,使用RegQueryValue(或RegQueryValueEx)读取子键的值,使用RegSetValue(或
RegvSetValueEx)设置子键值,使用RegEnumKey获得所有子键,使用RegDeleteKey删除一个键;
3、完成操作后使用RegCloseKey关闭键。
下述应用程序演示了如何生成键、存放值并取得注册表数据。在HKEY_LOCAL_MACHINE键下面生成三
个子键Test\Mastering vb5、    Windows Width、Windows Height,用于存放上次执行时的窗体尺寸。
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As
Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As
Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey
As Long, ByVal lpValueName As String) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (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 "RegSetValueExA" (ByVal hKey
As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As
Any, ByVal cbData As Long) As Long ' Note that if you declare the lpData parameter as
String, you must pass it By Value.
Const ERROR_SUCCESS = 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, Len(keyValue) + 1)
Else
keyValue = String(bufSize + 1, "")
retValue = RegQueryValueEx(keyId, subKey, 0&, reg_sz, bykeyvalue, bufSize)
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, Len(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 keyValue, Len(keyValue) +
1)
keyValue = Me.Height
retValue = RegSetValueEx(keyId, "Windows Height", 0&, reg_sz, ByVal keyValue, Len(keyValue)
+ 1)
End Sub
? 在“状态区”中建立图标
    Win95中如“音量”、“日期”、“屏幕”等可以在Win95的状态栏(Tray)上建立快捷图标。如果鼠
标停留在图标上,会显示出相应的工具提示(ToolTip),当用户在图标上单击(或双击)鼠标左键时,软
件会实现相应的功能,单击右键时会实现其简捷的功能。
Tray也称作SysTray、TrayIcon、NotifyIcon、Status Area等,它是Win95/NT的任务条上一个特殊区
域,许多软件运行时在Tray中加入自己的图标,这个区域的另一个特殊之处在于你可以从资源管理器内
拖文件然后放在这个区域。正是由于这些特性,Tray编程在Win95/NT中有特殊的地位。
Tray编程比较特殊,主要包括三个主要方面:图标、工具提示和消息。它属于Shell编程的一部分,
主要是利用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_NotifyIconA" (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。下面编程很清楚地说明他们的响应时间次
序。
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.TrayTip = "Hello! I am the King of the world!"
cSysTray1.InTray = True
cSysTray1.TrayIcon = "c:\fittings\FACE.ico"
End Sub
    SYSTRAY控件还拥有InTray、Name、TrayIcon、TrayTip、Parent、Index、Object、Tag属性。InTray
设置是否在Win95的状态栏Tray上建立一个快捷图标;Name设置SYSTRAY控件的名称;TrayIcon设
置在Tray上所建立快捷图标的外观;TrayTip设置如果鼠标停留在图标上所显示的工具提示(ToolTip)内
容;Tag储存程序所需要的附加数据。

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网