dlephi7里如何获得多个同标题窗口的句柄!
在dlephi7里试了很久,和度娘闹了好几天。还是木有搞定,泪崩啊! @@在打开多个相同程序时,就有了多个相同类名和标题的窗口,但是他们的句柄不同,如何才能获得;比如有3个记事本都开着,窗口类名都是:’Notepad‘;窗口标题都是:‘无标题 - 记事本’;但是有3个句柄,如何吧3个不同的句柄都找到
var
h: array of Char;
hedt: array of HWND;
str:string;
i:integer;
function 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;
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 = 'notepad.exe') then
begin
MyHwnd := GetHWndByPID(FProcessEntry32.th32ProcessID);
Memo1.Lines.Add(inttostr(MyHwnd));
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
for i := 0 to memo1.Lines.Count - 1 do
begin
hedt:=FindWindowEx(strtoint(memo1.Lines),0,'edit',nil);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
;
SendMessage(hedt, WM_SETTEXT, 255, LongInt(Pchar('内容写入记事本一')));
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
SendMessage(hedt, WM_SETTEXT, 255, LongInt(Pchar('内容写入记事本二')));
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
SendMessage(hedt, WM_SETTEXT, 255, LongInt(Pchar('内容写入记事本三')));
end;
页:
[1]