星期四, 4月 30, 2009

取得最上層的視窗名稱及程序名稱


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, psAPI, ExtCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
function GetProcessPath(PID : Integer): String;
var
hProcess: THandle;
hMod : hModule;
cbNeeded: DWORD;
szProcessName: array[0..1024] of Char;
begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION OR PROCESS_VM_READ,
FALSE, PID );
szProcessName := 'unknown';
if (hProcess<>0) then
begin
if (EnumProcessModules(hProcess,@hMod,sizeof(hMod),cbNeeded)) then
begin
GetModuleFileNameEx(hProcess,hMod,szProcessName,sizeof(szProcessName));
Result := StrPas(szProcessName);
end;
end;
CloseHandle(hProcess);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
wnd : THandle;
szText: array[0..254] of char;
ThreadId: DWORD;
FileName:string;
begin
wnd:= GetForegroundWindow;
if wnd <> 0 then
begin
if GetWindowText(wnd, @szText, 255)>0 then
Label1.caption := szText;

GetWindowThreadProcessId(wnd, ThreadId);
FileName:=GetProcessPath(ThreadId);
Label2.caption :=FileName;
end;
end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
Button1.Click;
end;

end.

星期六, 4月 04, 2009

讓滑鼠游標漏辦不要再現


{$R *.RES}
begin
Application.Initialize;
Screen.Cursors[crHourGlass] := Screen.Cursors[crDefault];
Application.CreateForm(TForm1, Form1);
Application.Run;
end.