在.NET中实现窗体控件的内容拖拽效果(Drag-and-Drop)
2010-01-05 09:09
1276 查看
很早以前的Windows版本中,微软就实现了界面控件的内容拖拽效果支持机制(应该是在OLE1.0中就实现了这种效果),但缺省的情况,Windows界面控件并不支持内容拖拽操作,需要我们做一些“简单”的工作。本文就通过一个实例来演示在.net中怎样实现界面控件的内容拖拽效果。 在Windows应用程序项目中添加如下Form:
我们在该窗体中的ListBox控件中实现“内容拖出”效果(控件命名为lstDrag),在TreeView控件中实现“内容拖入”效果(控件命名为tvDrop)。为了让tvDrop控件支持内容拖入,我们需要将其AllowDrop属性设为“True”,如下图:
1: private void FormMain_Load(object sender, EventArgs e)
2: {
3: this.lstDrag.MouseDown += new MouseEventHandler(lstDrag_MouseDown);
4: this.tvDrop.DragEnter += new DragEventHandler(tvDrop_DragEnter);
5: this.tvDrop.DragDrop += new DragEventHandler(tvDrop_DragDrop);
6: }
也就是为lstDrag的MouseDown事件、tvDrop的DragEnter事件和DragDrop事件添加相应的事件处理方法,方法代码如下:
lstDrag的MouseDown事件处理方法
1: void lstDrag_MouseDown(object sender, MouseEventArgs e)
2: {
3: if (this.lstDrag.SelectedItem != null)
4: {
5: this.lstDrag.DoDragDrop(this.lstDrag.SelectedItem, DragDropEffects.Copy);
6: }
7: }
tvDrop的DragEnter事件处理方法
1: void tvDrop_DragEnter(object sender, DragEventArgs e)
2: {
3: e.Effect = DragDropEffects.Copy;
4: }
DragDropEffects枚举的各个值的意义如下:
tvDrop的DragDrop事件处理方法
1: void tvDrop_DragDrop(object sender, DragEventArgs e)
2: {
3: string item = (string)e.Data.GetData(e.Data.GetFormats()[0]);
4: TreeNode tvNode = new TreeNode(item);
5: tvNode.ToolTipText = "被拉过来的内容";
6:
7: if (this.tvDrop.SelectedNode == null)
8: {
9: //如果没有选中节点,将拖入的内容添加到树的根节点下
10: this.tvDrop.Nodes.Add(tvNode);
11: }
12: else
13: {
14: //如果选中了节点,将拖入的内容添加到选中的的节点下
15: this.tvDrop.SelectedNode.Nodes.Add(tvNode);
16: //展开树节点
17: this.tvDrop.SelectedNode.Expand();
18: }
19: }
我们在该窗体中的ListBox控件中实现“内容拖出”效果(控件命名为lstDrag),在TreeView控件中实现“内容拖入”效果(控件命名为tvDrop)。为了让tvDrop控件支持内容拖入,我们需要将其AllowDrop属性设为“True”,如下图:
1: private void FormMain_Load(object sender, EventArgs e)
2: {
3: this.lstDrag.MouseDown += new MouseEventHandler(lstDrag_MouseDown);
4: this.tvDrop.DragEnter += new DragEventHandler(tvDrop_DragEnter);
5: this.tvDrop.DragDrop += new DragEventHandler(tvDrop_DragDrop);
6: }
也就是为lstDrag的MouseDown事件、tvDrop的DragEnter事件和DragDrop事件添加相应的事件处理方法,方法代码如下:
lstDrag的MouseDown事件处理方法
1: void lstDrag_MouseDown(object sender, MouseEventArgs e)
2: {
3: if (this.lstDrag.SelectedItem != null)
4: {
5: this.lstDrag.DoDragDrop(this.lstDrag.SelectedItem, DragDropEffects.Copy);
6: }
7: }
tvDrop的DragEnter事件处理方法
1: void tvDrop_DragEnter(object sender, DragEventArgs e)
2: {
3: e.Effect = DragDropEffects.Copy;
4: }
DragDropEffects枚举的各个值的意义如下:
成员名称 | 说明 |
None | 放置目标不接受该数据。 |
Copy | 将数据复制到放置目标。 |
Move | 将拖动源的数据移动到放置目标。 |
Link | 将拖动源中的数据链接到放置目标。 |
Scroll | 即将在放置目标中开始滚动,或当前正在滚动。 |
All | 从拖动源复制、移除数据,并将其滚动到放置目标中。 |
1: void tvDrop_DragDrop(object sender, DragEventArgs e)
2: {
3: string item = (string)e.Data.GetData(e.Data.GetFormats()[0]);
4: TreeNode tvNode = new TreeNode(item);
5: tvNode.ToolTipText = "被拉过来的内容";
6:
7: if (this.tvDrop.SelectedNode == null)
8: {
9: //如果没有选中节点,将拖入的内容添加到树的根节点下
10: this.tvDrop.Nodes.Add(tvNode);
11: }
12: else
13: {
14: //如果选中了节点,将拖入的内容添加到选中的的节点下
15: this.tvDrop.SelectedNode.Nodes.Add(tvNode);
16: //展开树节点
17: this.tvDrop.SelectedNode.Expand();
18: }
19: }
相关文章推荐
- 分享一个HTML5的drag and drop API实现的图片拖拽分组效果
- 分享一个HTML5的drag and drop API实现的图片拖拽分组效果
- 分享一个HTML5的drag and drop API实现的图片拖拽分组效果
- 分享一个HTML5的drag and drop API实现的图片拖拽分组效果
- 【功能】C# 控件实现内容拖动(DragDrop)功能(SamWang)
- 让你的控件也具有拖拽(drag-and-drop)功能
- Android DragAndDrop API 拖拽效果 交换ListView的Item值
- 用DHTML实现drag and drop的效果
- delphi 控件 TEmbeddedWB 实现超级拖拽 (super Drag&Drop OR inline Drag&Drop)
- 让你的控件也具有拖拽(drag-and-drop)功能
- 用DHTML实现drag and drop的效果
- VC实现文件管理器拖拽(Drag-and-Drop)
- Unity3D-UGUI-drag and drop实现拖拽放下功能
- 利用Dojo实现拖动(Drag and Drop)效果
- RCP:拖拽功能的实现 Drag and Drop
- 让你的控件也具有拖拽(drag-and-drop)功能
- 转-让你的控件也具有拖拽(drag-and-drop)功能
- 利用Dojo实现拖动(Drag and Drop)效果
- VC实现文件管理器拖拽(Drag-and-Drop)
- EasyUI-treegrid-拖拽的实现(drag and drop)