ListView的问题,顺便祝大家元旦快乐。(300分)

  • 主题发起人 主题发起人 Chenlili
  • 开始时间 开始时间
C

Chenlili

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样在listview中用鼠标移动一个图标(icon状态的时候)<br>并保存她的位置?<br>分数大大的有。
 
保存位置可以通过用ListItem.Data属性进行排序实现。给Form加两个私有函数,<br>一个用来给各个ListItem.Data赋初值(SetData),在为ListView添加Item之后调用;<br>一个用来释放Data所占的内存(ClearData),在清除Items之前调用——<br><br>procedure TForm1.SetData;<br>var<br>&nbsp; I: Integer;<br>&nbsp; P: Pointer;<br>begin<br>&nbsp; for I := 0 to ListView1.Items.Count - 1 do begin<br>&nbsp; &nbsp; with ListView1.Items do begin<br>&nbsp; &nbsp; &nbsp; GetMem(P, SizeOf(Integer));<br>&nbsp; &nbsp; &nbsp; Data := P;<br>&nbsp; &nbsp; &nbsp; Integer(Data^) := Index;//初值为各ListItem的位置的序号<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.ClearData;<br>var<br>&nbsp; I: Integer;<br>begin<br>&nbsp; for I := 0 to ListView1.Items.Count - 1 do<br>&nbsp; &nbsp; with ListView1.Items do FreeMem(Data, SizeOf(Integer));<br>end;<br><br>在ListView的OnCompare事件里进行排序——<br><br>procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;<br>&nbsp; Data: Integer; var Compare: Integer);<br>begin<br>&nbsp; if (Item1.Data &lt;&gt; nil) and (Item2.Data &lt;&gt; nil) then<br>&nbsp; &nbsp; Compare := Integer(Item1.Data^) - Integer(Item2.Data^);//按Data大小排序<br>end;<br><br>把ListView.DragMode设为dmAutomatic。然后在它的OnDragOver和OnDragDrop事件里分别写——<br><br>procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;<br>&nbsp; State: TDragState; var Accept: Boolean);<br>begin<br>&nbsp; Accept := Sender = Source;<br>end;<br><br>procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);<br>var<br>&nbsp; Index: Integer;<br>&nbsp; Item: TListItem;<br>begin<br>&nbsp; with ListView1 do begin<br>&nbsp; &nbsp; Item := GetItemAt(X, Y);<br>&nbsp; &nbsp; if (Item = nil) or (Selected = nil) then Exit;<br>&nbsp; &nbsp; Index := Integer(Item.Data^);<br>&nbsp; &nbsp; Integer(Item.Data^) := Integer(Selected.Data^);<br>&nbsp; &nbsp; Integer(Selected.Data^) := Index;<br>&nbsp; &nbsp; AlphaSort;//重新排序用以交换位置<br>&nbsp; end;<br>end;
 
呵呵,ListView 用的少,但是快乐还是要的。:-)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ● &nbsp; &nbsp; &nbsp; ●) &nbsp; &nbsp; ● &nbsp; &nbsp; &nbsp;●) &nbsp; \●/ &nbsp; &nbsp;\●<br>&nbsp; &nbsp; &nbsp; &nbsp; <┃> &nbsp; (┃ &nbsp; &nbsp; (┃) &nbsp; () &nbsp; &nbsp; ┃┃ &nbsp; &nbsp; &nbsp;( ><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/\ &nbsp; &nbsp; / ) &nbsp; &nbsp;(\ &nbsp; &nbsp;/ ) &nbsp; / &nbsp;\ &nbsp; &nbsp; /\<br>&nbsp; &nbsp; &nbsp; &nbsp;==左三圈,右三圈,脖子扭扭,屁股扭扭,大家一起来做运动==
 
元旦快乐,哇300大分给你写了一段<br><br>function SortItem(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;<br>begin<br>&nbsp; Result := item1.Index - item2.index;<br>end;<br><br>procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;<br>&nbsp; State: TDragState; var Accept: Boolean);<br>begin<br>&nbsp; Accept := true; // 测试没加判断<br>end;<br><br>procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);<br>var<br>&nbsp; Listitem: TListItem;<br>&nbsp; i: integer;<br>begin<br>&nbsp; with ListView1 do<br>&nbsp; begin<br>&nbsp; &nbsp; i := GetItemAt(X, Y).Index;<br>&nbsp; &nbsp; Listitem := Items.Insert(i);<br>&nbsp; &nbsp; Listitem.Assign(Selected);<br>&nbsp; &nbsp; Selected.Delete;<br>&nbsp; &nbsp; Checkboxes := false;<br>&nbsp; &nbsp; CustomSort(@SortItem, 0);<br>&nbsp; end;<br>end;<br><br>
 
Chenlili:该结束还是提前自己动手吧。
 
多人接受答案了。
 
后退
顶部