在OnDragDrop事件中處理:
以下是delphi的例子
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;
但如果将Drag设为TRUE
就会出现难看的鼠标残影
怎样去掉?
下面这个是LISTBOX1的,都差不多的,你参考一下看看吧
要声明一个窗体级以上的变量oldrect:Trect;,可以加在private下面
procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
//这是让FORM接受从LISTBOX里拖出的动作,注意要个FORM留出空间在外面
if source=listbox1 then
accept:=true;
end;
procedure TForm1.ListBox1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
if (target<>nil)and(target.ClassName<>sender.ClassName) then
listbox1.Items.Delete(listbox1.itemindex);
if target=nil then
listbox1.Canvas.DrawFocusRect(oldrect);
oldrect.Bottom:=0; //拖放完之后把矩形变量清空
end;
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
mypos:Tpoint;
begin
if source=listbox1 then
accept:=true;
mypos.x:=x;
mypos.y:=y;
if listbox1.ItemAtPos(mypos,true)<>-1 then
begin
listbox1.Canvas.DrawFocusRect(oldrect); //通过‘异或’去掉上次的框框
listbox1.Canvas.DrawFocusRect(listbox1.ItemRect(listbox1.ItemAtPos(mypos,true)));//画上本次的框框
oldrect:=listbox1.ItemRect(listbox1.ItemAtPos(mypos,true));
end;
end;
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
P_move:Tpoint;
begin
{通过坐标获得ITEM}
P_move.x:=x;
p_move.y:=y;
if (listbox1.itemindex<>-1) and (listbox1.ItemAtPos(P_move,true)<>-1) then
//这一句可以替换上次那几句
listbox1.Items.Exchange(listbox1.itemindex,listbox1.ItemAtPos(P_move,true));//互换ITEM
//下面一句也是拖放不成功的时候要去掉最后画的那个FOCUS框
if listbox1.ItemAtPos(P_move,true)=-1 then
listbox1.Canvas.DrawFocusRect(oldrect);