var
h: array[0..2] of Char;
hedt: array[0..2] 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[i]:=FindWindowEx(strtoint(memo1.Lines[i]),0,'edit',nil);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
;
SendMessage(hedt[0], WM_SETTEXT, 255, LongInt(Pchar('内容写入记事本一')));
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
SendMessage(hedt[1], WM_SETTEXT, 255, LongInt(Pchar('内容写入记事本二')));
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
SendMessage(hedt[2], WM_SETTEXT, 255, LongInt(Pchar('内容写入记事本三')));
end;
|