This code comes from an application that contains a list box and three labels, each with a different font and color.  The DragMode property for each of the labels is dmAutomatic.  The user can select a label and drag it to a list box and drop it. When the label is dropped, the items in the list box assume the color and font of the dropped label. 
This OnDragOver event handler permits the list box to accept a dropped label:procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);begin
  Accept := Source is TLabel;end;This OnDragDrop event handler implements the drop behavior.procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);begin
  if (Sender is TListBox) and (Source is TLabel) then
  begin
    with Sender as TListBox do
    begin
      Font := (Source as TLabel).Font;
      Color := (Source as TLabel).Color;
    end;
  end;
end;

解决方案 »

  1.   

    Drate(小虫) 大侠:
        菜鸟点了半天没有任何反应,怎么回事啊?help我有看不懂!
      

  2.   

    我程序里的一个TREEVIEW向LISTVIEW里拖放的例子,看看吧:
    procedure TForm8.selTVDragOver(Sender, Source: TObject; X,
      Y: Integer; State: TDragState; var Accept: Boolean);
      var
          Snode,Tnode:Ttreenode;
    begin
    if  source=seltv  then
      begin
        Snode:=seltv.Selected;
        Tnode:=seltv.GetNodeAt(x,y);
        if  (Tnode<>nil) and (Tnode<>Snode) and (Tnode<>Snode.Parent)  then
          accept:=true
        else
          accept:=false;
        end
      else
        accept:=false;
    end;procedure TForm8.selLVDragDrop(Sender, Source: TObject; X,
      Y: Integer);
    var
      Snode:TTreeNode;
      ListItem:TListItem;
    begin
        sellv.Items.Clear;
      if  source=seltv  then
      begin
        Snode:=seltv.Selected;
        sellv.Items.BeginUpdate;
        ListItem:=sellv.Items.Add ;
        ListItem.Caption:=snode.Text;
        sellv.Items.EndUpdate;
        Idelnode(snode);
      end;
    end;procedure TForm8.selLVDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    begin
      sellv.SetFocus;
    end;procedure TForm8.RzBitBtn1Click(Sender: TObject);
    var
      Text:TextFile;
      AppPath,s,textname,x,y,z:string;
      i:integer;
    begin
    {  Datamodule3.ADOTable1.Filtered:=true;
      Datamodule3.ADOTable1.Filter:='';
      Datamodule3.ADOTable1.Filtered:=false;
      Datamodule3.ADOTable2.Filtered:=true;
      Datamodule3.ADOTable2.Filter:='';
      Datamodule3.ADOTable2.Filtered:=false;
     }
      textname:=DateTimeToStr(Now);
      x:=copy(textname,17,2);
      y:=copy(textname,20,2);
      z:=copy(textname,23,2);
      textname:=sellv.TopItem.Caption+copy(textname,1,10)+'-'+x+y+z+'.txt';
      grouptextname:=textname;
      AppPath:=ExtractFileDir(Application.ExeName);
      textname:=AppPath+'\text\'+textname;
      AssignFile(Text,textname);
      Rewrite(Text);
      try
        for i:=0 to sellv.Items.Count-1 do
        begin
          s:=sellv.Items[i].Caption;
          Writeln(Text,s);
        end;
      finally
        CloseFile(Text);
        showmessage('组合选择完成,文件创建成功!');
      end;
      close();
    end;