zoukankan      html  css  js  c++  java
  • ListBox1相互拖拽数据Demo

     
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ShellApi;
    
    type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        DropFileListBox1: TListBox;
        Label1: TLabel;
        Label2: TLabel;
        procedure FormCreate(Sender: TObject);
        procedure ListBox1Click(Sender: TObject);
        procedure ListBox1DblClick(Sender: TObject);
        procedure DropFileListBox1Click(Sender: TObject);
        procedure DropFileListBox1DblClick(Sender: TObject);
        procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
          State: TDragState; var Accept: Boolean);
        procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
    
    
    
      private
        procedure WMDROPFILES(var Msg: TMessage);
        procedure DropFileListBox1DropFiles(Sender: TObject;
          FileNames: TStringList);
    
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
      S: AnsiString;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.WMDROPFILES(var Msg: TMessage);
    var
      a : Integer;
      Buffer: Array[0..255] of Char;
    begin
      a := DragQueryFile(Msg.wParam, $FFFFFFFF, Buffer, 255);
      for a := 0 to a - 1 do begin    // 处理选择多个文件
         DragQueryFile(Msg.wParam, a, Buffer, 255);  // 文件名就在Buffer里面了
         ListBox1.Items.Append(buffer);   // 将文件名称添加到ListBox1的最后
      end;
      DragFinish(Msg.wParam);
    end;
    
    
    
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      DragAcceptFiles(Handle, True);
      DropFileListBox1.OnDragDrop := listbox1.OnDragDrop;
      DropFileListBox1.OnDragOver := listbox1.OnDragOver;
      DropFileListBox1.MultiSelect := True;
      ListBox1.MultiSelect := True;
      DropFileListBox1.DragMode := dmAutomatic;
      ListBox1.DragMode := dmAutomatic;
    
    end;
    
    procedure TForm1.ListBox1Click(Sender: TObject);
    begin
      S := ListBox1.Items.Strings[ListBox1.ItemIndex];   // 取文件路径及名称
    end;
    
    procedure TForm1.ListBox1DblClick(Sender: TObject);
    begin
      ShellExecute(handle,'Open',PChar(S),nil,nil,SW_SHOWNORMAL);  // 打开文件
    end;
    
    procedure TForm1.DropFileListBox1DropFiles(Sender: TObject;
      FileNames: TStringList);
    begin
     DropFileListBox1.Items.AddStrings(FileNames);  // 处理文件拖拽
    end;
    
    procedure TForm1.DropFileListBox1Click(Sender: TObject);
    begin
      S := DropFileListBox1.Items.Strings[DropFileListBox1.ItemIndex];
    end;
    
    procedure TForm1.DropFileListBox1DblClick(Sender: TObject);
    begin
      ShellExecute(handle,'Open',PChar(S),nil,nil,SW_SHOWNORMAL);  // 打开文件
    end;
    procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    var
      aPoint: TPoint;
    begin
      aPoint.x := x;
      aPoint.y := y;
    
      if (source is Tlistbox) then
        if ((source as Tlistbox).SelCount > 0) and ((sender as Tlistbox).ItemAtPos(apoint, true) <> -1)
          and ((sender as Tlistbox).itematpos(apoint, true) <> (sender as Tlistbox).itemindex) then
       begin
         accept := true;
       end;
    end;
    
    
    
    procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
    var
     aPoint: TPoint;
     st: string;
     p: integer;
     i, num: integer;
     MYlBox: TStrings;
    begin
     aPoint.x := x;
     aPoint.y := y;
    
     p := (sender as Tlistbox).itematpos(aPoint, true);
     MYlBox := Tstringlist.Create;
     try
       Num := (source as Tlistbox).Items.Count;
       for i := num - 1 downto 0 do
       begin
        if (source as Tlistbox).Selected[i] then
        begin
         st := (source as Tlistbox).Items[i];
         MYlBox.Add(st)
       end;
      end;
       if p > -1 then
       begin
        for i := 0 to Mylbox.Count - 1 do
        begin
         (sender as Tlistbox).Items.Insert(p, mylbox[i])
        end;
       end
       else
       begin
        for i := Mylbox.Count - 1 downto 0 do
        begin
         (sender as Tlistbox).Items.Add(mylbox[i])
        end;
       end;
    
       (source as Tlistbox).DeleteSelected;
       if P > -1 then
        (sender as Tlistbox).Selected[p] := true;
       finally
         mylbox.Free;
       end;
    
    end;
    
    end.
    
    

  • 相关阅读:
    max-points-on-a-line
    evaluate-reverse-polish-notation
    minimum-depth-of-binary-tree
    ML&MLDS笔记:偏差 vs 方差
    机器人的运动范围
    矩阵中的路径
    滑动窗口的最大值
    数据流中的中位数
    1.微服务架构设计(英文-起源)
    5.如何复制一个文件(编程)
  • 原文地址:https://www.cnblogs.com/beeone/p/1792466.html
Copyright © 2011-2022 走看看