unit Unit30;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TForm30 = class(TForm)
Panel1: TPanel;
Bevel1: TBevel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure Panel1Resize(Sender: TObject);
private
{ Private declarations }
NotepadHandle : THandle;
public
{ Public declarations }
end;
var
Form30: TForm30;
implementation
{$R *.dfm}
uses TLHelp32, ShellAPI;
function GetPIDbyProcessName(processName:String):integer;
var
GotProcess: Boolean;
tempHandle: tHandle;
procE: tProcessEntry32;
begin
tempHandle:=CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
procE.dwSize:=SizeOf(procE);
GotProcess:=Process32First(tempHandle, procE);
{$B-}
if GotProcess and (procE.szExeFile <> processName) then
repeat GotProcess := Process32Next(tempHandle, procE);
until (not GotProcess) or (procE.szExeFile = processName);
{$B+}
if GotProcess then
result := procE.th32ProcessID
else
result := 0; // process not found in running process list
CloseHandle(tempHandle);
end;
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 TForm30.Button1Click(Sender: TObject);
var
appPID : THandle;
begin
appPID := GetPIDbyProcessName('notepad.exe');
NotepadHandle := GetHWndByPID(appPID);
if (NotepadHandle <> 0) then
begin
Winapi.Windows.SetParent(NotepadHandle, Panel1.Handle);
ShowWindow(NotepadHandle, SW_SHOWMAXIMIZED);
end;
end;
procedure TForm30.Panel1Resize(Sender: TObject);
begin
if (NotepadHandle <> 0) then
MoveWindow(NotepadHandle, 0, 0, (Sender as TPanel).ClientWidth, (Sender as TPanel).ClientHeight, True);
end;
end.
Bookmarks