[Delphi] 프로그램 활성화하기
// --------------------------------------------
// 파일이 실행상태가 아니면 실행하기
// 최소화 상태면 활성화시키기
// 다른 창 아래에 가려졌으면 맨 앞으로 가져오기
// --------------------------------------------
procedure test;
begin
h := WindowFromProcessID(getProcessId('실행파일명.exe'));
// h := FindWindow('TFormName', nil);
if h = 0 then
ShellExecute(handle, 'open', PChar(FileName), nil, nil, SW_SHOW)
else if IsIconic(h) then
ShowWindow(h, SW_RESTORE)
else
SetForegroundWindow(h);
end;
// 실행파일명으로 프로세스 ID 찾기
function getProcessId(Exename: string; User: string = ''): DWORD;
var
snap: DWORD;
pe: TprocessEntry32;
sUser, sDomain: string;
begin
Result := 0;
Exename := LowerCase(Exename);
snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if snap <> INVALID_HANDLE_VALUE then
begin
pe.dwSize := SizeOf(TprocessEntry32);
if process32First(snap, pe) then
begin
repeat
if LowerCase(pe.szExeFile) = Exename then
begin
if User <> '' then
begin
if not GetUserAndDomainFromPID(pe.th32ProcessID, sUser,
sDomain) then
Continue;
if LowerCase(sUser) <> LowerCase(User) then
Continue;
end;
Result := pe.th32ProcessID;
CloseHandle(snap);
Exit;
end;
until not process32Next(snap, pe);
CloseHandle(snap);
end;
Result := 0;
end;
end;
// 프로세스 ID로 핸들 구하기
function WindowFromProcessID(pID: DWORD): HWND;
var
Data: TEnumData;
begin
Data.pID := pID;
Data.hW := 0;
EnumWindows(@EnumProc, longint(@Data));
Result := Data.hW;
end;