cuilin
asked on
How to Kill or Minimized Window Explorer
Hi,
How to kill or Minimized the Running Window Explorer ?
I try to use FindWindow('Explorer', nil) but the handle can't be found. and I try to use FindWindow('Progman', nil) but It trigger to ShutDown the Window.
Thanks,
Cuilin
How to kill or Minimized the Running Window Explorer ?
I try to use FindWindow('Explorer', nil) but the handle can't be found. and I try to use FindWindow('Progman', nil) but It trigger to ShutDown the Window.
Thanks,
Cuilin
Hi,
explorer wont usually listen to sendmessage ,better to use postmessage:
first example is to kill the process which is a bit blatent ,something you should really only use if for instance explorer has hung.
procedure CloseIExplorer(h : HWND);
Var pID,hProcess:DWord;
Begin
GetWindowThreadProcessId(h ,@pID);
hProcess:=OpenProcess(PROC ESS_TERMIN ATE,TRUE,p ID);
TerminateProcess(hProcess, 1);
End;
//TO kill process IMEDIATLY
procedure TForm1.Button1Click(Sender : TObject);
Var
IExplorer : Thandle;
begin
IExplorer := FindWindow('IEFrame',nil);
If IExplorer <> 0 Then
CloseIExplorer(IExplorer);
end;
but these would be better to use for close and minimize:
//TO CLOSE nicely
procedure TForm1.Button1Click(Sender : TObject);
Var
IExplorer : Thandle;
begin
IExplorer := FindWindow('IEFrame',nil);
If IExplorer <> 0 Then
PostMessage(IExplorer, WM_CLOSE,0,0);
//or PostMessage(IExplorer, WM_QUIT,0,0);
end;
//TO MINIMIZE
procedure TForm1.Button2Click(Sender : TObject);
Var
IExplorer : Thandle;
begin
IExplorer := FindWindow('IEFrame',nil);
If IExplorer <> 0 Then
PostMessage(IExplorer,WM_S YSCOMMAND, SC_MINIMIZE,0);
end;
note WM_CLOSE is better for like notepad so it can ask you to save any changes etc while WM_QUIT just ignores any changes and quits ,cant think of a situation where that would matter for explorer but still wm_close is probably better.
explorer wont usually listen to sendmessage ,better to use postmessage:
first example is to kill the process which is a bit blatent ,something you should really only use if for instance explorer has hung.
procedure CloseIExplorer(h : HWND);
Var pID,hProcess:DWord;
Begin
GetWindowThreadProcessId(h
hProcess:=OpenProcess(PROC
TerminateProcess(hProcess,
End;
//TO kill process IMEDIATLY
procedure TForm1.Button1Click(Sender
Var
IExplorer : Thandle;
begin
IExplorer := FindWindow('IEFrame',nil);
If IExplorer <> 0 Then
CloseIExplorer(IExplorer);
end;
but these would be better to use for close and minimize:
//TO CLOSE nicely
procedure TForm1.Button1Click(Sender
Var
IExplorer : Thandle;
begin
IExplorer := FindWindow('IEFrame',nil);
If IExplorer <> 0 Then
PostMessage(IExplorer, WM_CLOSE,0,0);
//or PostMessage(IExplorer, WM_QUIT,0,0);
end;
//TO MINIMIZE
procedure TForm1.Button2Click(Sender
Var
IExplorer : Thandle;
begin
IExplorer := FindWindow('IEFrame',nil);
If IExplorer <> 0 Then
PostMessage(IExplorer,WM_S
end;
note WM_CLOSE is better for like notepad so it can ask you to save any changes etc while WM_QUIT just ignores any changes and quits ,cant think of a situation where that would matter for explorer but still wm_close is probably better.
to get the proper classnames to use with findwindow() you should have a program installed with delphi called winsight.if not get a free program from here called sinfo:
http://lcpx07.lc.ehu.es/JMA/win95.html
it will tell you the classnames etc ..
http://lcpx07.lc.ehu.es/JMA/win95.html
it will tell you the classnames etc ..
Or alternativelt, use findwindow with the window title and then use GetClassName with the handle.
(Saves downloading other software, but isn't as easy.)
John.
(Saves downloading other software, but isn't as easy.)
John.
Are you sure you want to close Explorer? 99% of all windows users use it as Shell. If you close the shell it will be very dificult for the user to work. In fact, if Explorer is the shell, after kiling it it starts up again.
If you are trying to write some kind of Shell, then it should be starterd _as_ a shell. In win 9x it is done by putting "SHELL=c:\my_shell\my_shel l.exe" in system.ini. In NT you need to write a string value named "Shell" with value "c:\my_shell\my_shell.exe" in "HKEY_CURRENT_USER\Softwar e\Microsof t\Windows NT\CurrentVersion\Winlogon "
If you are trying to write some kind of Shell, then it should be starterd _as_ a shell. In win 9x it is done by putting "SHELL=c:\my_shell\my_shel
The following code was tested on Windows2000:
VAR
ProcessId, // Used to store Windows Explorer Process ID number
hwnd: longint; // Used to store WIndows Explorer Window handle
ProcessHandle: cardinal; // Used to store WIndows Explorer process handle
begin
//.... your code here....
hwnd := FindWindow('ExploreWClass' ,NIL);
if (hwnd <> 0) then
ShowMessage('Got Window handle of EXPLORER.EXE: '+IntToStr(hwnd));
ProcessHandle:=0;
rCode:= GetWindowThreadProcessId(h wnd,@Proce ssId);
if ProcessId<>0 then
ShowMessage('Got Process ID of EXPLORER.EXE: '+IntToStr(ProcessId)); //ProcessID is the number (PID) that would show up in Task Manager next to Explorer.exe process
ProcessHandle := OpenProcess(PROCESS_ALL_AC CESS, TRUE, ProcessId);
IF ProcessHandle <> 0 THEN
ShowMessage('Got process handle of EXPLORER.EXE: '+IntToStr(ProcessHandle)) ;
TerminateProcess(ProcessHa ndle,0); // Kills Windows Explorer and restart a program specified by "DefaultShell" parameter in System.INI or Registry (see the above comment by FrodoBeggins)
//.... more of your code.....
end;
Please keep in mind, using TerminateProcess is not recommended. There is a better (and harder) way of achieving the same task properly, however I did not do more research becuase this solution suited my needs.
This trick would allow you to almost completely overtake the system while your app runs.
Hope this helps.
Vlad
VAR
ProcessId, // Used to store Windows Explorer Process ID number
hwnd: longint; // Used to store WIndows Explorer Window handle
ProcessHandle: cardinal; // Used to store WIndows Explorer process handle
begin
//.... your code here....
hwnd := FindWindow('ExploreWClass'
if (hwnd <> 0) then
ShowMessage('Got Window handle of EXPLORER.EXE: '+IntToStr(hwnd));
ProcessHandle:=0;
rCode:= GetWindowThreadProcessId(h
if ProcessId<>0 then
ShowMessage('Got Process ID of EXPLORER.EXE: '+IntToStr(ProcessId)); //ProcessID is the number (PID) that would show up in Task Manager next to Explorer.exe process
ProcessHandle := OpenProcess(PROCESS_ALL_AC
IF ProcessHandle <> 0 THEN
ShowMessage('Got process handle of EXPLORER.EXE: '+IntToStr(ProcessHandle))
TerminateProcess(ProcessHa
//.... more of your code.....
end;
Please keep in mind, using TerminateProcess is not recommended. There is a better (and harder) way of achieving the same task properly, however I did not do more research becuase this solution suited my needs.
This trick would allow you to almost completely overtake the system while your app runs.
Hope this helps.
Vlad
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
var
h : HWND;
begin
h := FindWindow('ExploreWClass'
if h <> 0 then
SendMessage(h, WM_CLOSE, 0, 0);
end;