天极网 邹刚
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。
-
vb教程之利用WindowsAPI获得系统高级功能
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
JavaScript判断两个数组相等的四类方法
js如何操作video标签
React实战--利用甘特图和看板,强化Paas平
【记录】正则替换的偏方
前端下载 Blob 类型整理
抽象语法树AST必知必会
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程