创建增强型“任务管理器” 刘雄伟 2001年 36期 大家都知道,在Windows系统中,只要按下“Ctrl+Alt+Del”就会自动调出任务管理器,能查看和关闭正在执行的任务。然而,它所列出的也只是部分运行程序列表。下面,我们就用VB来创建一个增强型的“任务管理器”,它将从内存中获得所有正在运行的程序列表。不过,选择从这里关闭程序较容易引起系统不稳定,因此本程序仅作系统进程查看用途。   程序设计:   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.然后打开代码窗口输入如下的源代码:   Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long   Private Declare Function GetWindow Lib "user32"(ByVal hwnd As Long, ByVal wCmd As Long)As Long   Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long   Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)As Long   Private 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   Private Const SWP_NOSIZE = &H1   Private Const HWND_TOPMOST = -1   Private Const WM_CLOSE = &H10   Private Const GW_HWNDNEXT = 2   Private Const GW_HWNDFIRST = 0   Private Windowstext As String *256   Private Programs(200)As Integer   Private Sub RunningProgram()   '查找所有正在运行的程序   Dim GetwinValue As Integer,Programnums As Integer   Dim GetwinTextValue As Long,windowstextLength As Long   Programnums = 0   List1.Clear   GetwinValue = GetWindow(Form1.hwnd.GW_HWNDFIRST)   Do   GetwinTextValue = GetWindowText(GetwinValue,Windowstext,256)   If GetwinTextValue <> 0 Then   windowstextLength = GetWindowTextLengthGetwinValue   If Left(Windowstext,windowstextLength)<> Form1.Caption And Left(Windows,windowstextLength)<> App.Title Then   Form1.List1.AddItem Windowstext   Programs(Programnums)= GetwinValue   Programnums = Programnums + 1   End If   End If   GetwinValue = GetWindow(GetwinValue,GW_HWNDNEXT)   Loop Until GetwinValue = 0   End Sub   Private Sub Command1_Click()   '关闭选定的正在运行的程序   Dim GetwinTextValue As Long   If MsgBox("确实要关闭" & "“ " & List1.List(List1.ListIndex) & " ”",vbYesNo + vbQuestion, "确认框")= vbYes Then   GetwinTextValue = SendMessage(ProgramsList1.ListIndex),WM_CLOSE,0,0)   If GetwinTextValue <> 0 Then   MsgBox "对不起,当前程序不能关闭!",vbCritical, "不成功"   Call RunningProgram   Else   MsgBox "程序已经关闭!,vbInformation,"成功"   Call RunningProgram   End If   End If   End Sub   Private Sub Command2_Click()   End '关闭程序   End Sub   Private Sub Form_Load()   '将程序窗口置于所有窗口之上   SetWindowPos Form1.hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE   Call RunningProgram   Form1.Icon = LoadPicture()   Form1.Caption = "程序管理器"   Label1.Caption = "警告:“结束任务”有可能导致系统崩溃。在结束之前请确认程序用途。"   End Sub   4.运行以上程序,在列表框中列出所有正在运行的程序(见图^36020503a^),选中要关闭的程序,点击“结束任务”按钮,出现“确认”对话框,点击“是”则关闭程序,同时自动更新程序列表。以上程序在VB 6.0中文版和Windows 98/Me环境下调试运行通过。