TListBox如何实现鼠标拖动列表项

字体大小: 中小 标准 ->行高大小: 标准
TListBox如何实现鼠标拖动,使其Items[]的内容变换位置?

请看下面的例子:
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  if Y <= TListBox(Sender).ItemHeight div 4 then
    TListBox(Sender).Perform(WM_VSCROLL, SB_LINEUP, 0);
  if Y >= TListBox(Sender).Height - TListBox(Sender).ItemHeight then
    TListBox(Sender).Perform(WM_VSCROLL, SB_LINEDOWN, 0);
  Accept := Source = Sender;
end;

procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  I, J: Integer;
begin
  J := TListBox(Source).ItemIndex;
  if J < 0 then Exit;
  I := TListbox(Sender).ItemAtPos(Point(X, Y), True);
  if I < 0 then Exit;
  TListbox(Sender).Items.Move(J, I);
  TListBox(Source).ItemIndex := I;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListBox1.DragMode := dmAutomatic;
end;

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/68036.html