5,392
社区成员
发帖
与我相关
我的任务
分享
handle := FindWindow(nil,PChar('窗口的标题'));
或者:procedure TForm1.Button1Click(Sender: TObject);
var
hCurrentWindow: HWnd;
WndText:String;
begin
hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);
while hCurrentWindow <> 0 do
begin
WndText:=GetWndText(hCurrentWindow);
if UpperCase(WndText)='窗口的标题' then begin
...
...
end;
hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT);
end;
end;
uses TLHelp32;
procedure TForm1.Button1Click(Sender: TObject);
var
ProcessName : string; //进程名
FSnapshotHandle:THandle; //进程快照句柄
FProcessEntry32:TProcessEntry32; //进程入口的结构体信息
ContinueLoop:BOOL;
MyHwnd:THandle;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //创建一个进程快照
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); //得到系统中第一个进程
//循环例举
while ContinueLoop do
begin
ProcessName := FProcessEntry32.szExeFile;
if(ProcessName = '要找的应用程序名.exe') then begin
MyHwnd := GetHWndByPID(FProcessEntry32.th32ProcessID);
...
...
end;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle); // 释放快照句柄
end;
//跟据ProcessId获取进程的窗口句柄
function TForm1.GetHWndByPID(const hPID: THandle): THandle;
type
PEnumInfo = ^TEnumInfo;
TEnumInfo = record
ProcessID: DWORD;
HWND: THandle;
end;
function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
var
PID: DWORD;
begin
GetWindowThreadProcessID(Wnd, @PID);
Result := (PID <> EI.ProcessID) or
(not IsWindowVisible(WND)) or
(not IsWindowEnabled(WND));
if not Result then EI.HWND := WND;
end;
function FindMainWindow(PID: DWORD): DWORD;
var
EI: TEnumInfo;
begin
EI.ProcessID := PID;
EI.HWND := 0;
EnumWindows(@EnumWindowsProc, Integer(@EI));
Result := EI.HWND;
end;
begin
if hPID<>0 then
Result:=FindMainWindow(hPID)
else
Result:=0;
end;
1:function EnumChildWindowsProc(H: HWnd; lparam: longint): Boolean; stdcall;
var
Buffer: array[0..10] of Char;
begin
Result := True; //設定為True才會再找下一個
getclassname(h,buffer,10);
if trim(StrPas(Buffer))='Edit' then
begin
ThisUrl:= StrAlloc(250);
SendMessage(h,WM_GETTEXT,249,Integer(ThisUrl));
Result:=False;//設定為false不找下一個
end;
end;
procedure GetEdit;
begin
EnumChildWindows(getforegroundwindow, @EnumChildWindowsProc, 0);
end;
2:关注
3:有三种Edit 类型
1)对话框,单文档,如:记事本,写字板类型
先要Find主窗口(主应用句柄)
找到主应用最后一个子窗口
找到主应用最后一个子窗口的最顶层窗口的句柄。
2)MDI多文档,如VC 的编辑框,UltraEdit32等
先要Find主窗口(主应用句柄)
连接MDIClient, 即用FindWindowEx找到主应用的子窗口名为"MDIClient"的窗口句柄。
找到MDICLient窗口句柄的,最后一个顶层窗口(the last TopWindow),
3)Edit框不是最后一个TopWindow,如VC 的编译结果显示框,和Delphi的Unit.pas编辑框。
用遍历法找
我写了一个VC 例子,基本全是API调用,C BUilder直接用,稍做改动就可用于delphi。
// 记录输入参数的指针
static char *wClassName;
static char *wWindowName;
////////////////////////////////////////////////////
// 回调函数,用于找主应用句柄,类名,窗口名
// 如果不是为了用匹配法寻找窗口句柄
// 用FindWindow(wClassName, wWindowName)就够了
////////////////////////////////////////////////////
bool __stdcall EnumProc(HWND hWnd,LPARAM lParam)
{
char title[110];
char className[95];
if(hWnd == NULL)
return FALSE;
::GetClassName(hWnd,className,95);
::GetWindowText(hWnd,title,110);
if (wClassName != NULL)
if ( strstr(className, wClassName) == NULL )
return TRUE;
if (wWindowName != NULL)
if ( strstr(title, wWindowName) == NULL )
return TRUE;
// 找到了,把句柄传回去。
*((HWND*)lParam) = hWnd;
return FALSE;
}
////////////////////////////////////////////////////
// 找到一个窗口类名象..., 窗口标题象...的主应用句柄
// 可以类名或窗口名可以为NULL。
////////////////////////////////////////////////////
HWND GetWindowLike(const char *lpLikeClassName,const char *lpLikeWindowName)
{
HWND ControledhWnd = 0;
if ( (lpLikeClassName != NULL)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
h: HWND;
procedure TForm1.Timer1Timer(Sender: TObject);
var
pt: TPoint;
arr: array[0..254] of Char;
begin
if GetCursorPos(pt) then {如果能获取点}
begin
h := WindowFromPoint(pt); {返回句柄}
GetClassName(h, arr, Length(arr)); {获取该句柄窗口的类名}
Text := arr; {显示在标题}
end;
end;
end.