自定义的listbox支持拖放

unit unit2;

interface

uses
  Classes, Controls, StdCtrls;

type
  TListBox2 = class(TCustomListBox)
  protected
    procedure DragOver(Source: TObject; X: Integer; Y: Integer; State: TDragState; var Accept: Boolean); override;
    procedure DblClick; override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure DragDrop(Source: TObject; X: Integer; Y: Integer); override;
  end;

implementation

{ TListBox2 }

constructor TListBox2.Create(AOwner: TComponent);
begin
  inherited;
  DragMode := dmAutomatic;
end;

procedure TListBox2.DblClick;
begin
  inherited;
  Items.Delete(ItemIndex);
end;

procedure TListBox2.DragDrop(Source: TObject; X, Y: Integer);
begin
  inherited;
  Items.Exchange(ItemIndex, ItemAtPos(Point(X,Y), True));
end;

procedure TListBox2.DragOver(Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
  inherited;
  Accept := True;
end;

end.
 



procedure TForm1.btn1Click(Sender: TObject);
begin
  with TListBox2.Create(Self)
  do begin
    Parent := Self;
    Align := alLeft;
    Items.CommaText := 'A,B,C,D,E,F,G';
  end;
end;

猜你喜欢

转载自www.cnblogs.com/tobetterlife/p/12169460.html