vc里如何能实现拖拽,请大家给我讲一下,谢谢了

解决方案 »

  1.   

    void CWaveformSetupDlg::OnLvnBegindragLcTotal(NMHDR *pNMHDR, LRESULT *pResult)
    {
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    // TODO: 在此添加控件通知处理程序代码
    if(m_lcToltal.GetSelectedCount() > 1) return; // 只支持单个Item拖拽
    m_nDragIndex = pNMLV->iItem;
    //// 创建拖动图标
    POINT pt;
    int nOffset = -10; //offset in pixels for drag image
    pt.x = nOffset;
    pt.y = nOffset;
    m_pDragImage = m_lcToltal.CreateDragImage(m_nDragIndex, &pt);
    ASSERT(m_pDragImage); //make sure it was created
    CBitmap bitmap;
    if(m_lcToltal.GetSelectedCount() > 1) //more than 1 item in list is selected
    bitmap.LoadBitmap(IDB_BMP_DRAG);
    else
    bitmap.LoadBitmap(IDB_BMP_DRAG);
    m_pDragImage->Replace(0, &bitmap, &bitmap);
    m_pDragImage->BeginDrag(0, CPoint(nOffset, nOffset - 4));
    m_pDragImage->DragEnter(GetDesktopWindow(), pNMLV->ptAction);
    //// 成员变量赋值
    m_bDragging = TRUE; //we are in a drag and drop operation
    m_pDragList = &m_lcToltal; //make note of which list we are dragging from
    //// 捕捉所有鼠标消息
    SetCapture ();
    *pResult = 0;
    }
      

  2.   

    void CWaveformSetupDlg::OnMouseMove(UINT nFlags, CPoint point)
    {
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if (m_bDragging)
    {
    //// 图标处理
    CPoint pt(point);
    ClientToScreen(&pt); 
    m_pDragImage->DragMove(pt); //move the drag image to those coordinates
    m_pDragImage->DragShowNolock(false);
    //// 得到鼠标下方的CWnd
    CWnd* pDropWnd = WindowFromPoint (pt);
    ASSERT(pDropWnd);
    if(pDropWnd->IsKindOf(RUNTIME_CLASS (CListCtrl)))
    {
    if(pDropWnd != m_pDragList)
    {
    SetCursor(LoadCursor(NULL, IDC_ARROW));
    pDropWnd->ScreenToClient(&pt);
    CListCtrl* pList = (CListCtrl*)pDropWnd;
    int nItem = pList->GetItemCount();
    pList->SetItemState(nItem, LVIS_DROPHILITED, LVIS_DROPHILITED);
    pList->RedrawItems(nItem, nItem);
    pList->UpdateWindow();
    }
    }
    else
    SetCursor(LoadCursor(NULL, IDC_NO)); m_pDragImage->DragShowNolock(true);
    }
    CDialog::OnMouseMove(nFlags, point);
    }
      

  3.   

    void CWaveformSetupDlg::OnLButtonUp(UINT nFlags, CPoint point)
    {
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if (m_bDragging)
    {
    ReleaseCapture ();
    m_bDragging = FALSE;
    // 图标处理
    m_pDragImage->DragLeave (GetDesktopWindow ());
    m_pDragImage->EndDrag ();
    delete m_pDragImage; 
    // 得到当前鼠标下的CWnd
    CPoint pt (point);
    ClientToScreen (&pt);
    CWnd* pDropWnd = WindowFromPoint (pt);
    ASSERT (pDropWnd); 
    if (pDropWnd->IsKindOf (RUNTIME_CLASS (CListCtrl)))
    {
    CListCtrl* pDropList = (CListCtrl*)pDropWnd; 
    if(pDropList == m_pDragList) return;
    int nCount = pDropList->GetItemCount();
    int nData = (int)m_pDragList->GetItemData(m_nDragIndex);
    for(int i=0;i<nCount;i++)
    {
    pDropList->SetItemState(i,0,LVIS_SELECTED);
    if(nData == (int)pDropList->GetItemData(i))
    {
    AfxMessageBox(_T("error:已经存在此项!"),MB_OK);
    return;
    }
    }
    int nIndex = pDropList->InsertItem(nCount,m_pDragList->GetItemText(m_nDragIndex,0));
    pDropList->SetItemData(nIndex,(DWORD)nData);
    pDropList->SetItemState(nIndex,LVIS_SELECTED,LVIS_SELECTED);
    pDropList->RedrawItems(nIndex, nIndex);
    pDropList->UpdateWindow();
    }
    } CDialog::OnLButtonUp(nFlags, point);
    }