看到很多关于API函数调用的, 感觉很有必要总结归纳并共享出来.
平时写程序时会用到系统用户名,现将实现方法贴出,大家分享!
1、申明API函数
FUNCTION ulong WNetGetUser(ref string lpName,ref string lpUserName,ref ulong lpnLength) LIBRARY "mpr.dll" ALIAS FOR "WNetGetUserA"
2、PB脚本语言
string ls_name, ls_username
ulong ll_len
ll_len = 256
ls_username = space(ll_len)
setnull(ls_name)
WNetGetUser(ls_Name,ls_UserName,ll_Len)
messagebox("系统登录用户名",ls_username)
以上脚本在WIN2000下测试通过
-----------------------------------------------------------------------------------------------------------------------------------------------
利用Api函数ShellAboutA可以显示一个与Windows操作系统风格一致的About对话框
关键字 显示一个与Windows操作系统风格一致的About对话框:
首先声明如下外部函数:
function int ShellAboutA(ulong al_hWnd, string as_szApp, string as_szOtherStuff, ulong hIcon) library "shell32.dll"
ShellAboutA(handle(parent),"关于学籍管理系统","欢迎使用学籍管理系统",0)
在应用程序中启动控制面板
在应用程序中启动控制面板,只需用ShellExecute函数打开对应的CPL文件即可,例如要在应用程序中修改Windows密码,只需打开Password.cpl文件,启动ODBC管理器只要打开ODBCCP32.CPL。
函数声明:
Function Long ShellExecute(Long hwindow, String lpOperation, String lpFile, String lpParameters, String lpDirectory, Long nShowCmd) Library 'shell32.dll' Alias for ShellExecuteA
Function Long GetDesktopWindow() Library 'user32.dll'
脚本如下:
String ls_cpl_name
String ls_null
SetNull(ls_null)
ls_cpl_name = "Password.cpl"
ShellExecute(GetDesktopWindow(), ls_null, 'rundll32.exe', "shell32.dll,Control_RunDLL " + ls_cpl_name + ",", ls_null, 0)
将指定的窗口带至窗口列表顶部
在应用程序中有时我们需要用Bringwindowtotop将指定的窗口带至窗口列表顶部。倘若它部分或全部隐藏于其他窗口下面,则将隐藏的部分完全显示出来。但是在某些情况下这个函数并不能达到我们想要的效果,倘若某窗口并非前台应用程序的一部分,那么一旦随同该窗口调用本函数,仍会将窗口带至它自己那个应用程序的窗口列表顶部。但是,不会同时使那个应用成为前台应用程序。这意味着在调用了本函数后,窗口仍会保持隐藏状态。那么如何使应用成为前台应用呢?我们可以使用SetForegroundWindow函数。
例如:
FUNCTION long FindWindowA( ulong Winhandle, string wintitle ) Library "user32"
FUNCTION ulong SetForegroundWindow(ulong hwnd) LIBRARY "user32.dll"
long ll_winhandle
ll_winhandle=FindWindowA( 0, "test" )
if ll_winhandle >0 then
SetForegroundWindow(ll_winhandle)
end if
获得Windows系统目录
在应用程序中有时需要用到Windows系统目录,用GetWindowsDirectory函数即可实现。
首先声明如下外部函数:
Function uint GetWindowsDirectoryA( &
ref string dirtext, uint textlen) &
library "KERNEL32.DLL"
Script如下:
String ls_WinPath
ls_WinPath = Space( 128 )
GetWindowsDirectoryA( ls_WinPath, 128 )
用ExitWindowEx关闭系统
为方便用户,有时需要在应用程序中增加退出并关闭计算机的功能,其实只要几行代码即可实现。首先声明API函数:
Function long ExitWindowsEx (long uFlags , long dwReserved ) Library "user32"
然后在程序中调用:
ExitWindowEx( 1, 0 )
参数型及说明:
uFlags Long,指定下述一个或多个标志(用OR运算符合并到一起)
EWX_FORCE 强迫中止没有响应的进程
EWX_LOGOFF 中止进程,然后注销
EWX_SHUTDOWN 关掉系统电源(如果可能的话,ATX电源就可以)
EWX_REBOOT 重新引导系统
EWX_SHUTDOWN 关闭系统
dwReserved Long,保留,设为零
-----------------------------------------------------------------------------------------------------------------------------------
用API函数实现显示隐藏任务栏!
首先声明如下外部函数:
Function long FindWindowExA ( long hWnd, long hWndChild, ref string pszClassName, ref string lpszWindow) library 'user32'
Function long ShowWindow (long hWnd, long nCmdShow ) library 'user32'
在Script中加入如下内容:
Constant Long SW_HIDE = 0
Constant Long SW_NORMAL = 1
Constant Long SW_SHOWMINIMIZED = 2
Constant Long SW_SHOWMAXIMIZED = 3
Constant Long SW_SHOWNOACTIVATE = 4
Constant Long SW_SHOW = 5
Constant Long SW_MINIMIZE = 6
Constant Long SW_SHOWMINNOACTIVE = 7
Constant Long SW_SHOWNA = 8
Constant Long SW_RESTORE = 9
Constant Long SW_SHOWDEFAULT = 10
String ls_ShellTaskBarWnd = "Shell_TrayWnd"
String ls_Null
Long ll_HTaskBar, ll_HDeskTop
ll_HTaskBar = FindWindowExA( 0, 0, ls_ShellTaskBarWnd, ls_Null )
ShowWindow( ll_HTaskBar, SW_HIDE )
MessageBox( "你好", "看不到任务栏了吧!呵,确定就恢复了!" )
ll_HTaskBar = FindWindowExA( 0, 0, ls_ShellTaskBarWnd, ls_Null )
ShowWindow( ll_HTaskBar, SW_SHOW )
其中脚本可以另外写,做到开关任务栏效果!!爽吧!