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。
文章来源于领测软件测试网 https://www.ltesting.net/