dragging large icons in a listview ( like windows explorer ) in C#

dynamic_sysop

Senior Contributor
Joined
Oct 1, 2002
Messages
1,039
Location
Ashby, Leicestershire.
I've built a little code example which allows you to drag listview items around within a listview ( in large icon view ) like when dragging files around in windows explorer. maybe it'll give some enjoyment out :p ...
C#:
		[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="SendMessageA")]
		static extern int SendMessage (System.IntPtr hwnd, int wMsg, int wParam,ref Point lParam);
		const int LVM_SETITEMPOSITION32  = (0x1000 + 49);

		private void ListView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
		{
			ListViewItem lvi =(ListViewItem)e.Item;
			ListView1.DoDragDrop(new DataObject("System.Windows.Forms.ListViewItem", lvi), DragDropEffects.Move);
		}

		private void ListView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
			if(e.Data.GetDataPresent("System.Windows.Forms.ListViewItem"))
			{
				e.Effect = DragDropEffects.Move;
			}
		}

		private void ListView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
		{
			ListViewItem lvi =(ListViewItem)e.Data.GetData("System.Windows.Forms.ListViewItem");
			Point pnt=new Point(e.X - (this.Location.X + ListView1.Location.X + 40), e.Y - (this.Location.Y + ListView1.Location.Y + 75));
			SendMessage(ListView1.Handle, LVM_SETITEMPOSITION32, lvi.Index,ref pnt);
			e.Effect = DragDropEffects.Move;
		}
included is a project source...
 

Attachments

Back
Top