麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Delphi > 正文

用Delphi編寫系統進程監控程序

2019-11-18 17:59:31
字體:
來源:轉載
供稿:網友

 本程序通過調用kernel32.dll中的幾個API 函數,搜索并列出系統中除本進程外的所有進程的ID、對應的文件說明符、優先級、CPU占有率、線程數、相關進程信息等有關信息,并可中止所選進程。
  本程序運行時會在系統托盤區加入圖標,不會出現在按Ctrl+Alt+Del出現的任務列表中,也不會在任務欄上顯示任務按鈕,在不活動或最小化時會自動隱藏。不會重復運行,若程序已經運行,再想運行時只會激活已經運行的程序。
本程序避免程序反復運行的方法是比較獨特的。因為筆者在試用網上介紹一些方法后,發現程序從最小化狀態被激活時,單擊窗口最小化按鈕時,窗口卻不能最小化。于是筆者采用了發送和處理自定義消息的方法。在程序運行時先枚舉系統中已有窗口,若發現程序已經運行,就向該程序窗口發送自定義消息,然后結束。已經運行的程序接到自定義消息后顯示出窗口。

//工程文件PRocviewpro.dpr
program procviewpro;

uses
Forms, windows, messages, main in 'procview.pas' {Form1};

{$R *.RES}
{
//這是系統自動的
begin
application.Initialize;
Application.Title :='系統進程監控';
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
}

var
myhwnd:hwnd;

begin
myhwnd := FindWindow(nil, '系統進程監控'); // 查找窗口
if myhwnd=0 then // 沒有發現,繼續運行
begin
Application.Initialize;
Application.Title :='系統進程監控';
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else //發現窗口,發送鼠標單擊系統托盤區消息以激活窗口
postmessage(myhwnd,WM_SYSTRAYMSG,0,wm_lbuttondown);
{
//下面的方法的缺點是:若窗口原先為最小化狀態,激活后單擊窗口最小化按鈕將不能最小化窗口
showwindow(myhwnd,sw_restore);
FlashWindow(MYHWND,TRUE);
}
end.

{
//下面是使用全局原子的方法避免程序反復運行
const
atomstr='procview';

var
atom:integer;
begin
if globalfindatom(atomstr)=0 then
begin
atom:=globaladdatom(atomstr);
with application do
begin
Initialize;
Title := '系統進程監控';
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,'請先選擇要終止的進程!','操作提示',MB_OK+MB_ICONINFORMATION);
end
else
begin
if messagebox(form1.handle,pchar('終止'+itemfocused.caption+'?')
,'終止進程',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); //隱藏任務欄上的任務按鈕
OnMinimize:=AppOnMinimize; //最小化時自動隱藏
OnDeactivate:=FormDeactivate; //不活動時自動隱藏
OnActivate:=btnRefreshClick;
end;
RegisterServiceProcess(GetcurrentProcessID,1); //將程序注冊為系統服務程序,以避免出現在任務列表中
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:='系統進程監控';
end;
Shell_NotifyIcon(NIM_ADD,@SysTrayIcon); //將程序圖標加入系統托盤區
with lvSysProc do
begin
SmallImages:=TImageList.CreateSize(16,16);
SmallImages.ShareImages:=True;
end;
FormOldWidth:=self.Width;
FormOldHeight:=self.Height;
end;

//最小化時自動隱藏
procedure Tform1.AppOnMinimize(Sender:TObject);
begin
ShowWindow(application.handle,SW_HIDE);
end;

//響應鼠標在系統托盤區圖標上點擊
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); //取消系統托盤區圖標
RegisterServiceProcess(GetcurrentProcessID,0); //取消系統服務程序的注冊
lvSysProc.SmallImages.Free;
end;

//不活動時自動隱藏
procedure TForm1.FormDeactivate(Sender: TObject);
begin
application.minimize;
end;


procedure TForm1.lblEmailClick(Sender: TObject);
begin
if ShellExecute(Handle,'Open',Pchar('Mailto:[email protected]'),nil,nil,SW_SHOW)<33 then
MessageBox(form1.Handle,'無法啟動電子郵件軟件!','我很遺憾',MB_ICONINFORMATION+MB_OK);
end;

//當窗體大小改變時調整各組件位置
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中文版中均能正常編譯和運行。大家有什么問題請Email to:[email protected]與我討論。

后記:
  上面的代碼中RegisterServiceProcess()是win 9x才有的未公開的api函數.

  在學習masm32后,用masm32重寫并改進了這個程序
  有興趣的朋友可以下載最新的版本:
http://www.hcny.gov.cn/netres/download/procview.rar


上一篇:如何使用Delphi設計強大的服務器程序

下一篇:Delphi中建議使用的語句

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 久久国产精品二国产精品 | 国产精品久久久久网站 | 国产精品一区99 | 免费国产一区二区视频 | 午夜伊人| 美国av免费看 | 国产午夜精品一区二区三区四区 | 欧美成在线视频 | 欧美a视频 | 国产亚洲精品久久久久久大师 | 久久欧美亚洲另类专区91大神 | 成人午夜在线免费观看 | 中文字幕精品在线视频 | japan护士性xxxⅹhd | 久久免费看片 | 91久久久久久久久久久久久 | 亚洲精华液久久含羞草 | a视频在线看 | 国产精品久久久久久一区二区三区 | 91女上位 在线播放 性欧美日本 | 最近中文字幕一区二区 | 日韩视频一区 | 中国女人内谢8xxxxxx在 | 国产99久久精品一区二区 | 日韩精品二区 | 韩国精品视频在线观看 | 久久男 | 亚洲日本欧美 | 免费毛片观看 | 国产午夜亚洲精品午夜鲁丝片 | 亚洲狠狠入 | 99成人精品视频 | 久久综合久久精品 | 久久精品在线免费观看 | 毛片视频大全 | 99精品在线视频观看 | 国产亚洲精品综合一区91555 | 国产91在线免费 | 免费一级特黄欧美大片勹久久网 | 国产免费v片 | 日韩视频一区在线 |