PROGRAM TwoWinEx;
 {$APPTYPE GUI}
 USES
  Windows, Messages, Classes, SysUtils, ShellApi;

 VAR
  slFoundWnd: TStringlist;
  WndHandle : THandle;
  Wnd1, Wnd2: THandle;
  WorkArea  : TRect;


Procedure ErrorLog(ErrorInfo: String);
  Var
   slSave: TStringlist;
 Begin
  Try
   slSave:= TStringlist.Create;
    Try
     If FileExists('ErrorLog.txt')
     Then
      Begin
       Try
        slSave.LoadFromFile('ErrorLog.txt');
       Except
       End;
      End;

      slSave.Text:=

       DateTimeToStr(Now) +#13#10+
       ErrorInfo          +#13#10+
       ''                 +#13#10+
       slSave.Text;

      slSave.SaveToFile('ErrorLog.txt');
    Finally
     slSave.Free;
    End;
  Except
  End;
 End;


Function SearchCallback(Wnd: HWND; lParam: PtrInt): BOOL; StdCall;
  Var
   ClassName: Array[0..255] Of Char;
 Begin
  Try
   If GetClassName(Wnd, ClassName, 255) <> 0
   Then
    Begin
     If Pos('CabinetWClass', String(ClassName)) <> 0
     Then slFoundWnd.Add(IntToStr(Wnd));
      Result:= True;
    End;
  Except
   On E: Exception
   Do ErrorLog('SearchCallback'+#13#10+E.ClassName+#13#10+E.Message);
  End;
 End;


Procedure PositionWndLeft;
 Begin
  MoveWindow(Wnd1,
   WorkArea.Left,
   WorkArea.Top,
   ((WorkArea.Right-WorkArea.Left) Div 2),
   (WorkArea.Bottom-WorkArea.Top),
   True);
 End;


Procedure PositionWndRight;
 Begin
  MoveWindow(Wnd2,
   ((WorkArea.Right-WorkArea.Left) Div 2),
   WorkArea.Top,
   ((WorkArea.Right-WorkArea.Left) Div 2),
   (WorkArea.Bottom-WorkArea.Top),
   True);
 End;


Procedure StartTwoWinEx;
  Var
   Path1, Path2: String;
 Begin
  Try
   If DirectoryExists('I:\') And DirectoryExists('I:\(DOWNLOADS)')
   Then
    Begin
     Path1:= 'I:\';
     Path2:= 'I:\(DOWNLOADS)';
    End
   Else
    Begin
     Path1:= 'C:\';
     Path2:= 'D:\';
    End;

   ShellExecute(0, Nil, PChar(Path1), Nil, Nil, SW_SHOW);
   ShellExecute(0, Nil, PChar(Path2), Nil, Nil, SW_SHOW);

    Repeat
     slFoundWnd.Clear;
     EnumWindows(@SearchCallback, 0);
    Until slFoundWnd.Count >= 2;

   SystemParametersInfo(SPI_GETWORKAREA, 0, @WorkArea,0) ;

   Wnd1:= StrToInt(slFoundWnd[0]);
   Wnd2:= StrToInt(slFoundWnd[1]);

   PositionWndLeft;
   PositionWndRight;
  Except
   On E: Exception
   Do ErrorLog('StartTwoWinEx'+#13#10+E.ClassName+#13#10+E.Message);
  End;
 End;


Procedure DelExcept2;
  Var
   I: Integer;
 Begin
  Try
   For I:= 2 To slFoundWnd.Count-1
   Do
    Begin
     WndHandle:= StrToInt(slFoundWnd[I]);
     SendMessage(WndHandle, WM_CLOSE, 0, 0);
    End;
  Except
   On E: Exception
   Do ErrorLog('DelExcept2'+#13#10+E.ClassName+#13#10+E.Message);
  End;
 End;


Procedure Bring2ToFront;
 Begin
  Try
   SystemParametersInfo(SPI_GETWORKAREA, 0, @WorkArea,0) ;
    Wnd1:= StrToInt(slFoundWnd[0]);
    Wnd2:= StrToInt(slFoundWnd[1]);

    If IsIconic(Wnd1)
    Then
     Begin
      ShowWindow(Wnd1, SW_RESTORE);
      PositionWndLeft;
     End
    Else
     Begin
      ShowWindow(Wnd1, SW_MINIMIZE);
      ShowWindow(Wnd1, SW_RESTORE);
      PositionWndLeft;
     End;

    If IsIconic(Wnd2)
    Then
     Begin
      ShowWindow(Wnd2, SW_RESTORE);
      PositionWndRight;
     End
    Else
     Begin
      ShowWindow(Wnd2, SW_MINIMIZE);
      ShowWindow(Wnd2, SW_RESTORE);
      PositionWndRight;
     End;
  Except
   On E: Exception
   Do ErrorLog('Bring2ToFront'+#13#10+E.ClassName+#13#10+E.Message);
  End;
 End;


Begin
 Try
  slFoundWnd:= TStringlist.Create;
   Try
    EnumWindows(@SearchCallback, 0);

     If slFoundWnd.Count > 2
     Then
      Begin
       DelExcept2;
       Bring2ToFront;
      End;

     If slFoundWnd.Count = 2
     Then Bring2ToFront;

     If slFoundWnd.Count = 1
     Then
      Begin
       WndHandle:= StrToInt(slFoundWnd[0]);
       SendMessage(WndHandle, WM_CLOSE, 0, 0);
       StartTwoWinEx;
      End;

     If slFoundWnd.Count = 0
     Then StartTwoWinEx;

   Finally
    slFoundWnd.Free;
   End;
 Except
  On E: Exception
  Do ErrorLog('MAIN PRG'+#13#10+E.ClassName+#13#10+E.Message);
 End;
End.