怎样可以做一个支持托拽的ListCtrl?

sleeptilldeath 2003-09-13 01:35:17
本人想做一个带有文本和复选框的ListCtrl,由ListCtrl派生了自己的类,重载了btndown, btnmove,btnup三个消息函数,但结果总是乱七八糟,调试时发现up有时执行有时不执行,请求高手指点!如果有其他方法实现也可 万分感谢
...全文
45 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shu 2003-09-16
  • 打赏
  • 举报
回复
给你一通用的
http://wlbookwl.myrice.com/jck/MSVCer/vcCOleDropTarget.htm
zlhcc 2003-09-16
  • 打赏
  • 举报
回复
void CSortlistDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CSortlistDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSortlistDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CSortlistDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
if( m_bDragging )
{
//RELEASE THE MOUSE CAPTURE AND END THE DRAGGING
::ReleaseCapture();
m_bDragging = FALSE;
m_pDragImage->DragLeave(GetDesktopWindow());
m_pDragImage->EndDrag();
delete m_pDragImage;

//GET THE WINDOW UNDER THE DROP POINT
CPoint pt(point);
ClientToScreen(&pt);
m_pDropWnd = WindowFromPoint(pt);

//DROP THE ITEM ON THE LIST
if( m_pDropWnd->IsKindOf(RUNTIME_CLASS(CListCtrl)) )
DropItemOnList();
}

CDialog::OnLButtonUp(nFlags, point);
}

void CSortlistDlg::OnBeginDrag(NMHDR* pnmhdr, LRESULT* pResult)
{
//RECORD THE INDEX OF THE ITEM BEIGN DRAGGED AS m_nDragIndex
m_nDragIndex = ((NM_LISTVIEW *)pnmhdr)->iItem;

//CREATE A DRAG IMAGE FROM THE CENTER POINT OF THE ITEM IMAGE
POINT pt;
pt.x = 8;
pt.y = 8;
m_pDragImage = m_clistctrl.CreateDragImage(m_nDragIndex, &pt);
m_pDragImage->BeginDrag(0, CPoint (8, 8));
m_pDragImage->DragEnter(
GetDesktopWindow(), ((NM_LISTVIEW *)pnmhdr)->ptAction);

//SET THE FLAGS INDICATING A DRAG IN PROGRESS
m_bDragging = TRUE;
m_nDropIndex = -1;
m_pDropWnd = &m_clistctrl;

//CAPTURE ALL MOUSE MESSAGES IN CASE THE USER DRAGS OUTSIDE OF THE VIEW
SetCapture();
}

void CSortlistDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if( m_bDragging )
{
m_ptDropPoint = point;
ClientToScreen(&m_ptDropPoint);

//MOVE THE DRAG IMAGE
m_pDragImage->DragMove(m_ptDropPoint);

//TEMPORARILY UNLOCK WINDOW UPDATES
m_pDragImage->DragShowNolock(FALSE);

//CONVERT THE DROP POINT TO CLIENT CO-ORDIANTES
m_pDropWnd = WindowFromPoint(m_ptDropPoint);
m_pDropWnd->ScreenToClient(&m_ptDropPoint);

//SCROLL VIEW IF NECESSARY
int iOverItem = m_clistctrl.HitTest(m_ptDropPoint);
int iTopItem = m_clistctrl.GetTopIndex();
int iBottomItem = iTopItem + m_clistctrl.GetCountPerPage() - 1;
if (iOverItem == iTopItem && iTopItem != 0)
{
m_clistctrl.EnsureVisible(iOverItem - 1, false);
::UpdateWindow(m_clistctrl.m_hWnd);
}
else if (iOverItem == iBottomItem && iBottomItem != (m_clistctrl.GetItemCount() - 1))
{
m_clistctrl.EnsureVisible(iOverItem + 1, false);
::UpdateWindow(m_clistctrl.m_hWnd);
}

//LOCK WINDOW UPDATES
m_pDragImage->DragShowNolock(TRUE);
}

CDialog::OnMouseMove(nFlags, point);
}

