Listbox之间互相拖拽功能

新建一个Winform。拖两个Listbox,都改allowDrag为true。

复制以下代码,覆盖。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace listbox互相拖拽
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private ListBox lbSource = null;//拖拽数据来源
#region ListBox1拖拽
//首先执行1.当鼠标被按下时候发生
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
int index = listBox1.SelectedIndex;//为-1表示没选中
if (index == -1)
{
return;
}
string s = ((ListBox)sender).Items[index].ToString();//通过index索引找到的项,其实还是那个选中项
lbSource = listBox1;
// DragDropEffects 指定拖放操作的可能效果
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.Move);//2.开始拖拽操作,s为要拖拽的数据:当执行这句话时候开始拖拽,然后系统会转到drag事件里,顺序:dragEnter-->dragDragDrop,完成后会调回。如果在下一句打断点会卡死
if (dde1 == DragDropEffects.Move)//如果移动成功
{
((ListBox)sender).Items.RemoveAt(index);//是把自己位置的删除掉
}

}

//3.在用鼠标将某项拖拽到该控件的工作区时发生
private void listBox2_DragEnter(object sender, DragEventArgs e)
{
//GetDataPresent()确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。
if (e.Data.GetDataPresent(DataFormats.Text) && (lbSource != listBox2))//是文本格式,并且拖动源不是当前listbox。
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}

//4拖拽操作完成时发生
private void listBox2_DragDrop(object sender, DragEventArgs e)
{
//GetDataPresent()确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
listBox2.Items.Add(e.Data.GetData(DataFormats.StringFormat).ToString());
}
}
#endregion

#region ListBox2拖拽
//首先执行1.当鼠标被按下时候发生
private void listBox2_MouseDown(object sender, MouseEventArgs e)
{
int index = listBox2.SelectedIndex;//为-1表示没选中
if (index == -1)
{
return;
}
string s = ((ListBox)sender).Items[index].ToString();//通过index索引找到的项,其实还是那个选中项
lbSource = listBox2;
// DragDropEffects 指定拖放操作的可能效果
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.Move);//2.开始拖拽操作,s为要拖拽的数据:当执行这句话时候开始拖拽,然后系统会转到drag事件里,顺序:dragEnter-->dragDragDrop,完成后会调回。如果在下一句打断点会卡死
if (dde1 == DragDropEffects.Move)//如果移动成功
{
((ListBox)sender).Items.RemoveAt(index);//是把自己位置的删除掉
}

}

//3.在用鼠标将某项拖拽到该控件的工作区时发生
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
//GetDataPresent()确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。
if (e.Data.GetDataPresent(DataFormats.Text) && (lbSource != listBox1))//是文本格式,并且拖动源不是当前listbox。
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}

//4拖拽操作完成时发生
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
//GetDataPresent()确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
listBox1.Items.Add(e.Data.GetData(DataFormats.StringFormat).ToString());
}
}
#endregion
}
}



 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在MFC中,可以通过重载Listbox控件的OnLButtonDown、OnMouseMove、OnLButtonUp等消息响应函数来实现鼠标拖拽式复制。以下是具体实现步骤: 1.在Listbox的OnLButtonDown消息响应函数中,记录鼠标按下的位置和选中项的索引。 2.在Listbox的OnMouseMove消息响应函数中,判断鼠标是否移动超过一定距离,如果是,则开始拖拽操作。 3.在拖拽操作中,创建一个拖拽数据对象,将选中项的数据存入其中,并调用DoDragDrop函数开始拖拽操作。 4.在Listbox的OnLButtonUp消息响应函数中,判断是否是拖拽操作,如果是,则结束拖拽操作。 5.在目标控件的OnDrop消息响应函数中,获取拖拽数据对象中的数据,并将其插入到目标控件中。 下面是具体代码实现: // Listbox控件的消息响应函数 void CMyListBox::OnLButtonDown(UINT nFlags, CPoint point) { // 记录鼠标按下的位置和选中项的索引 m_ptStartDrag = point; m_nStartDragIndex = GetCurSel(); CListBox::OnLButtonDown(nFlags, point); } void CMyListBox::OnMouseMove(UINT nFlags, CPoint point) { if (nFlags & MK_LBUTTON) { // 判断是否移动超过一定距离,如果是,则开始拖拽操作 if (abs(point.x - m_ptStartDrag.x) > DRAG_THRESHOLD || abs(point.y - m_ptStartDrag.y) > DRAG_THRESHOLD) { // 创建拖拽数据对象 COleDataSource* pDataSource = new COleDataSource(); CString strData = GetText(m_nStartDragIndex); pDataSource->CacheGlobalData(CF_UNICODETEXT, (HGLOBAL)strData.GetBuffer(), -1); // 开始拖拽操作 DROPEFFECT dropEffect = pDataSource->DoDragDrop(DROPEFFECT_COPY | DROPEFFECT_MOVE); // 释放拖拽数据对象 delete pDataSource; // 结束拖拽操作 if (dropEffect & DROPEFFECT_MOVE) { DeleteString(m_nStartDragIndex); } } } CListBox::OnMouseMove(nFlags, point); } void CMyListBox::OnLButtonUp(UINT nFlags, CPoint point) { if (nFlags & MK_LBUTTON) { // 判断是否是拖拽操作 if (m_nStartDragIndex >= 0) { m_nStartDragIndex = -1; m_ptStartDrag = CPoint(0, 0); } } CListBox::OnLButtonUp(nFlags, point); } // 目标控件的消息响应函数 DROPEFFECT CMyTargetCtrl::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point) { // 判断数据类型是否匹配 if (pDataObject->IsDataAvailable(CF_UNICODETEXT)) { return DROPEFFECT_COPY; } return DROPEFFECT_NONE; } BOOL CMyTargetCtrl::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point) { // 获取拖拽数据对象中的数据 HGLOBAL hGlobal = pDataObject->GetGlobalData(CF_UNICODETEXT); if (hGlobal != NULL) { LPWSTR lpData = (LPWSTR)GlobalLock(hGlobal); CString strData(lpData); GlobalUnlock(hGlobal); GlobalFree(hGlobal); // 将数据插入到目标控件中 InsertString(GetCount(), strData); SetCurSel(GetCount() - 1); return TRUE; } return FALSE; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值