Unit u2WindowsExplorer;
Interface
Uses
Windows, Messages, SysUtils, Classes, Controls, Forms, ShellApi;
Type
TForm1 =
Class(TForm)
Procedure FormCreate(Sender: TObject);
Procedure FormActivate(Sender: TObject);
Procedure FormShow(Sender: TObject);
End;
Type
PFindWindowStruct = ^TFindWindowStruct;
TFindWindowStruct =
Record
Caption :
String;
ClassName:
String;
WindowHandle: THandle;
End;
Var
Form1 : TForm1;
stringlist_FindWindow : TStringlist;
WindowHandle : THandle;
Implementation
{$R *.dfm}
Procedure ErrorLog(ErrorInfo:
String);
Var
LogFile : TStringlist;
LoadedFile : TStringlist;
boolean_Loaded : Boolean;
Procedure LoadErrorLog;
Begin
Try
boolean_Loaded := False;
If FileExists('
ErrorLog.txt')
Then
Begin
LoadedFile := TStringlist.Create;
LoadedFile.LoadFromFile('
ErrorLog.txt');
boolean_Loaded := True;
End;
Except
Exit;
End;
End;
Procedure Free_LogFile;
Begin
Try
FreeAndNil(LogFile);
Except
Exit;
End;
End;
Procedure Free_LoadedFile;
Begin
Try
FreeAndNil(LoadedFile);
Except
Exit;
End;
End;
Begin
LoadErrorLog;
Try
LogFile:= TStringlist.Create;
Try
If boolean_Loaded = True
Then
Begin
LogFile.Add(ErrorInfo);
LogFile.Text := LogFile.Text + LoadedFile.Text;
Free_LoadedFile;
End
Else
Begin
LogFile.Add(ErrorInfo);
End;
LogFile.SaveToFile('
ErrorLog.txt');
Finally
Free_LogFile;
End;
Except
Free_LoadedFile;
Exit;
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
ErrorLog('
EnumWindowsProc (Free_Memory) Failed');
Exit;
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;
stringlist_FindWindow.Add(IntToStr(hWindow));
End;
End;
End;
End;
Finally
Free_Memory;
End;
Except
ErrorLog('
EnumWindowsProc Failed');
Exit;
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
ErrorLog('
FindAWindow Failed');
Exit;
End;
End;
Procedure Start2WindowsExplorer;
Begin
Try
If DirectoryExists('
I:\')
Then
Begin
ShellExecute(Form1.Handle,
Nil, PChar('
I:\MARTIN'),
Nil,
Nil, SW_SHOW);
ShellExecute(Form1.Handle,
Nil, PChar('
I:\MARTIN\(DOWNLOADS)'),
Nil,
Nil, SW_SHOW);
End
Else
Begin
ShellExecute(Form1.Handle,
Nil, PChar('
C:\'),
Nil,
Nil, SW_SHOW);
ShellExecute(Form1.Handle,
Nil, PChar('
D:\'),
Nil,
Nil, SW_SHOW);
End;
Except
ErrorLog('
Start2WindowsExplorer Failed');
Exit;
End;
End;
Procedure Close1WindowsExplorer;
Procedure Terminate;
Begin
Try
WindowHandle := StrToInt(stringlist_FindWindow[0]);
PostMessage(WindowHandle, WM_QUIT, 0, 0);
Except
ErrorLog('
Close1WindowsExplorer (Terminate) Failed');
Exit;
End;
End;
Begin
Try
PostMessage(WindowHandle, WM_CLOSE, 0, 0);
Start2WindowsExplorer;
Except
ErrorLog('
Close1WindowsExplorer Failed');
Terminate;
Exit;
End;
End;
Procedure Bring2WindowsExplorerToFront;
Var
i : Integer;
Begin
Try
For i:= 0
To stringlist_FindWindow.Count-1
Do
Begin
WindowHandle := StrToInt(stringlist_FindWindow[i]);
If IsIconic(WindowHandle)
Then ShowWindow(WindowHandle, SW_RESTORE)
Else
Begin
ShowWindow(WindowHandle, SW_MINIMIZE);
ShowWindow(WindowHandle, SW_RESTORE);
End;
End;
Except
ErrorLog('
Bring2WindowsExplorerToFront Failed');
Exit;
End;
End;
Procedure DeleteAllAndStart2WindowsExplorer;
Var
i: Integer;
Begin
Try
For i:= 0
To stringlist_FindWindow.Count-1
Do
Begin
WindowHandle := StrToInt(stringlist_FindWindow[i]);
PostMessage(WindowHandle, WM_CLOSE, 0, 0);
End;
Start2WindowsExplorer;
Except
ErrorLog('
DeleteAllAndStart2WindowsExplorer Failed');
Exit;
End;
End;
Procedure TForm1.FormCreate(Sender: TObject);
Procedure Free_Stringlist;
Begin
Try
FreeAndNil(stringlist_FindWindow);
Except
ErrorLog('
FormCreate (Free_Stringlist) Failed');
Exit;
End;
End;
Begin
Try
Form1.Height := 1;
Form1.Width := 1;
Form1.Top := 0;
Form1.Left := 0;
Form1.AlphaBlend := True;
Form1.AlphaBlendValue := 0;
stringlist_FindWindow := TStringlist.Create;
Try
FindAWindow('
:\', '
CabinetWClass');
Case stringlist_FindWindow.Count
Of
0 : Start2WindowsExplorer;
1 : Close1WindowsExplorer;
2 : Bring2WindowsExplorerToFront;
Else
Begin
DeleteAllAndStart2WindowsExplorer;
End;
End;
Finally
Free_Stringlist;
End;
Except
ErrorLog('
FormCreate Failed');
Exit;
End;
End;
Procedure TForm1.FormShow(Sender: TObject);
Var Owner: HWND;
Begin
Try
Owner := GetWindow(Form1.Handle, GW_OWNER);
ShowWindow(Owner, SW_HIDE);
Except
Exit;
End;
End;
Procedure TForm1.FormActivate(Sender: TObject);
Procedure Terminate;
Begin
Try
Application.Terminate;
Except
Exit;
End;
End;
Begin
Try
Close;
Except
Terminate;
End;
End;
End.