您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > VBA中API_函数集锦
VBA中API函数集锦Followingcontentcopiedfromaccess911.netAPI函数在VB中得到了充分的运用,同时也让无数VB爱好者沉溺于其中。幸运的是ACCESS使用的VBA也能很好的引入API函数1、如何让窗体总在最前面?*API函数声明DeclareFunctionSetWindowPosLibuser32(ByValhwndAsLong,ByValhWndInsertAfterAsLong,ByValxAsLong,ByValyAsLong,ByValcxAsLong,ByValcyAsLong,ByValwFlagsAsLong)AsLong注释:常量声明PrivateConstSWP_NOSIZE=&H1PrivateConstSWP_NOMOVE=&H2PrivateConstHWND_TOPMOST=-1PrivateConstHWND_NOTOPMOST=-2注释:在某个form里写:SetWindowPosme.hWnd,WND_TOPMOST,0,0,0,0,SWP_NOMOVE注释:或下面SetWindowPosme.hWnd,WND_TOPMOST,0,0,0,0,SWP_NOSIZE2、使用API函数sendmessage,获得光标所在行和列。Subgetcaretpos(byvalTextHwnd&,LineNo&,ColNo&)注释:TextHwnd为TextBox的hWnd属性值,LineNo为所在行数,ColNo为列数dimI&,j&,k&注释:获取起始位置到光标所在位置字节数I=SendMessage(TextHwnd,&HB0&,0,0)j=I/2^16注释:确定所在行LineNo=SendMessage(TextHwnd,&HC9&,j,0)+1注释:确定所在列k=SendMessage(TextHwnd,&HBB&,-1,0)ColNo=j-k+1Endsub3、如何以某种颜色填充某区域?*API函数声明PrivateDeclareSubFloodFillLibgdi32_(ByValHdcAsLong,ByValXAsLong,ByValYAs_Long,ByValcrColorAsLong注释:设(fillx,filly)为此区域内任一点注释:Color为某种颜色FloodFillPicture1.Hdc,fillx,filly,Color4、如何关闭计算机?!请注意你WINDOWS的版本*API函数声明DeclareFunctionExitWindowsLibUser(ByValdwReturnCodeAsLong,ByValwReservedAsInteger)AsInteger注释:执行DimDUMMYDUMMY=ExitWindows(0,0)强制关闭计算机:用API函数ExitWindowsEx可以实现强制关机,即便是您的应用程序尚未保存文件。PrivateDeclareFunctionExitWindowsExLibuser32(ByValuFlagsAsLong,ByValdwReservedAsLong)AsLongConstEWX_SHUTDOWN=1ConstEWX_LOGOFF=0ConstEWX_REBOOT=2ConstEWX_FORCE=4PrivateSubCommand1_Click()Dimaa=ExitWindowsEx(EWX_LOGOFForEWX_FORCEorEWX_SHUTDOWN,0)EndSub如果将a=ExitWindowsEx(EWX_LOGOFForEWX_FORCEorEWX_SHUTDOWN,0)改换为a=ExitWindowsEx(EWX_LOGOFForEWX_REBOOT,0)即可实现强制重启计算机!5、如何获取Windows目录和System目录?注释:复制以下代码到一模块中PublicDeclareFunctionGetWindowsDirectoryLibkernel32AliasGetWindowsDirectoryA(ByVallpBufferAsString,ByValnSizeAsLong)AsLongPublicDeclareFunctionGetSystemDirectoryLibkernel32AliasGetSystemDirectoryA(ByVallpBufferAsString,ByValnSizeAsLong)AsLong注释:在程序中调用DimWindowsDirectoryAsString,SystemDirectoryAsString,xAsLongWindowsDirectory=Space(255)SystemDirectory=Space(255)x=GetWindowsDirectory(WindowsDirectory,255)x=GetSystemDirectory(SystemDirectory,255)MsgBoxWindows的安装目录是:+WindowsDirectory+,系统目录是:+SystemDirectory6、如何建立简单的超级连接?*API函数声明PrivateDeclareFunctionShellExecuteLibshell32.dllAliasShellExecuteA(ByValhWndAsLong,ByVallpOperationAsString,ByVallpFileAsString,ByVallpParametersAsString,ByVallpDirectoryAsString,ByValnShowCmdAsLong)AsLong注释:打开某个网址ShellExecute0,open,注释:给某个信箱发电子邮件ShellExecutehwnd,open,mailto:sst95@21cn.com,vbNullString,vbNullString,07、如何得知TextBox中文字所有的行数?*API函数声明DeclareFunctionSendMessageLibuser32AliasSendMessageA(ByValhwndAsLong,ByValwMsgAsLong,ByValwParamAsLong,lParamAsAny)AsLongPublicConstEM_GETLINECOUNT=&HBA注释:在程序中调用LineCnt=SendMessage(ctl.hwnd,EM_GETLINECOUNT,0,0)注释:LineCnt即为此TextBox的行数。8、如何设置ListBox的水平卷动轴的宽度?*API函数声明ConstLB_SETHORIZONTALEXTENT=&H194PrivateDeclareFunctionSendMessageLibuser32AliasSendMessageA_(ByValhwndAsLong,ByValwMsgAsLong,ByValwParamAsLong,_lParamAsAny)AsLong注释:调用CallSendMessage(List1.hwnd,LB_SETHORIZONTALEXTENT,400,ByVal0&)注释:注意400是以象素为单位,你可以根据情况自行设定。9、如何交换鼠标按键?*API函数声明DeclareFunctionSwapMouseButton&Libuser32_(ByValbSwapaslong)要交换鼠标按键,将bSwap参数设置为True。要恢复正常设置,将bSwap设置为False。然后调用函数就可以交换和恢复鼠标按键了。10、如何让窗体的标题条闪烁以引起用户注意?在窗体中放一个Timer控件Timer1,设置其Inteval=200*API函数声明PrivateDeclareFunctionFlashWindowLibuser32(ByValhwndAsLong,ByValbInvertAsLong)AsLong注释:在窗体中写下如下代码:PrivateSubTimer1_Timer()FlashWindowMe.hwnd,TrueEndSub11、怎样找到鼠标指针的XY坐标?*API函数声明TypePOINTAPIxAsLongyAsLongEndTypeDeclareFunctionGetCursorPosLibuser32(lpPointAsPOINTAPI)AsLong调用:GetCursorPoszprintz.xprintz.y12、怎样获得和改变双击鼠标的时间间隔?获得鼠标双击间隔时间:PublicDeclareFunctionGetDoubleClickTimeLibuser32Alias_GetDoubleClickTime()AsLong获得鼠标双击间隔时间:DeclareFunctionSetDoubleClickTimeLibuser32AliasSetDoubleClickTime(ByValwCountAsLong)AsLong注释:注意:这种改变将影响到整个操作系统以上两个函数都可精确到毫秒级。13、在程序中如何打开和关闭光驱门?*API函数声明如下:PrivateDeclareFunctionmciSendStringLibwinmm.dllAliasmciSendStringA(ByVallpstrCommandAsString,ByVallpstrReturnStringAsString,ByValuReturnLengthAsLong,ByValhwndCallbackAsLong)AsLong注释:调用时的代码如下DimRetAsLongDimRetStrAsString注释:打开光驱门Ret=mciSendString(setCDAudiodooropen,RetStr,0,0)注释:关闭光驱门Ret=mciSendString(setCDAudiodoorclosed,RetStr,0,0)14、如何获得Windows启动方式?在Form1中加入一个CommandButton、一个Label并加入如下代码:PrivateDeclareFunctionGetSystemMetricsLibuser32(ByValnIndexAsLong)AsLongConstSM_CLEANBOOT=67PrivateSubCommand1_Click()SelectCaseGetSystemMetrics(SM_CLEANBOOT)Case1Label1=安全模式.Case2Label1=支持网络的安全模式.CaseElseLabel1=Windows运行在普通模式.EndSelectEndSub15、怎样使Ctrl-Alt-Delete无效?*API函数声明PrivateDeclareFunctionSystemParametersInfoLibuser32AliasSystemParametersInfoA(ByValuActionAsLong,ByValuParamAsLong,ByVallpvParamAsAny,ByValfuWinIniAsLong)AsLong编写如下函数:SubDisableCtrlAltDelete(bDisabledAsBoolean)DimXAsLongX=SystemParametersInfo(97,bDisabled,CStr(1),0)EndSub使Ctrl-Alt-Delete无效:CallDisableCtrlAltDelete(True)恢复Ctrl-Alt-Delete:CallDisableCtrlAltDelete(False)16、如何移动没有标题栏的窗口?我们一般是用鼠标按住窗口的标题栏,然后移动窗口,当窗口没有标题栏时,我们可以用下面的方法来移动窗口:*API函数声明:DeclareFunctionRe
本文标题:VBA中API_函数集锦
链接地址:https://www.777doc.com/doc-2865680 .html