创建增强型“任务管理器”
?牐牫绦蛏杓疲?
?牐?1.启动VB 6.0,点击菜单“外接程序/外接程序管理器”打开外接程序管理器窗口,在可用外部列表框中选择“VB 6 API Viewer”选项,然后在右下角的“加载行为”中勾选“加载/卸载”复选框,然后点击“确定”按钮将“API浏览器”添加到“外接程序”菜单中,点击“API浏览器”选项,打开“API浏览器”窗口,点击“文件/加载文本文件”,在打开的“选择一个文本API文件”对话框中选择“WIN32API”文件,然后点击“打开”按钮,在“API类型”为“声明”的情况下,设置“声明范围”为“私有”,添加“ GetWindowText、GetWindow、GetWindowTextLength、SendMessage、SetWindowPos”函数到“选定项”窗口中;然后选择“API类型”为“常数”,添加“SWP_NOSIZE、HWND_TOPMOST、WM_CLOSE、GW_HWNDNEXT、GW_HWNDFIRST”到“选定项”窗口中,最后点击“插入”按钮将代码插入Form1中。(^36020503a^)
?牐?2.利用控件工具箱上的命令按钮控件,在窗体上添加两个命令按钮(Command1.caption=“结束任务(&E)”、Command2.caption=“退出(&C)”,然后在窗体上添加一个列表框(List1)和一个标签框(Label1)。
?牐?3.然后打开代码窗口输入如下的源代码:
?牐燩rivate Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
?牐燩rivate Declare Function GetWindow Lib "user32"(ByVal hwnd As Long, ByVal wCmd As Long)As Long
?牐燩rivate Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
?牐燩rivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)As Long
?牐燩rivate Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
?牐燩rivate Const SWP_NOSIZE = &H1
?牐燩rivate Const HWND_TOPMOST = -1
?牐燩rivate Const WM_CLOSE = &H10
?牐燩rivate Const GW_HWNDNEXT = 2
?牐燩rivate Const GW_HWNDFIRST = 0
?牐燩rivate Windowstext As String *256
?牐燩rivate Programs(200)As Integer
?牐燩rivate Sub RunningProgram()
?牐牐Р檎宜姓谠诵械某绦?
?牐燚im GetwinValue As Integer,Programnums As Integer
?牐燚im GetwinTextValue As Long,windowstextLength As Long
?牐燩rogramnums = 0
?牐燣ist1.Clear
?牐燝etwinValue = GetWindow(Form1.hwnd.GW_HWNDFIRST)??
?牐燚o
?牐燝etwinTextValue = GetWindowText(GetwinValue,Windowstext,256)
?牐營f GetwinTextValue <> 0 Then
?牐爓indowstextLength = GetWindowTextLength?烥etwinValue??
?牐營f Left(Windowstext,windowstextLength)<> Form1.Caption And Left(Windows,windowstextLength)<> App.Title Then
?牐燜orm1.List1.AddItem Windowstext
?牐燩rograms(Programnums)= GetwinValue
?牐燩rogramnums = Programnums + 1
?牐燛nd If
?牐燛nd If
?牐燝etwinValue = GetWindow(GetwinValue,GW_HWNDNEXT)
?牐燣oop Until GetwinValue = 0
?牐燛nd Sub
?牐燩rivate Sub Command1_Click()
?牐牐Ч乇昭《ǖ恼谠诵械某绦?
?牐燚im GetwinTextValue As Long
?牐營f MsgBox("确实要关闭" & "“ " & List1.List(List1.ListIndex) & " ”",vbYesNo + vbQuestion, "确认框")= vbYes Then
?牐燝etwinTextValue = SendMessage(Programs?烲ist1.ListIndex),WM_CLOSE,0,0)
?牐營f GetwinTextValue <> 0 Then
?牐燤sgBox "对不起,当前程序不能关闭!",vbCritical, "不成功"
?牐燙all RunningProgram
?牐燛lse
?牐燤sgBox "程序已经关闭!,vbInformation,"成功"
?牐燙all RunningProgram
?牐燛nd If
?牐燛nd If
?牐燛nd Sub
?牐燩rivate Sub Command2_Click()
?牐燛nd '关闭程序
?牐燛nd Sub
?牐燩rivate Sub Form_Load()
?牐牐Ы绦虼翱谥糜谒写翱谥?
?牐燬etWindowPos Form1.hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE
?牐燙all RunningProgram
?牐燜orm1.Icon = LoadPicture()
?牐燜orm1.Caption = "程序管理器"
?牐燣abel1.Caption = "警告:“结束任务”有可能导致系统崩溃。在结束之前请确认程序用途。"
?牐燛nd Sub
?牐?4.运行以上程序,在列表框中列出所有正在运行的程序(见图^36020503a^),选中要关闭的程序,点击“结束任务”按钮,出现“确认”对话框,点击“是”则关闭程序,同时自动更新程序列表。以上程序在VB 6.0中文版和Windows 98/Me环境下调试运行通过。