program Window;

uses Windows, Messages;

const AppName='Створення процесу';

var AMessage  : TMsg;
    hWindow   : HWnd;
    hBtnExit  : HWnd;
    hBtnAgain : HWnd;
    hBtnSetPriority : HWnd;
    hBtnTerm : HWnd;
    SetPriorityFlag : boolean = false;
    PI        : TProcessInformation;

procedure StartNewProc(Wnd:HWnd);
var SI : TStartupInfo;
begin
  FillChar(SI, Sizeof(SI), #0);
  SI.cb := SizeOf(SI);
  SI.dwX := 50;
  SI.dwY := 50;
  SI.dwFlags := STARTF_USEPOSITION;
  if not CreateProcess('APIProcess.exe', nil, nil, nil, false,
    NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
      MessageBox(Wnd, 'Не вийшло створити процес.', '', MB_OK or MB_ICONERROR);
end;

function WindowProc(Window:HWnd; AMessage, WParam, LParam:longint):longint; stdcall; export;
begin
  WindowProc:=0;
  case AMessage of
    WM_DESTROY: begin PostQuitMessage(0); Exit; end;
    WM_COMMAND: if LParam = hBtnExit then begin PostQuitMessage(0); Exit; end
                  else if LParam = hBtnAgain then StartNewProc(Window)
                    else if LParam = hBtnSetPriority then begin
                      if SetPriorityFlag then
                        SetPriorityClass(PI.hProcess, HIGH_PRIORITY_CLASS) else
                          SetPriorityClass(PI.hProcess, IDLE_PRIORITY_CLASS);
                        SetPriorityFlag := not SetPriorityFlag;
                    end
                      else if LParam = hBtnTerm then TerminateProcess(PI.hProcess, 0);
    WM_KEYDOWN: if (wParam = VK_RETURN) or (wParam = VK_ESCAPE) then SendMessage(hBtnExit, BM_CLICK, 0, 0)
                  else if wParam = VK_F1 then SendMessage(hBtnAgain, BM_CLICK, 0, 0);
  end;
  WindowProc := DefWindowProc(Window, AMessage, WParam, LParam);
end;

function WinRegister: boolean;
var WindowClass: TWndClass;
begin
  with WindowClass do begin
    Style := CS_HREDRAW or CS_VREDRAW;
    lpfnWndProc :=@ WindowProc;
    cbClsExtra := 0;
    cbWndExtra := 0;
    hInstance := SysInit.HInstance;
    hIcon := LoadIcon(0, IDI_APPLICATION);
    hCursor := LoadCursor(0, IDC_ARROW);
    hbrBackGround := COLOR_WINDOW;
    lpszMenuName := nil;
    lpszClassName := AppName;
  end;
  Result := RegisterClass(WindowClass) <> 0;
end;

function WinCreate: HWnd;
var hWindow : HWnd;
begin
  hWindow := CreateWindow(AppName, 'Створення нового процесу ', WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 600, 400, 0, 0, HInstance, nil);

  if hWindow<>0 then begin
    ShowWindow(hWindow, SW_SHOWNORMAL);
    UpdateWindow(hWindow);
    hBtnExit := CreateWindow('BUTTON', 'Вихід', WS_CHILD or BS_DEFPUSHBUTTON or WS_TABSTOP,
      500, 10, 90, 30, hWindow, 0, HInstance, nil);
    if hBtnExit <> 0 then ShowWindow(hBtnExit, SW_SHOWNORMAL);
    hBtnAgain := CreateWindow('BUTTON', 'Дублювати', WS_CHILD or WS_TABSTOP,
      500, 70, 90, 30, hWindow, 0, HInstance, nil);
    if hBtnAgain <> 0 then ShowWindow(hBtnAgain, SW_SHOWNORMAL);
    hBtnSetPriority := CreateWindow('BUTTON', 'Пріоритет', WS_CHILD or WS_TABSTOP,
      500, 130, 90, 30, hWindow, 0, HInstance, nil);
    if hBtnSetPriority <> 0 then ShowWindow(hBtnSetPriority, SW_SHOWNORMAL);
    hBtnTerm := CreateWindow('BUTTON', 'Завершити', WS_CHILD or WS_TABSTOP,
      500, 190, 90, 30, hWindow, 0, HInstance, nil);
    if hBtnTerm <> 0 then ShowWindow(hBtnTerm, SW_SHOWNORMAL);
  end;
  Result := hWindow;
end;

begin
  if not WinRegister then begin
    MessageBox(0,'Клас вікна не зареєстровано',AppName,MB_OK);
    Exit;
  end;
  hWindow:=WinCreate;
  if hWindow=0 then begin
    MessageBox(0,'Не вийшло створити вікно.',AppName,MB_OK);
    Exit;
  end;
  while GetMessage(AMessage,0,0,0) do begin
    TranslateMessage(AMessage);
    DispatchMessage(AMessage);
  end;

  CloseHandle(PI.hThread);
  CloseHandle(PI.hProcess);

  Halt(AMessage.wParam);
end.
