本程序通過(guò)調(diào)用kernel32.dll中的幾個(gè)API 函數(shù),搜索并列出系統(tǒng)中除本進(jìn)程外的所有進(jìn)程的ID、對(duì)應(yīng)的文件說(shuō)明符、優(yōu)先級(jí)、CPU占有率、線程數(shù)、相關(guān)進(jìn)程信息等有關(guān)信息,并可中止所選進(jìn)程。 本程序運(yùn)行時(shí)會(huì)在系統(tǒng)托盤區(qū)加入圖標(biāo),不會(huì)出現(xiàn)在按Ctrl+Alt+Del出現(xiàn)的任務(wù)列表中,也不會(huì)在任務(wù)欄上顯示任務(wù)按鈕,在不活動(dòng)或最小化時(shí)會(huì)自動(dòng)隱藏。不會(huì)重復(fù)運(yùn)行,若程序已經(jīng)運(yùn)行,再想運(yùn)行時(shí)只會(huì)激活已經(jīng)運(yùn)行的程序。 本程序避免程序反復(fù)運(yùn)行的方法是比較獨(dú)特的。因?yàn)楣P者在試用網(wǎng)上介紹一些方法后,發(fā)現(xiàn)程序從最小化狀態(tài)被激活時(shí),單擊窗口最小化按鈕時(shí),窗口卻不能最小化。于是筆者采用了發(fā)送和處理自定義消息的方法。在程序運(yùn)行時(shí)先枚舉系統(tǒng)中已有窗口,若發(fā)現(xiàn)程序已經(jīng)運(yùn)行,就向該程序窗口發(fā)送自定義消息,然后結(jié)束。已經(jīng)運(yùn)行的程序接到自定義消息后顯示出窗口。
//工程文件procviewpro.dpr program procviewpro;
uses Forms, windows, messages, main in 'procview.pas' {Form1};
{$R *.RES} { //這是系統(tǒng)自動(dòng)的 begin Application.Initialize; Application.Title :='系統(tǒng)進(jìn)程監(jiān)控'; Application.CreateForm(TForm1, Form1); Application.Run; end. }
var myhwnd:hwnd;
begin myhwnd := FindWindow(nil, '系統(tǒng)進(jìn)程監(jiān)控'); // 查找窗口 if myhwnd=0 then // 沒有發(fā)現(xiàn),繼續(xù)運(yùn)行 begin Application.Initialize; Application.Title :='系統(tǒng)進(jìn)程監(jiān)控'; Application.CreateForm(TForm1, Form1); Application.Run; end else //發(fā)現(xiàn)窗口,發(fā)送鼠標(biāo)單擊系統(tǒng)托盤區(qū)消息以激活窗口 postmessage(myhwnd,WM_SYSTRAYMSG,0,wm_lbuttondown); { //下面的方法的缺點(diǎn)是:若窗口原先為最小化狀態(tài),激活后單擊窗口最小化按鈕將不能最小化窗口 showwindow(myhwnd,sw_restore); FlashWindow(MYHWND,TRUE); } end.
{ //下面是使用全局原子的方法避免程序反復(fù)運(yùn)行 const atomstr='procview';
var atom:integer; begin if globalfindatom(atomstr)=0 then begin atom:=globaladdatom(atomstr); with application do begin Initialize; Title := '系統(tǒng)進(jìn)程監(jiān)控'; CreateForm(TForm1, Form1); Run; end; globaldeleteatom(atom); end; end. }
//單元文件procview.pas unit procview;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, TLHelp32,Buttons, ComCtrls, ExtCtrls,ShellAPI, MyFlag;
const PROCESS_TERMINATE=0; SYSTRAY_ID=1; WM_SYSTRAYMSG=WM_USER+100;
type TForm1 = class(TForm) lvSysProc: TListView; lblSysProc: TLabel; lblAboutProc: TLabel; lvAboutProc: TListView; lblCountSysProc: TLabel; lblCountAboutProc: TLabel; Panel1: TPanel; btnDetermine: TButton; btnRefresh: TButton; lblOthers: TLabel; lblEmail: TLabel; MyFlag1: TMyFlag; procedure btnRefreshClick(Sender: TObject); procedure btnDetermineClick(Sender: TObject); procedure lvSysProcClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure AppOnMinimize(Sender:TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormDeactivate(Sender: TObject); procedure lblEmailClick(Sender: TObject); procedure FormResize(Sender: TObject); private { Private declarations } fshandle:thandle; FormOldHeight,FormOldWidth:Integer; procedure SysTrayOnClick(var message:TMessage);message WM_SYSTRAYMSG; public { Public declarations } end;
var Form1: TForm1; idid: dword; fp32:tprocessentry32; fm32:tmoduleentry32; SysTrayIcon:TNotifyIconData;
implementation
{$R *.DFM}
function RegisterServiceProcess(dwProcessID,dwType:integer):integer;stdcall;external 'KERNEL32.DLL';
procedure TForm1.btnRefreshClick(Sender: TObject); var clp:bool; newitem1:Tlistitem; MyIcon:TIcon;
IconIndex:word; ProcFile : array[0..MAX_PATH] of char;
begin MyIcon:=TIcon.create; lvSysProc.Items.clear; lvSysProc.SmallImages.clear; fshandle:=CreateToolhelp32Snapshot(th32cs_snapprocess,0); fp32.dwsize:=sizeof(fp32); clp:=process32first(fshandle,fp32); IconIndex:=0; while integer(clp)<>0 do begin if fp32.th32processid<>getcurrentprocessid then begin newitem1:=lvSysProc.items.add; { newitem1.caption:=fp32.szexefile; MyIcon.Handle:=ExtractIcon(Form1.Handle,fp32.szexefile,0); }
StrCopy(ProcFile,fp32.szExeFile); newitem1.caption:=ProcFile; MyIcon.Handle:=ExtractAssociatedIcon(HINSTANCE,ProcFile,IconIndex); if MyIcon.Handle<>0 then begin with lvSysProc do begin NewItem1.ImageIndex:=smallimages.addicon(MyIcon); end; end; with newitem1.subitems do begin add(IntToHex(fp32.th32processid,4)); Add(IntToHex(fp32.th32ParentProcessID,4)); Add(IntToHex(fp32.pcPriClassBase,4)); Add(IntToHex(fp32.cntUsage,4)); Add(IntToStr(fp32.cntThreads)); end; end; clp:=process32next(fshandle,fp32); end; closehandle(fshandle); lblCountSysProc.caption:=IntToStr(lvSysProc.items.count); MyIcon.Free; end;
procedure TForm1.btnDetermineClick(Sender: TObject); var processhndle:thandle; begin with lvSysProc do begin if selected=nil then begin messagebox(form1.handle,'請(qǐng)先選擇要終止的進(jìn)程!','操作提示',MB_OK+MB_ICONINFORMATION); end else begin if messagebox(form1.handle,pchar('終止'+itemfocused.caption+'?') ,'終止進(jìn)程',mb_yesno+MB_ICONWARNING+MB_DEFBUTTON2)=mryes then begin idid:=strtoint('$'+itemfocused.subitems[0]); processhndle:=openprocess(PROCESS_TERMINATE,bool(0),idid); if integer(terminateprocess(processhndle,0))=0 then messagebox(form1.handle,pchar('不能終止'+itemfocused.caption+'!') ,'操作失敗',mb_ok+MB_ICONERROR) else begin Selected.Delete; lvAboutProc.Items.Clear; lblCountSysProc.caption:=inttostr(lvSysProc.items.count); lblCountAboutProc.caption:=''; end end; end; end; end;
procedure TForm1.lvSysProcClick(Sender: TObject); var newitem2:Tlistitem; clp:bool; begin if lvSysProc.selected<>nil then begin idid:=strtoint('$'+lvSysProc.itemfocused.subitems[0]); lvAboutProc.items.Clear; fshandle:=CreateToolhelp32Snapshot(th32cs_snapmodule,idid); fm32.dwsize:=sizeof(fm32); clp:=Module32First(fshandle,fm32); while integer(clp)<>0 do begin newitem2:=lvAboutProc.Items.add; with newitem2 do begin caption:=fm32.szexepath; with newitem2.subitems do begin add(IntToHex(fm32.th32moduleid,4)); add(IntToHex(fm32.GlblcntUsage,4)); add(IntToHex(fm32.proccntUsage,4)); end; end; clp:=Module32Next(fshandle,fm32); end; closehandle(fshandle); lblCountAboutProc.Caption:=IntToStr(lvAboutProc.items.count); end end;
procedure TForm1.FormCreate(Sender: TObject); begin with application do begin showwindow(handle,SW_HIDE); //隱藏任務(wù)欄上的任務(wù)按鈕 OnMinimize:=AppOnMinimize; //最小化時(shí)自動(dòng)隱藏 OnDeactivate:=FormDeactivate; //不活動(dòng)時(shí)自動(dòng)隱藏 OnActivate:=btnRefreshClick; end; RegisterServiceProcess(GetcurrentProcessID,1); //將程序注冊(cè)為系統(tǒng)服務(wù)程序,以避免出現(xiàn)在任務(wù)列表中 with SysTrayIcon do begin cbSize:=sizeof(SysTrayIcon); wnd:=Handle; uID:=SYSTRAY_ID; uFlags:=NIF_ICON OR NIF_MESSAGE OR NIF_TIP; uCallBackMessage:=WM_SYSTRAYMSG; hIcon:=Application.Icon.Handle; szTip:='系統(tǒng)進(jìn)程監(jiān)控'; end; Shell_NotifyIcon(NIM_ADD,@SysTrayIcon); //將程序圖標(biāo)加入系統(tǒng)托盤區(qū) with lvSysProc do begin SmallImages:=TImageList.CreateSize(16,16); SmallImages.ShareImages:=True; end; FormOldWidth:=self.Width; FormOldHeight:=self.Height; end;
//最小化時(shí)自動(dòng)隱藏 procedure Tform1.AppOnMinimize(Sender:TObject); begin ShowWindow(application.handle,SW_HIDE); end;
//響應(yīng)鼠標(biāo)在系統(tǒng)托盤區(qū)圖標(biāo)上點(diǎn)擊 procedure tform1.SysTrayOnClick(var message:TMessage); begin with message do begin if (lparam=wm_lbuttondown) or (lparam=wm_rbuttondown) then begin application.restore; SetForegroundWindow(Handle); showwindow(application.handle,SW_HIDE); end; end; end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin Shell_NotifyIcon(NIM_DELETE,@SysTrayIcon); //取消系統(tǒng)托盤區(qū)圖標(biāo) RegisterServiceProcess(GetcurrentProcessID,0); //取消系統(tǒng)服務(wù)程序的注冊(cè) lvSysProc.SmallImages.Free; end;
//不活動(dòng)時(shí)自動(dòng)隱藏 procedure TForm1.FormDeactivate(Sender: TObject); begin application.minimize; end;
procedure TForm1.lblEmailClick(Sender: TObject); begin if ShellExecute(Handle,'Open',Pchar('Mailto:purpleendurer@163.com'),nil,nil,SW_SHOW)<33 then MessageBox(form1.Handle,'無(wú)法啟動(dòng)電子郵件軟件!','我很遺憾',MB_ICONINFORMATION+MB_OK); end;
//當(dāng)窗體大小改變時(shí)調(diào)整各組件位置 procedure TForm1.FormResize(Sender: TObject); begin with panel1 do top:=top+self.Height-FormOldHeight; with lvSysProc do begin width:=width+self.Width-FormOldWidth; end;
with lvAboutProc do begin height:=height+self.Height-FormOldHeight; width:=width+self.Width-FormOldWidth; end; FormOldWidth:=self.Width; FormOldHeight:=self.Height; end;
end.
以上程序在Delphi 2,Windows 95中文版和Delphi 5,Windows 97中文版中均能正常編譯和運(yùn)行。大家有什么問(wèn)題請(qǐng)Email to:purpleendurer@163.com與我討論。
作者:黃志斌 廣西河池地區(qū)經(jīng)濟(jì)學(xué)校 郵編:547000 Email: purpleendurer@163.com
|