Board index » delphi » How can I close programs and make them active (not as simple as it sounds)

How can I close programs and make them active (not as simple as it sounds)

Hi, I am making a new version of my OS called Doorz (doorz3). it should be
out for the new year. I can use the below code to get a list of all running
programs and put them in a list-box. I need to be albe to close these
programs, and be albe to make them active. I do not know the handle, and I
don't know how to find it out.. Help!

here's the code I use to put the list of programs in listbox1...

function EnumWindowsCallBack(Window: HWnd; LPARAM:LParam):bool stdcall;
const
  MaxWTextLength = 1024;
var
  WindowText: String;

begin
  // Find the Name of the Window
  SetLength(WindowText, MaxWTextLength);
  SetLength(WindowText, GetWindowText(Window, PChar(WindowText),
MaxWTextLength));

  // Add the Window Name
  if (Length(Trim(WindowText)) > 0) AND (IsWindowVisible(Window)) then
    begin
     form1.ListBox1.Items.Add(Trim(WindowText)); // Add String Value
    end;

  Result := True; // Continue Enumeration
end;

to active the function I use this....
 EnumWindows(@EnumWindowsCallBack, 0); // Enumerate Windows (and Log them)

PLEASE HELP

From Steve
<><><><><>
visit www.freespeech.org/gunsoft
icq #48322092

 

Re:How can I close programs and make them active (not as simple as it sounds)


Hi !

You need to store the window handle, this you may do in the
ListBox.Items.Objects[] property:

 if (Length(Trim(WindowText)) > 0) AND (IsWindowVisible(Window)) then
 begin
    form1.ListBox1.Items.AddObject(Trim(WindowText), TObject(Window));
// Add String Value
  end;

Now the window handle is stored typecasted as a TObject.
When you want to close it, retrieve the handle by typecasting back to
HWND:

procedure TForm1.CloseSelectedWindow;
var
  Window: HWND;
begin
  Window:=HWND(form1.ListBox1.Items.Objects[form1.ListBox1.ItemIndex]);
  PostQuitMessage(Window);
end;

..and to activate:

procedure TForm1.ActivateSelectedWindow;
var
  Window: HWND;
begin
  Window:=HWND(form1.ListBox1.Items.Objects[form1.ListBox1.ItemIndex]);
  ShowWindow(Window, SW_SHOWNORMAL);
end;

...well, I didn't try this, so I'm not 100% about the postQuitMessage()
and ShowWindow(), but it should show the technique, at least...

Good luck !

Bjoerge Saether
Consultant / Developer
Asker, Norway
bsaether.removet...@online.no (remove the obvious)

Steve skrev i meldingen
<82tf3i$49...@nclient11-gui.server.{*word*269}.net>...

Quote
>Hi, I am making a new version of my OS called Doorz (doorz3). it should
be
>out for the new year. I can use the below code to get a list of all
running
>programs and put them in a list-box. I need to be albe to close these
>programs, and be albe to make them active. I do not know the handle,
and I
>don't know how to find it out.. Help!

>here's the code I use to put the list of programs in listbox1...

>function EnumWindowsCallBack(Window: HWnd; LPARAM:LParam):bool stdcall;
>const
>  MaxWTextLength = 1024;
>var
>  WindowText: String;

>begin
>  // Find the Name of the Window
>  SetLength(WindowText, MaxWTextLength);
>  SetLength(WindowText, GetWindowText(Window, PChar(WindowText),
>MaxWTextLength));

>  // Add the Window Name
>  if (Length(Trim(WindowText)) > 0) AND (IsWindowVisible(Window)) then
>    begin
>     form1.ListBox1.Items.Add(Trim(WindowText)); // Add String Value
>    end;

>  Result := True; // Continue Enumeration
>end;

>to active the function I use this....
> EnumWindows(@EnumWindowsCallBack, 0); // Enumerate Windows (and Log
them)

>PLEASE HELP

>From Steve
><><><><><>
>visit www.freespeech.org/gunsoft
>icq #48322092

Other Threads