woawn 发表于 2012-8-25 02:46:59

delphi 判断当前程序是否是活动窗口

虽然这个程序我没有运行成功但是这里面的程序很好,涉及的新知识比较多大家可以看看




uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,shellapi, StdCtrls,Tlhelp32;

function EnumWindowsProc(hwnd:HWND;lParam:DWORD):boolean; stdcall;
var
szCaption: array of Char;
c,h:integer;

begin
GetWindowText(hwnd,szCaption,127);
if length(szCaption)>0 then
begin
if trim(szCaption)<>'' then
if pos('qq2009',LowerCase(szCaption))>0 then
showmessage(inttostr(HWND));
{GetWindowThreadProcessId(HWND,@c); //这里是窗体名枚举 想判断某个窗体在这里写就成
if c=FindProcessID('calc.exe') then
showmessage(inttostr(HWND));
{if pos('QQ2009',szCaption) >0 then
begin
showmessage(szCaption);
h:=FindWindow(nil,szCaption);
showmessage(inttostr(h));
end;}
end;
result:=TRUE;
end;

function FindProcessID(s:string):integer;
var
found,find:boolean;
FSnapshotHandle:tHANDLE;
lppe:TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //CreateToolhelp32Snapshot函数得到进程快照
Find:=False;
lppe.dwSize := Sizeof(lppe); //初始化
found := Process32First(FSnapshotHandle, lppe); //Process32First 得到一个系统快照里第一个进程的信息
while found do
begin
if LowerCase(ExtractFileName(lppe.szExeFile))=LowerCase(s) then
begin
Result:=lppe.th32ProcessID; //找到进程返回ID
find:=true;
CloseHandle(FSnapshotHandle);
exit;
end;
found := Process32Next(FSnapshotHandle, lppe);
end;
CloseHandle(FSnapshotHandle);
if find=False then
Result:=0; //找不到进程返回0
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc,0);
end;


hostmi 发表于 2012-8-25 10:51:33

学习了,谢谢分享

Cocol 发表于 2013-7-2 12:11:05

看看老帖,支持下

lyw4682 发表于 2013-8-7 10:45:18

看看!!!!!!!!!!

Angel丶L 发表于 2015-2-1 16:44:50

支持楼主发扬Delphi
页: [1]
查看完整版本: delphi 判断当前程序是否是活动窗口