Einzelnen Beitrag anzeigen

brechi

Registriert seit: 30. Jan 2004
823 Beiträge
 
#12

AW: Data-Pointer in Interfacevariable casten

  Alt 15. Jul 2010, 20:08
Oder du schreibst dir deine eigene TListView die alles für dich übernimmt:

Delphi-Quellcode:
unit Unit9;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  ComCtrls,
  StdCtrls;

type
  TListView = class(comCtrls.TListView)
  private
    FOnDeletion: TLVDeletedEvent;
    procedure SetOnDeletion(const Value: TLVDeletedEvent);
    function GetOnDeletion: TLVDeletedEvent;
  protected
    procedure InternOnDeletion(Sender: TObject; Item: TListItem);
  public
    constructor Create(AOwner: TComponent); override;
  published

    procedure AddItem(Item: String; AObject: IInterface); reintroduce;
    property OnDeletion: TLVDeletedEvent read GetOnDeletion write SetOnDeletion;
  end;

  TForm9 = class(TForm)
    ListViewMain: TListView;
    Button1: TButton;
    procedure ListViewMainClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure Refresh;
    procedure GetAllAccounts(_o: TInterfaceList);
  public
    { Public declarations }
  end;

var
  Form9: TForm9;

implementation

{$R *.dfm}

type
  IDataObject = interface(IInterface)
    ['{C94DCAC7-CA42-45D3-95D7-9BC321BA2E2A}']
    procedure Delete;
  end;

  IAccountData = interface(IInterface)['{DA626905-CB35-4474-B353-9F8E973709AA}']
    function GetId: Integer;
    procedure SetId(_Id: Integer);
    property ID: Integer read Getid write SetId;
  end;

  TAccountData = class(TInterfacedObject, IAccountData)
  protected
    FID: Integer;
    function Getid: Integer;
    procedure SetId(_Id: Integer);
  public
    destructor Destroy; override;
  end;

  TAccountDataExtend = class(TAccountData, IDataObject)
  public
    procedure Delete;
  end;

procedure TForm9.ListViewMainClick(Sender: TObject);
var
  obj: IInterface;
  obj3: IDataObject;
begin
  inherited;
  if ListViewMain.ItemIndex >= 0 then begin
    obj := IInterface(ListViewMain.Selected.Data);
    if Succeeded(obj.QueryInterface(IDataObject, obj3)) then begin
      obj3.Delete; //Funktion siehe unten
    end;
  end;
end;

procedure TForm9.Refresh;
var
  tmp: IAccountData;
  i: Integer;
  x: TInterfaceList;
begin
  //Vorgängig Listview aufräumen, Objekte zerstören, Interface Releasen

  x := TInterfaceList.Create;
  try
    GetAllAccounts(x); //Siehe Funktion unten
    for i := 0 to x.Count - 1 do begin
      if Succeeded(x[i].QueryInterface(IAccountData, tmp)) then begin
        ListViewMain.AddItem(IntToStr(tmp.ID), tmp);
      end;
    end;
  finally
    FreeAndNil(x); //Ich nehme an die InterfaceList zerstört die Objekte nicht
  end;
end {LoadListView};

procedure TForm9.Button1Click(Sender: TObject);
begin
  Refresh;
end;

procedure TForm9.GetAllAccounts(_o: TInterfaceList);
var
  tmp: IAccountData; //Oder IInterface?
  i: integer;
begin
  for i := 0 to 4 do begin
    tmp := TAccountDataExtend.Create;
    tmp.ID := i;
    _o.Add(tmp);
  end;
end {GetAllAccounts};

//----------------- TAccountDataExtend

procedure TAccountDataExtend.Delete;
begin
  ShowMessage(IntToStr(FID));
end {Delete};

{ TAccountData }

destructor TAccountData.Destroy;
begin
  MessageBox(0,'del',nil,0);
  inherited;
end;

function TAccountData.Getid: Integer;
begin
  Result := Fid;
end;

procedure TAccountData.SetId(_Id: Integer);
begin
  Fid := _id;
end;


{ TListView }

procedure TListView.AddItem(Item: String; AObject: IInterface);
begin
  AObject._AddRef;
  inherited AddItem(Item, Pointer(AObject))
end;

procedure TListView.SetOnDeletion(const Value: TLVDeletedEvent);
begin
  FOnDeletion := Value;
end;

constructor TListView.Create(AOwner: TComponent);
begin
  inherited;
  inherited OnDeletion := InternOnDeletion;
end;

function TListView.GetOnDeletion: TLVDeletedEvent;
begin
  Result := FOnDeletion;
end;

procedure TListView.InternOnDeletion(Sender: TObject; Item: TListItem);
begin
  if Assigned(item.Data) then
    IInterface(item.Data)._Release;
end;

end.
  Mit Zitat antworten Zitat