我用ListView控件,已经可以从其他窗口把文件直接拖进来了,ListView显示文件的一些基本信息如路径,大小等,设置RowSelect := TRUE,如何实现行的拖拽呢?比如用鼠标直接把第一行的拖到第三行,不知道该怎么写,我看别人的程序里有用DragOver和DragDrop的,猜想和这个有关系,抄过来,但根本就不执行这两个过程:(请大虾多多指教,我是在没招了:(谢谢!

解决方案 »

  1.   

    “delphi精要“有这方面的知识,好像你先得给listview添加DragOver和DragDrop消息才行
      

  2.   

    在dragover事件中,添加一行代码:
    Accept := Source = ListView1;
    然后在DragDrop事件中编写处理代码。
    如果还不行,发邮件给我[email protected]
      

  3.   

    我抄来的代码是这样的:
    procedure TfrmMain.ListView1DragOver(Sender, Source: TObject; X,
     Y: Integer; State: TDragState; var Accept: Boolean);
    begin
     //-- only allow dragging withing listview
     if (Source = ListView1) then Accept := true;
    end;procedure TfrmMain.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
    var
     liOffset: Integer;
    begin
     with ListView1 do
     begin
       if Assigned(DropTarget) then
       begin
         Items.BeginUpdate;
         try
           //-- insert Item before DropTarget, only
           //-- if the item is dragged after the last item
           //-- this item should be put at the end of the list
           liOffset := 0;
           if GetItemAt(X, Y) = nil then liOffset := 1;
           with Items.Insert(Items.IndexOf(DropTarget) + liOffset) do
             Assign(ItemFocused);
           //-- delete FocusedItem
           Items.Delete(Items.IndexOf(ItemFocused));
           //-- Workaround for whatever; else the listview will be drawn
    with checkboxes...
           Checkboxes := false;
           //-- Show No Sort Arrow, List is unsorted now
           //ListView1.ShowSortArrows := false;
         finally
           Items.EndUpdate;
         end;
       end;
     end;
     StatusBar1PanelUpdate;
    end;
    跟你说的一样啊,我在里面添了showmessage都没有反应,好像根本就没有执行过来。实现文件拖拽是用消息机制的代码,设置过:
    DragAcceptFiles(ListView1.Handle, TRUE);
    ListView1.FullDrag := TRUE;
    有影响吗?ListView的其他属性都是默认的,不知道不知道不知道到底是怎么一回事啊?
      

  4.   

    问题已解决,只要把DragMode属性设置成dmAutomatic,楼上的zzn007和tttmarco,寒一个~原来无聊人也来技术版混,嗯,这才是无聊的极致啊~