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[0..256] 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;

相关阅读 >>

Delphi程序中操作注册表

Delphi 给 tcombobox 添加图标

webbrower常见问题及解决方法

Delphi中showmodal 和 show 的区别

Delphi xe 泛型数组和splitstring处理数据

Delphi idhttp友好错误信息的捕获

Delphi 下载者源代码

Delphi xml解析、使用treeview呈现效果

Delphi 调用ie隐藏的命令

Delphi字符串中取数字

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...