unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, TLHelp32;
type
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
WindowHandle: THandle;
h:HWnd;
implementation
{$R *.DFM}
function GetWindowThreadProcessId(hWnd: HWND;
var dwProcessId: DWORD): DWORD;
stdcall;
external '
user32.dll'
name '
GetWindowThreadProcessId';
function MyEnumWindowProc(AHandle: THandle; LParam: LongWord): boolean;
stdcall;
var
ProcessID: THandle;
begin
ProcessID := 0;
GetWindowThreadProcessID(AHandle, ProcessID);
Result :=
not (ProcessID = LParam);
if not Result
then
WindowHandle := AHandle;
end;
function GetWindowHandleByExeName(
const AExeName:
string): THandle;
var
SnapShot: THandle;
p: TProcessEntry32;
ProcessHandle: THandle;
begin
Result := 0;
WindowHandle := 0;
ProcessHandle := 0;
p.dwSize := SizeOf(p);
SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0);
try
if Process32First(SnapShot, p)
then
repeat
if AnsiLowerCase(AExeName) = AnsiLowerCase(p.szExeFile)
then
ProcessHandle := p.th32ProcessID;
until (ProcessHandle <> 0)
or not Process32Next(SnapShot, p);
EnumWindows(@MyEnumWindowProc, ProcessHandle);
Result := WindowHandle;
finally
CloseHandle(SnapShot);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
m_exe:
string;
begin
m_exe := Edit1.Text;
GetWindowHandleByExeName(m_exe);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
h:=WindowHandle;
postmessage(h,WM_SYSCOMMAND,SC_MINIMIZE,0);
// SC_CLOSE = Fenster schließen
//(beim Hauptfenster wird die Anwendung beendet)
//SC_MINIMIZE = Fenster minimieren
//SC_MAXIMIZE = Fenster maximieren
//SC_RESTORE = Fenster wiederherstellen
end;
end.