VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 利用Windows API获得系统高级功能
发表评论(0)作者:邹刚, 平台:VB6.0+Win98, 阅读:10507, 日期:2002-06-04
  VB无疑是最先进的编程工具之一,但在涉及windows 32位系统的核心编程方面——譬如一些高级功能的实现上,它仍然显得有些力不从心,这需要我们充分利用vb的强大的Windows API函数调用能力来弥补。以下代码将向您展示如何利用Windows API控制系统托盘(图标)区、开始菜单、任务栏以及禁用/起用ctrl-alt-del、退出网络登录、立即关机、重新启动等系统高级功能。

  启动VB6,建立一个标准exe工程,添加14个command按钮,caption属性依次为 “隐藏开始菜单”、“显示开始菜单”、“隐藏系统托盘”、“显示系统托盘”、“禁用 ctrl-alt-del”、“起用 ctrl-alt-del”、“隐藏任务栏”、“显示任务栏”、“立即重新启动”、“关机”、“退出网络登陆”、“显示关机对话框”,调整上述控件到适当位置,双击窗体,写入以下代码:

  

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
注释:寻找窗口列表中第一个符合指定条件的顶级窗口
注释:lpClassName指向包含了窗口类名的空中止(C语言)字串的指针;或设为零,注释:表示接收任何类
注释:lpWindowName指向包含了窗口文本(或标签)的空中止(C语言)字串的指针;注释:或设为零,表示接收任何窗口标题




Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
注释:控制窗口的可见性
注释:hwnd窗口句柄,要向这个窗口应用由nCmdShow指定的命令
注释:nCmdShow为窗口指定可视性方面的一个命令



  
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
注释:在窗口列表中寻找与指定条件相符的第一个子窗口
注释:hWnd1在其中查找子的父窗口
注释:hWnd2从这个窗口后开始查找。这样便可利用对FindWindowEx的多次调用找到符合条件的所有子窗口。如设为零,表示从第一个子窗口开始搜索




Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long)
注释:退出windows,并用特定的选项重新启动




Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
注释:允许获取和设置数量众多的windows系统参数
注释:uAction指定要设置的参数




Private Const EWX_LOGOFF = 0注释:中止进程,然后注销
Private Const EWX_SHUTDOWN = 1注释:关掉系统电源
Private Const EWX_REBOOT = 2注释:重新引导系统
Private Const EWX_FORCE = 4注释:强迫中止没有响应的进程
Private Const SPI_SCREENSAVERRUNNING = 97



Private Sub Command1_Click() 注释:隐藏开始菜单
Dim Handle As Long, FindClass As Long
FindClass = FindWindow("Shell_TrayWnd", "")
Handle = FindWindowEx(FindClass, 0, "Button", vbNullString)
ShowWindow Handle, 0
End Sub




Private Sub Command11_Click() 注释:起用 ctrl-alt-del
Dim ret As Integer
Dim pOld As Boolean
ret = SystemParametersInfo(SPI_SCREENSAVERRUNNING, True, pOld, 0)
End Sub




Private Sub Command12_Click() 注释:禁用 ctrl-alt-del
Dim ret As Integer
Dim pOld As Boolean
ret = SystemParametersInfo(SPI_SCREENSAVERRUNNING, False, pOld, 0)
End Sub




Private Sub Command13_Click() 注释:立即重新启动
ExitWindowsEx EWX_REBOOT, 0
End Sub



Private Sub Command14_Click() 注释:关机
ExitWindowsEx EWX_SHUTDOWN, 0
End Sub



Private Sub Command15_Click() 注释:退出网络登陆
ExitWindowsEx EWX_LOGOFF, 0
End Sub



Private Sub Command16_Click() 注释:显示关机对话框
ExitWindowsEx EWX_FORCE, 0
End Sub



Private Sub Command2_Click() 注释:显示开始菜单
Dim Handle As Long, FindClass As Long
FindClass = FindWindow("Shell_TrayWnd", "")
Handle = FindWindowEx(FindClass, 0, "Button", vbNullString)
ShowWindow Handle, 1
End Sub



Private Sub Command5_Click() 注释:隐藏系统托盘
Dim FindClass As Long, Handle As Long
FindClass = FindWindow("Shell_TrayWnd", "")
Handle = FindWindowEx(FindClass, 0, "TrayNotifyWnd", vbNullString)
ShowWindow Handle, 0
End Sub



Private Sub Command6_Click() 注释:显示系统托盘
Dim FindClass As Long, Handle As Long
FindClass = FindWindow("Shell_TrayWnd", "")
Handle = FindWindowEx(FindClass, 0, "TrayNotifyWnd", vbNullString)
ShowWindow Handle, 1
End Sub



Private Sub Command7_Click() 注释:显示任务栏
Dim FindClass As Long, FindClass2 As Long, Parent As Long, Handle As Long
FindClass = FindWindow("Shell_TrayWnd", "")
FindClass2 = FindWindowEx(FindClass, 0, "ReBarWindow32", vbNullString)
Parent = FindWindowEx(FindClass2, 0, "MSTaskSwWClass", vbNullString)
Handle = FindWindowEx(Parent, 0, "SysTabControl32", vbNullString)
ShowWindow Handle, 0
End Sub



Private Sub Command8_Click() 注释:隐藏任务栏
Dim FindClass As Long, FindClass2 As Long, Parent As Long, Handle As Long
FindClass = FindWindow("Shell_TrayWnd", "")
FindClass2 = FindWindowEx(FindClass, 0, "ReBarWindow32", vbNullString)
Parent = FindWindowEx(FindClass2, 0, "MSTaskSwWClass", vbNullString)
Handle = FindWindowEx(Parent, 0, "SysTabControl32", vbNullString)
ShowWindow Handle, 1
End Sub



Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
End
End Sub



  本文中的部分api函数及其相应的常数较少用到,希望编程爱好者能够有所借鉴和斧正,如果有问题欢迎与我联系zouhero@china.com。