Program TwoWindowsExplorer;
Uses
Windows, Messages, Classes, SysUtils, ShellApi;
Type
PFindWindowStruct = ^TFindWindowStruct;
TFindWindowStruct =
Record
Caption :
String;
ClassName :
String;
WindowHandle: THandle;
End;
Var
FoundWindows: TStringlist;
WndHandle : THandle;
Procedure ErrorLog(ErrorInfo:
String);
Var
LoadErrorLog : TStringlist;
SaveErrorInfo: TStringlist;
ErrorLog :
String;
Procedure Free_LoadErrorLog;
Begin
Try
FreeAndNil(LoadErrorLog);
Except
End;
End;
Procedure Free_SaveErrorInfo;
Begin
Try
FreeAndNil(SaveErrorInfo);
Except
End;
End;
Function ErrorLogExists : Boolean;
Begin
Try
Result:= False;
If FileExists('
ErrorLog.txt')
Then
Begin
Try
LoadErrorLog:= TStringlist.Create;
LoadErrorLog.LoadFromFile('
ErrorLog.txt');
ErrorLog:= LoadErrorLog.Text;
Result:= True;
Finally
Free_LoadErrorLog;
End;
End;
Except
End;
End;
Begin
Try
Try
SaveErrorInfo:= TStringlist.Create;
SaveErrorInfo.Add(DateTimeToStr(Now));
SaveErrorInfo.Add(ErrorInfo);
If ErrorLogExists
Then
Begin
SaveErrorInfo.Add('
');
SaveErrorInfo.Add(Trim(ErrorLog));
End;
SaveErrorInfo.SaveToFile('
ErrorLog.txt');
Finally
Free_SaveErrorInfo;
End;
Except
End;
End;
Function EnumWindowsProc(hWindow: HWND; lParam: LongInt): Boolean;
StdCall;
Var
lpBuffer : PChar;
WindowCaptionFound: Boolean;
ClassNameFound : Boolean;
Procedure Free_Memory;
Begin
Try
FreeMem(lpBuffer, SizeOf(lpBuffer^));
Except
On E:
Exception
Do ErrorLog('
EnumWindowsProc: Free_Memory Failed'+#13#10+E.ClassName+#13#10+E.
Message);
End;
End;
Begin
Try
GetMem(lpBuffer, 255);
Result := True;
WindowCaptionFound:= False;
ClassNameFound := False;
Try
If GetWindowText(hWindow, lpBuffer, 255) > 0
Then
Begin
If PFindWindowStruct(lParam).Caption = '
'
Then WindowCaptionFound:= True
Else
Begin
If Pos(PFindWindowStruct(lParam).Caption, StrPas(lpBuffer)) > 0
Then WindowCaptionFound:= True;
End;
If PFindWindowStruct(lParam).ClassName = '
'
Then ClassNameFound:= True
Else
Begin
If GetClassName(hWindow, lpBuffer, 255) > 0
Then
Begin
If Pos(PFindWindowStruct(lParam).ClassName, StrPas(lpBuffer)) > 0
Then ClassNameFound:= True;
If (WindowCaptionFound
And ClassNameFound)
Then
Begin
PFindWindowStruct(lParam).WindowHandle:= hWindow;
FoundWindows.Add(IntToStr(hWindow));
End;
End;
End;
End;
Finally
Free_Memory;
End;
Except
On E:
Exception
Do ErrorLog('
EnumWindowsProc Failed'+#13#10+E.ClassName+#13#10+E.
Message);
End;
End;
Function FindAWindow(WinCaption:
String; WinClassName:
String): THandle;
Var
WindowInfo: TFindWindowStruct;
Begin
Try
WindowInfo.Caption := WinCaption;
WindowInfo.ClassName := WinClassName;
WindowInfo.WindowHandle:= 0;
EnumWindows(@EnumWindowsProc, LongInt(@WindowInfo));
Result:= WindowInfo.WindowHandle;
Except
On E:
Exception
Do ErrorLog('
FindAWindow Failed'+#13#10+E.ClassName+#13#10+E.
Message);
End;
End;
Procedure StartTwoExplorer;
Begin
Try
If DirectoryExists('
I:\')
Then
Begin
ShellExecute(0,
Nil, PChar('
I:\MARTIN'),
Nil,
Nil, SW_SHOW);
ShellExecute(0,
Nil, PChar('
I:\MARTIN\(DOWNLOADS)'),
Nil,
Nil, SW_SHOW);
End
Else
Begin
ShellExecute(0,
Nil, PChar('
C:\'),
Nil,
Nil, SW_SHOW);
ShellExecute(0,
Nil, PChar('
D:\'),
Nil,
Nil, SW_SHOW);
End;
Except
On E:
Exception
Do ErrorLog('
StartTwoExplorer Failed'+#13#10+E.ClassName+#13#10+E.
Message);
End;
End;
Procedure CloseOneExplorer;
Begin
Try
WndHandle:= StrToInt(FoundWindows[0]);
PostMessage(WndHandle, WM_CLOSE, 0, 0);
StartTwoExplorer;
Except
On E:
Exception
Do ErrorLog('
CloseOneExplorer Failed'+#13#10+E.ClassName+#13#10+E.
Message);
End;
End;
Procedure TwoExplorerToFront;
Var
I: Integer;
Begin
Try
For I:= 0
To FoundWindows.Count-1
Do
Begin
WndHandle:= StrToInt(FoundWindows[I]);
If IsIconic(WndHandle)
Then ShowWindow(WndHandle, SW_RESTORE)
Else
Begin
ShowWindow(WndHandle, SW_MINIMIZE);
ShowWindow(WndHandle, SW_RESTORE);
End;
End;
Except
On E:
Exception
Do ErrorLog('
TwoExplorerToFront Failed'+#13#10+E.ClassName+#13#10+E.
Message);
End;
End;
Procedure CloseAllExplorer;
Var
I: Integer;
Begin
Try
For I:= 0
To FoundWindows.Count-1
Do
Begin
WndHandle:= StrToInt(FoundWindows[I]);
PostMessage(WndHandle, WM_CLOSE, 0, 0);
End;
StartTwoExplorer;
Except
On E:
Exception
Do ErrorLog('
CloseAllExplorer Failed'+#13#10+E.ClassName+#13#10+E.
Message);
End;
End;
Procedure Free_Stringlist;
Begin
Try
FreeAndNil(FoundWindows);
Except
On E:
Exception
Do ErrorLog('
Free_Stringlist Failed'+#13#10+E.ClassName+#13#10+E.
Message);
End;
End;
Begin
Try
FoundWindows:= TStringlist.Create;
Try
FindAWindow('
','
CabinetWClass');
Case FoundWindows.Count
Of
0 : StartTwoExplorer;
1 : CloseOneExplorer;
2 : TwoExplorerToFront;
Else CloseAllExplorer;
End;
Finally
Free_Stringlist;
End;
Except
On E:
Exception
Do ErrorLog('
MainProgram Failed'+#13#10+E.ClassName+#13#10+E.
Message);
End;
End.