Advertisement
rikokurniawan

Task Manager DAL

Jan 15th, 2013
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 10.47 KB | None | 0 0
  1. {
  2. Programmer : Jean Riko Kurniawan Putra
  3. blog Delphi Anime Lover / Djean Software : djeansoftware.blogspot.com
  4. }
  5. unit UTM;
  6.  
  7. interface
  8.  
  9. uses
  10.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  11.   Dialogs, ExtCtrls, Menus, ComCtrls, StdCtrls, Grids, TlHelp32, Shellapi;
  12.  
  13. type
  14.   TFTask = class(TForm)
  15.     PageControl1: TPageControl;
  16.     TabSheet1: TTabSheet;
  17.     StringGrid1: TStringGrid;
  18.     TabSheet2: TTabSheet;
  19.     GroupBox1: TGroupBox;
  20.     PB1: TPaintBox;
  21.     GroupBox2: TGroupBox;
  22.     pb2: TPaintBox;
  23.     StatusBar1: TStatusBar;
  24.     MainMenu1: TMainMenu;
  25.     Setting1: TMenuItem;
  26.     mnu11: TMenuItem;
  27.     PopupMenu1: TPopupMenu;
  28.     EndProcess1: TMenuItem;
  29.     Timer1: TTimer;
  30.     procedure FormShow(Sender: TObject);
  31.     procedure FormCreate(Sender: TObject);
  32.     procedure mnu11Click(Sender: TObject);
  33.     procedure EndProcess1Click(Sender: TObject);
  34.     procedure Timer1Timer(Sender: TObject);
  35.   private
  36.     { Private declarations }
  37.     Function GetCpuUsage(a:Byte):Extended;
  38.     Function ListProcesses(ProcessNameList :TStrings; ProcessPidList :TStrings; ProcessUsageList :TStrings):Boolean;
  39.     Function ShowList(ProcessNameList :TStrings; ProcessPidList :TStrings; ProcessUsageList :TStrings):Boolean;
  40.   public
  41.     { Public declarations }
  42.   end;
  43.  
  44. var
  45.   FTask: TFTask;
  46.   total: Extended;
  47.   donthide: Boolean;
  48.   usagelist,pidlist,pidlisttemp,kerneltimelist,kerneltimelisttemp,usertimelist,usertimelisttemp,dtlist,dtlisttemp: TStrings;
  49.  
  50. implementation
  51.  
  52. {$R *.dfm}
  53.  
  54. Function  TFTask.GetCpuUsage(a:byte):Extended;
  55. var
  56.    i:Shortint;
  57.    usertime,kerneltime,delaytime:Extended;
  58. begin
  59.   i:=pidlisttemp.IndexOf(pidlist.Strings[a]);//checks if this a new process or not
  60.   if i = -1 then //if this is a new process then it's informations will be saved
  61.     begin
  62.       pidlisttemp.Add(pidlist.Strings[a]);
  63.       kerneltimelisttemp.Add(kerneltimelist.Strings[a]);
  64.       usertimelisttemp.Add(usertimelist.Strings[a]);
  65.       dtlisttemp.Add(dtlist.Strings[a]);
  66.       Result:=0;
  67.     end
  68.   else
  69.     begin
  70.       delaytime:=strtofloat(dtlist.Strings[a])-strtofloat(dtlisttemp.Strings[i]);
  71.       kerneltime:=strtofloat(kerneltimelist.Strings[a])-strtofloat(kerneltimelisttemp.Strings[i]);
  72.       usertime:=strtofloat(usertimelist.Strings[a])-strtofloat(usertimelisttemp.Strings[i]);
  73.       { CPU Usage Gets From ( ( (NewUserTime-OldUsertime) + (NewKernelTime-OldKernelTime) ) / (Time Between These Two Calculate) }
  74.       Result:=(usertime+kerneltime)/(delaytime*100);
  75.       if Result<0 then
  76.       Result:=0;
  77.       dtlisttemp.Strings[i]:=dtlist.Strings[a];
  78.       kerneltimelisttemp.Strings[i]:=kerneltimelist.Strings[a];
  79.       usertimelisttemp.Strings[i]:=usertimelist.Strings[a];
  80.     end;
  81. end;
  82.  
  83. Function TFTask.ListProcesses(ProcessNameList :TStrings; ProcessPidList :TStrings; ProcessUsageList :TStrings):Boolean;
  84. var
  85.   ExeName : String;
  86.   _vt,_ct,_et,_kt,_ut:_FILETIME;
  87.   kt,ut:int64;
  88.   b1:LongBool;
  89.   prnum:Byte;
  90.   cpuuse:Extended;
  91.   proc: PROCESSENTRY32;
  92.   hSnap: HWND;
  93.   pid, k1: cardinal;// k1 is the Application Id Which Windows Uses In Kernel
  94.   Looper: BOOL;
  95. begin
  96.   total:=0;
  97.   prnum:=0;// I Think It's a bug coz In BDS2006 local variables don't automatically change to zero so we should do it
  98.   pidlist.Clear;
  99.   usertimelist.Clear;
  100.   kerneltimelist.Clear;
  101.   dtlist.Clear;
  102.   proc.dwSize := SizeOf(Proc);//Give proc.dwSize the Size of bytes of PROCESSENTRY32
  103.   hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
  104.   Looper := Process32First(hSnap,proc);
  105.   b1:=False;
  106.   pidlist.Clear;
  107.   kerneltimelist.Clear;
  108.   usertimelist.Clear;
  109.   while Integer(Looper) <> 0 do begin
  110.     ExeName := ExtractFileName(proc.szExeFile);
  111.     k1:=OpenProcess($1F0FFF,b1,proc.th32ProcessID);//Don't ask me why just do It! this is a Routine I don't know Where of Memory is :$1F0FFF
  112.     GetProcessTimes(k1,_ct,_et,_kt,_ut);// This procedure Returns value of used time of cpu for a certain Application. _et & _ct should be used But We don't need them.
  113.     dtlist.Add(inttostr(GetTickCount));// This list saves time in miliseconds for further use
  114.     kt:=_kt.dwHighDateTime;// kt is kernel time
  115.     kt:=kt * 4294967296;// Use This multiple BeCause ( shl ) and ( shr ) don't work with 64bit variables
  116.     kt:=kt+_kt.dwLowDateTime;// Kt is total of _kt.dwLowDateTime and _kt.dwHighDateTime
  117.     ut:=_ut.dwHighDateTime;// ut Is User Time
  118.     ut:=ut * 4294967296;
  119.     ut:=ut+_ut.dwLowDateTime;
  120.     ProcessPidList.Add(inttostr(proc.th32ProcessID));
  121.     pidlist.Add(inttostr(proc.th32ProcessID));// pid is Process ID and pidlist stores them
  122.     kerneltimelist.Add(inttostr(kt));
  123.     usertimelist.Add(inttostr(ut));
  124.     cpuuse:=GetCpuUsage(prnum);// cpuuse is Used Cpu Usage by current Application
  125.     ProcessNameList.Add(ExeName);
  126.     ProcessUsageList.Add(FormatFloat('00.00',cpuuse));
  127.     total:=total + strtofloat(ProcessUsageList.Strings[prnum]);// total is total of used cpu by all of applications
  128.     Looper := Process32Next(hSnap,proc);//Looper is oposite Zero until there is a program to process by this function
  129.     prnum:=prnum+1;//This calculates number of Applications
  130.   end;
  131.   CloseHandle(hSnap);
  132. end;
  133.  
  134. Function TFTask.ShowList(ProcessNameList :TStrings; ProcessPidList :TStrings; ProcessUsageList :TStrings):Boolean;
  135. var
  136.   I: Integer;
  137. begin
  138.   if PageControl1.ActivePage = TabSheet1 then begin
  139.     StringGrid1.RowCount:=ProcessNameList.Count + 1;
  140.     for I := 0 to ProcessNameList.Count - 1 do begin
  141.       StringGrid1.Cells[0,i+1]:=inttostr(i+1);
  142.       StringGrid1.Cells[1,i+1]:=ProcessNameList.Strings[i];
  143.       StringGrid1.Cells[2,i+1]:=ProcessUsageList.Strings[i];
  144.       StringGrid1.Cells[3,i+1]:=ProcessPidList.Strings[i];
  145.     end;
  146.   end;
  147.   StatusBar1.Panels[0].Text:='Processes : ' + inttostr(ProcessNameList.Count);
  148.   StatusBar1.Panels[1].Text:='CPU Usage : ' + floattostr(total) + '%';
  149. end;
  150.  
  151. procedure TFTask.FormShow(Sender: TObject);
  152. begin
  153.   With StatusBar1 Do Begin
  154.     Panels[0].Width:=Self.Width div 3;
  155.     Panels[1].Width:=Self.Width div 3;
  156.     Panels[2].Width:=Self.Width div 3;
  157.   End;
  158. end;
  159.  
  160. procedure TFTask.FormCreate(Sender: TObject);
  161. begin
  162.   pidlist:=TStringList.Create;
  163.   kerneltimelist:=TStringList.Create;
  164.   usertimelist:=TStringList.Create;
  165.   dtlist:=TStringList.Create;
  166.   pidlisttemp:=TStringList.Create;
  167.   kerneltimelisttemp:=TStringList.Create;
  168.   usertimelisttemp:=TStringList.Create;
  169.   dtlisttemp:=TStringList.Create;
  170.   usagelist:=TStringList.Create;
  171.   StringGrid1.Cells[1,0]:='Nama';
  172.   StringGrid1.Cells[2,0]:='Pemakaian Cpu';
  173.   StringGrid1.Cells[3,0]:='Cpu ID';
  174. end;
  175.  
  176. procedure TFTask.mnu11Click(Sender: TObject);
  177. begin
  178.   if mnu11.Checked then begin
  179.     mnu11.Checked:=False;
  180.     Self.FormStyle:=fsNormal;
  181.   end
  182.   else begin
  183.     mnu11.Checked:=True;
  184.     Self.FormStyle:=fsStayOnTop;
  185.   end;
  186. end;
  187.  
  188. procedure TFTask.EndProcess1Click(Sender: TObject);
  189. begin
  190.   if MessageDlg('Are You Sure You Want To Terminate This Process ?', mtConfirmation, [mbYes,mbNo], 0) = mrYes then
  191.   if Not TerminateProcess(OpenProcess(PROCESS_TERMINATE,Bool(1),strtoint(pidlist.Strings[StringGrid1.Selection.Top - 1])),0) then  //Terminates the OpenProcess
  192.   MessageDlg('This Process Is A Critical System Application So Wont Be Closed', mtError, [mbOk], 0);
  193. end;
  194.  
  195. procedure TFTask.Timer1Timer(Sender: TObject);
  196. var
  197.   i,i1: Byte;
  198.   lf : TLogFont;
  199.   tf : TFont;
  200.   ProcessNameList, ProcessPidList, ProcessUsagelist: TStrings;
  201. begin
  202.   ProcessNameList:=TStringList.Create;
  203.   ProcessPidList:=TStringList.Create;
  204.   ProcessUsagelist:=TStringList.Create;
  205.   ListProcesses(ProcessNameList,ProcessPidList,ProcessUsagelist);
  206.   if total>100 then total:=100;
  207.   usagelist.Add(inttostr(trunc((total * 85) / 100) + 1 ));
  208.   if usagelist.Count > 100 then begin
  209.     usagelist.Move(100,0);
  210.     usagelist.Delete(100);
  211.   end; //end if usagelist.Count
  212.   if PageControl1.ActivePage=TabSheet2 then begin
  213.     with pb2.Canvas do begin
  214.       Pen.Color:=$0046EEEA;
  215.       Pen.Width:=1;
  216.       Brush.Style:=bsSolid;
  217.       Brush.Color:=ClBlack;
  218.       Rectangle(0,0,551,89);
  219.       FloodFill(1,1,ClBlack,fsSurface);
  220.       Pen.Color:=ClLime;
  221.       Pen.Width:=2;
  222.       MoveTo(525,88 - strtoint(usagelist.Strings[0]));
  223.       for I := 1 to usageList.Count - 1 do LineTo(525 -(5*i),88 - strtoint(usagelist.Strings[i]));
  224.     end; //end with pb2.Canvas
  225.     with pb1.Canvas do begin
  226.       Pen.Color:=$0046EEEA;
  227.       Pen.Width:=1;
  228.       Brush.Style:=bsSolid;
  229.       Brush.Color:=ClBlack;
  230.       FloodFill(1,1,ClBlack,fsSurface);
  231.       Rectangle(0,0,551,200);
  232.       Brush.Color := clBtnFace;
  233.       Rectangle(0,202,551,313);
  234.       FloodFill(1,1,clBtnFace,fsSurface);
  235.       Font.Charset:=ARABIC_CHARSET;
  236.       Font.Name := 'Tahoma';
  237.       Font.Size := 9;
  238.       Font.Style := Font.Style - [fsBold];
  239.       Font.Color := clBlack;
  240.       tf := TFont.Create;
  241.       try//This piece of code will rotate text 90 degrees. Special thanks to Zarko Gajic About.com Guide to Delphi Programming
  242.         tf.Assign(Font);
  243.         GetObject(tf.Handle, sizeof(lf), @lf);
  244.         lf.lfEscapement := 900;
  245.         tf.Handle := CreateFontIndirect(lf);
  246.         Font.Assign(tf);
  247.       finally
  248.         tf.Free;
  249.       end; //end try
  250.     end; //end with pb1.Canvas
  251.     for I := 0 to ProcessNameList.Count - 1 do begin
  252.       i1:=trunc(strtofloat(ProcessUsagelist.Strings[i]));
  253.       case i1 of
  254.       0..33   :
  255.         begin
  256.           pb1.Canvas.Brush.Color:=ClGreen;
  257.           pb1.Canvas.Rectangle((i*15) + 1, 199-(trunc(strtofloat(ProcessUsagelist.Strings[i]))*2),(i*15)+14,200);
  258.           pb1.Canvas.FloodFill((i*15) + 1, 199, ClGreen,fsSurface);
  259.         end;
  260.       34..67  :
  261.         begin
  262.           pb1.Canvas.Brush.Color:=ClYellow;
  263.           pb1.Canvas.Rectangle((i*15) + 1, 199-(trunc(strtofloat(ProcessUsagelist.Strings[i]))*2),(i*15)+14,200);
  264.           pb1.Canvas.FloodFill((i*15) + 1, 199, ClYellow,fsSurface);
  265.         end;
  266.       68..100 :
  267.         begin
  268.           pb1.Canvas.Brush.Color:=ClRed;
  269.           pb1.Canvas.Rectangle((i*15) + 1, 199-(trunc(strtofloat(ProcessUsagelist.Strings[i]))*2),(i*15)+14,200);
  270.           pb1.Canvas.FloodFill((i*15) + 1, 199, ClRed,fsSurface);
  271.         end;
  272.       end; // end case i1 of
  273.       with Pb1.Canvas do begin
  274.         Brush.Color := clBtnFace;
  275.         TextOut((i*15) + 1, 308, ProcessNameList.Strings[i]);
  276.       end; // end  with Pb1.Canvas
  277.     end; //end for I := 0
  278.     pb1.Update;
  279.   end; //end if PageControl1.ActivePage=TabSheet2
  280.   ShowList(ProcessNameList,ProcessPidList,ProcessUsagelist);
  281.   ProcessNameList.Free;
  282.   ProcessPidList.Free;
  283.   ProcessUsagelist.Free;
  284. end;
  285.  
  286. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement