// 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
// 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::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);
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;
}
// 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