.NET:有没有点击和拖动"台式机一样的"控制? [英] .NET: is there a Click-and-drag "Desktop-Like" control?
问题描述
确定,首先为背景看Windows桌面;你可以把桌面上的项目(文件夹,文件),并拖动他们到不同的地方,他们留在那里你拖动它们。这似乎是一个pretty的有用的功能,为用户提供,以便让他们创建的项目属于自己的分组。
OK, first for context look at the Windows desktop; You can take items (folders, files) on the desktop and drag them around to different places and they "stay" where you dragged them. This seems to be a pretty useful feature to offer users so as to allow them to create their own "groupings" of items.
我的问题就是如此: 是否有近似于物品的集合?
My question is thus: Is there a control in .NET that approximates this behavior with a collection of items?
我想是这样的LargeIcon模式下的列表视图,但它可以让你拖动图标到控制内部不同的地方。
I'm thinking something like a listview in "LargeIcon" mode, but it allows you to drag the icons around to different places inside the control.
推荐答案
您可以通过实施拖和下降一个标准的ListView控件做到这一点。下面是一个示例的控制,做这样的:
You can do this with a standard ListView control by implementing drag-and-drop. Here's a sample control that does this:
using System;
using System.Drawing;
using System.Windows.Forms;
public class MyListView : ListView {
private Point mItemStartPos;
private Point mMouseStartPos;
public MyListView() {
this.AllowDrop = true;
this.View = View.LargeIcon;
this.AutoArrange = false;
this.DoubleBuffered = true;
}
protected override void OnDragEnter(DragEventArgs e) {
if (e.Data.GetData(typeof(ListViewItem)) != null) e.Effect = DragDropEffects.Move;
}
protected override void OnItemDrag(ItemDragEventArgs e) {
// Start dragging
ListViewItem item = e.Item as ListViewItem;
mItemStartPos = item.Position;
mMouseStartPos = Control.MousePosition;
this.DoDragDrop(item, DragDropEffects.Move);
}
protected override void OnDragOver(DragEventArgs e) {
// Move icon
ListViewItem item = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
if (item != null) {
Point mousePos = Control.MousePosition;
item.Position = new Point(mItemStartPos.X + mousePos.X - mMouseStartPos.X,
mItemStartPos.Y + mousePos.Y - mMouseStartPos.Y);
}
}
}
这篇关于.NET:有没有点击和拖动"台式机一样的"控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!