ListView中用鼠标拖动各项上下移动的问题。(100分)
在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);
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)