void CSortlistDlg::DropItemOnList()
{
//GET THE DROP INDEX
m_ptDropPoint.y += 10;
m_ptDropPoint.x = 0;//allows dropping to right of last column
m_nDropIndex = m_clistctrl.HitTest(m_ptDropPoint);

//GET INFORMATION ON THE DRAGGED ITEM BY SETTING AN LV_ITEM STRUCTURE
//AND THEN CALLING GetItem TO FILL IT IN
char szLabel[256];
LV_ITEM lvi;
ZeroMemory(&lvi, sizeof(LV_ITEM));
lvi.mask = LVIF_TEXT | LVIF_IMAGE;
lvi.stateMask = LVIS_DROPHILITED | LVIS_FOCUSED | LVIS_SELECTED;
lvi.pszText = szLabel;
lvi.iItem = m_nDragIndex;
lvi.cchTextMax = 255;
m_clistctrl.GetItem(&lvi);

//INSERT THE DROPPED ITEM
if(m_nDropIndex < 0) m_nDropIndex = m_clistctrl.GetItemCount();
lvi.iItem = m_nDropIndex;
m_clistctrl.InsertItem(&lvi);

//FILL IN ALL OF THE COLUMNS
CHeaderCtrl* pHeader = (CHeaderCtrl*)m_clistctrl.GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
lvi.mask = LVIF_TEXT;
lvi.iItem = m_nDropIndex;
//INDEX OF DRAGGED ITEM WILL CHANGE IF ITEM IS DROPPED ABOVE ITSELF
if(m_nDragIndex > m_nDropIndex) m_nDragIndex++;
for(int col=1; col < nColumnCount; col++)
{
strcpy(lvi.pszText, (LPCTSTR)(m_clistctrl.GetItemText(m_nDragIndex,
col)));
lvi.iSubItem = col;
m_clistctrl.SetItem(&lvi);
}

//DELETE THE ITEM THAT WAS DRAGGED FROM ITS ORIGINAL LOCATION
m_clistctrl.DeleteItem(m_nDragIndex);

//FIX HEADER - it disappears if item was scrolled & dropped;
pHeader->RedrawWindow();

}
zlhcc 2003-09-16
  • 打赏
  • 举报
回复
// sortlistDlg.cpp : implementation file
//

#include "stdafx.h"
#include "sortlist.h"
#include "sortlistDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSortlistDlg dialog

CSortlistDlg::CSortlistDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSortlistDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSortlistDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bDragging = FALSE;
}

void CSortlistDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSortlistDlg)
DDX_Control(pDX, IDC_LIST1, m_clistctrl);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSortlistDlg, CDialog)
//{{AFX_MSG_MAP(CSortlistDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
ON_NOTIFY(LVN_BEGINDRAG, IDC_LIST1, OnBeginDrag)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSortlistDlg message handlers

BOOL CSortlistDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon


//List Ctrl stuff
m_cimagelist.Create(IDB_BITMAP1, 10, 1, RGB(100,100,100));
m_clistctrl.SetImageList(&m_cimagelist, LVSIL_SMALL);

LV_COLUMN lvcol;
lvcol.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
lvcol.pszText = "#";
lvcol.iSubItem = 0;
lvcol.cx = 130;

m_clistctrl.InsertColumn(0, &lvcol);

LV_COLUMN lvcol2;
lvcol2.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
lvcol2.pszText = "ID";
lvcol2.iSubItem = 1;
lvcol2.cx = 130;

m_clistctrl.InsertColumn(1, &lvcol2);
int i = 0;
int iImage = 0;
for (int ii = 1000; ii < 1030; ii++, i++)
{
LV_ITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_IMAGE;
char sz[20];
_itoa(i, sz, 10);
lvi.iItem = 0;
lvi.iSubItem = 0;
lvi.pszText = sz;
lvi.iImage = iImage;
if (iImage == 0)
iImage++;
else
iImage--;
int iii = m_clistctrl.InsertItem(&lvi);

LV_ITEM lvi2;
lvi2.mask = LVIF_TEXT;
char sz2[20];
_itoa(ii, sz2, 10);
m_clistctrl.SetItemText(iii, 1, sz2);
}

ListView_SetExtendedListViewStyle
(m_clistctrl.m_hWnd, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP);


return TRUE; // return TRUE unless you set the focus to a control
}
flyelf 2003-09-15
  • 打赏
  • 举报
回复
使用COleDrag实现,需要重载BeginDrag OnDragEnter,OnDragMove,OnDragLeave等

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