Einzelnen Beitrag anzeigen

Benutzerbild von mika
mika

Registriert seit: 25. Okt 2002
176 Beiträge
 
Delphi 6 Professional
 
#9

Re: Override bei Funktion, beide Parameter sind Ganzzahl

  Alt 2. Sep 2005, 15:11
Ich poste mal kurz n bisschen mehr Code:

Delphi-Quellcode:
unit RunningPrograms;

interface

uses Contnrs, Windows, Messages;

Type
  TProgramInformations = class
    Handle: THandle;
    Caption: String;
  End;

  TRunningPrograms = class
    Constructor Create;
    Constructor Destroy;
    private
      oPrograms: TObjectlist;
      Function getIndexbyHandle(Handle: THandle): Integer;
    Public
      Function ReadToplevelPrograms: Boolean;
      Procedure AddProgram(Handle: THandle; Caption: String);
      Function DeleteProgram(Handle: THandle): Boolean; Overload;
      Function DeleteProgram(Index: Integer): Boolean; Overload;
      // Todo: per Override dem Entwickler nur eine Prozedur mit
      // beiden Parametern abieten
      Function getCaption(Handle: THandle): String; Overload;
      Function getCaption(Index: Integer): String; Overload;

      Function Count: Integer;
  end;

implementation

Constructor TRunningPrograms.Create;
begin
  If Not(Assigned(oPrograms)) then
    oPrograms := TObjectList.Create(True);
  oPrograms.Clear;
end;

Constructor TRunningPrograms.Destroy;
begin
  If Assigned(oPrograms) then
    oPrograms.Free;
  oPrograms := NIL;
end;

Function TRunningPrograms.getIndexbyHandle(Handle: THandle): Integer;
Var
  ndx: Integer;

Begin
  Result := -1;
  If Assigned(oPrograms) then
    for ndx := 0 to oPrograms.Count - 1 do
    begin
      If (oPrograms.Items[ndx] as TProgramInformations).Handle = Handle then
      Begin
        Result := ndx;
        Break;
      End;
    end;
End;

Function TRunningPrograms.ReadToplevelPrograms: Boolean;

  function GetWindows(const hWnd : THandle; LParam: LPARAM): LongBool; stdcall;
  var
    cLen: Longint;
    strCaption: String;

  begin
    Result := True;
    if (IsWindowVisible(hWnd)) and
       ((GetWindowLong(hWnd, GWL_HWndPARENT) = 0) or
       (GetWindowLong(hWnd, GWL_HWndPARENT) = GetDesktopWindow)) and
       (GetWindowLong(hWnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) then
    begin
      cLen := SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
      if cLen > 0 then
      begin
        SetLength(strCaption, cLen);
        SendMessage(hWnd, WM_GETTEXT, cLen + 1, Longint(Pchar(strCaption)));
        AddProgram(hWnd, strCaption);
      end;
    end;
    // mit Result = False kann die Callbackfunktion vorzeitig verlassen werden
  end;

Begin
  If Assigned(oPrograms) then
  begin
    EnumWindows(@GetWindows, 1);
    Result := True;
  end else
    Result := False;
End;

Procedure TRunningPrograms.AddProgram(Handle: THandle; Caption: String);
Begin
  If Assigned(oPrograms) then
  begin
    oPrograms.Add(TProgramInformations.Create);
    //(oPrograms.Last AS TProgramInformations).Handle := Handle;
    //(oPrograms.Last AS TProgramInformations).Caption := Caption;
  end;
End;

Function TRunningPrograms.DeleteProgram(Handle: THandle): Boolean;
Var
  ndx: Integer;

Begin
  ndx := getIndexbyHandle(Handle);
  If ndx >= 0 then
  begin
    Result := DeleteProgram(ndx);
  end else
    Result := False;
End;

Function TRunningPrograms.DeleteProgram(Index: Integer): Boolean;
Begin
  If oPrograms.Count - 1 >= Index then
  begin
    oPrograms.Delete(Index);
    Result := True;
  end else
    Result := False;
End;

Function TRunningPrograms.getCaption(Handle: THandle): String;
Begin
  Result := '';
  If Assigned(oPrograms) then
    If getIndexbyHandle(Handle) >= 0 then
      Result := getCaption(getIndexbyHandle(Handle));
End;

Function TRunningPrograms.getCaption(Index: Integer): String;
Begin
  Result := '';
  If Assigned(oPrograms) then
    IF oPrograms.Count - 1 >= Index then
      Result := (oPrograms.Items[Index] AS TProgramInformations).Caption;
End;

function TRunningPrograms.Count: Integer;
begin
  If Assigned(oPrograms) then
    Result := oPrograms.Count else
    Result := 0;
end;

end.

In der Funktion AddProgram bekomme ich eine Exception, wisst Ihr warum? Normalerweise müsste
das doch klappen wenn die Objektliste erzeugt ist oder? Hab schon öfter Objektlisten benutzt
und das hat eigentlich immer geklappt!

Hier mal mein Aufruf:

Delphi-Quellcode:
procedure TfrmMain.Button1Click(Sender: TObject);
Var
  myPrograms: TRunningPrograms;
  ndx: Integer;
  str: String;

begin
  myPrograms := TRunningPrograms.Create;
  If myPrograms.ReadToplevelPrograms then
  begin
    for ndx := 0 to myPrograms.Count - 1 do
    begin
      ListBox1.Items.Add(myPrograms.getCaption(ndx));
    end;
  end;
  myPrograms.Free;
end;
:: don't Panic ::
  Mit Zitat antworten Zitat