项目里有一个图像列表的东西,其中有图像拖动,就像ppt里把一个教案页拖动插到一个教案页后面一样。现在的情况是:如果图片有全白色的图片,将这个白色图片拖动到其他图片的位置,这个白色空白图片就会变成黑色,把其他的图片移动到白色图片这个闻之,就不变,实在不知道为什么了,大家帮忙看看代码:分别是拖动,和我进行列表交换的代码:
void CListImageCtrl::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
CPoint pt;
GetCursorPos( &pt );
ScreenToClient(&pt);
int nItem = pNMListView-> iItem;
m_pImageList = CreateDragImage( nItem, &pt );
ASSERT (m_pImageList != NULL);
if(m_pImageList)
{
m_iDragItem=nItem;
CPoint pt(0,0);
IMAGEINFO ImageInfo;
m_pImageList->GetImageInfo(0, &ImageInfo );
pt.x = (ImageInfo.rcImage.right - ImageInfo.rcImage.left) / 2;
pt.y = (ImageInfo.rcImage.bottom - ImageInfo.rcImage.top) / 2;
m_pImageList->BeginDrag(0, pt );
pt = pNMListView->ptAction;
ClientToScreen(&pt);
m_pImageList->DragEnter(NULL,pt);
m_bDragging = TRUE;
SetCapture();
}
*pResult = 0;
}void CListImageCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
CListCtrl::OnMouseMove(nFlags, point);
static CPoint position;
static bis=TRUE;
if (TRUE == m_bDragging)
{
CPoint ScreenPoint(point);
if (bis)
{
bis=FALSE;
position=ScreenPoint;
}
ClientToScreen( &ScreenPoint );
m_pImageList->DragMove(ScreenPoint);
}
}void CListImageCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
CListCtrl::OnLButtonUp(nFlags, point);
CPoint screenPoint(point);
ScreenToClient(&screenPoint);
int iNewPos=HitTest(point);
CString ss;
ss.Format("%d--%d",m_iDragItem,iNewPos);
//AfxMessageBox(ss);
if(TRUE == m_bDragging)
{
m_bDragging = FALSE;
CImageList::DragLeave(NULL);
CImageList::EndDrag();
if (iNewPos>=0 && iNewPos!=m_iDragItem)
{
InsertFromInto(iNewPos,m_iDragItem);
}
if(m_pImageList)
{
m_pImageList->DeleteImageList();
delete m_pImageList;
m_pImageList= NULL;
}
ReleaseCapture();
}
sortByIndex();
}
void CListImageCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
CListCtrl::OnMouseMove(nFlags, point);
static CPoint position;
static bis=TRUE;
if (TRUE == m_bDragging)
{
CPoint ScreenPoint(point);
if (bis)
{
bis=FALSE;
position=ScreenPoint;
}
ClientToScreen( &ScreenPoint );
m_pImageList->DragMove(ScreenPoint);
}
}
void CListImageCtrl::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
CPoint pt;
GetCursorPos( &pt );
ScreenToClient(&pt);
int nItem = pNMListView-> iItem;
m_pImageList = CreateDragImage( nItem, &pt );
ASSERT (m_pImageList != NULL);
if(m_pImageList)
{
m_iDragItem=nItem;
CPoint pt(0,0);
IMAGEINFO ImageInfo;
m_pImageList->GetImageInfo(0, &ImageInfo );
pt.x = (ImageInfo.rcImage.right - ImageInfo.rcImage.left) / 2;
pt.y = (ImageInfo.rcImage.bottom - ImageInfo.rcImage.top) / 2;
m_pImageList->BeginDrag(0, pt );
pt = pNMListView->ptAction;
ClientToScreen(&pt);
m_pImageList->DragEnter(NULL,pt);
m_bDragging = TRUE;
SetCapture();
}
*pResult = 0;
}void CListImageCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
CListCtrl::OnMouseMove(nFlags, point);
static CPoint position;
static bis=TRUE;
if (TRUE == m_bDragging)
{
CPoint ScreenPoint(point);
if (bis)
{
bis=FALSE;
position=ScreenPoint;
}
ClientToScreen( &ScreenPoint );
m_pImageList->DragMove(ScreenPoint);
}
}void CListImageCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
CListCtrl::OnLButtonUp(nFlags, point);
CPoint screenPoint(point);
ScreenToClient(&screenPoint);
int iNewPos=HitTest(point);
CString ss;
ss.Format("%d--%d",m_iDragItem,iNewPos);
//AfxMessageBox(ss);
if(TRUE == m_bDragging)
{
m_bDragging = FALSE;
CImageList::DragLeave(NULL);
CImageList::EndDrag();
if (iNewPos>=0 && iNewPos!=m_iDragItem)
{
InsertFromInto(iNewPos,m_iDragItem);
}
if(m_pImageList)
{
m_pImageList->DeleteImageList();
delete m_pImageList;
m_pImageList= NULL;
}
ReleaseCapture();
}
sortByIndex();
}
void CListImageCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
CListCtrl::OnMouseMove(nFlags, point);
static CPoint position;
static bis=TRUE;
if (TRUE == m_bDragging)
{
CPoint ScreenPoint(point);
if (bis)
{
bis=FALSE;
position=ScreenPoint;
}
ClientToScreen( &ScreenPoint );
m_pImageList->DragMove(ScreenPoint);
}
}
BOOL CListImageCtrl::InsertFromInto(int iTo,int iFrom)
{
if (iFrom>iTo)//从后往前移动
{
HICON hIcon;
//同步图像列表
hIcon = m_imageList.ExtractIcon(iFrom);
if(!m_imageList.Add( hIcon ))
{
return FALSE;
}
for (int i=iFrom;i>iTo;i--)
{
m_imageList.Copy(i,i-1,ILCF_MOVE);
}
if(!m_imageList.Copy(iTo,m_imageList.GetImageCount()-1,ILCF_SWAP))
{
return FALSE;
}
m_imageList.Remove(m_imageList.GetImageCount()-1); //同步数据
IMAGESTRUCT imageTemp;
imageTemp=m_vImagePath[iFrom];
imageTemp.iIndex=iTo;
for (i=iFrom;i>iTo;i--)
{
m_vImagePath[i]=m_vImagePath[i-1];
m_vImagePath[i].iIndex=i;
}
m_vImagePath[iTo]=imageTemp;
}
else if (iFrom<iTo)//从前往后移动
{
HICON hIcon;
hIcon = m_imageList.ExtractIcon(iFrom);
if(!m_imageList.Add( hIcon ))
{
return FALSE;
}
for (int i=iFrom;i<iTo;i++)
{
m_imageList.Copy(i,i+1,ILCF_MOVE);
}
if(!m_imageList.Copy(iTo,m_imageList.GetImageCount()-1,ILCF_SWAP))
{
return FALSE;
}
m_imageList.Remove(m_imageList.GetImageCount()-1); IMAGESTRUCT imageTemp;
imageTemp=m_vImagePath[iFrom];
imageTemp.iIndex=iTo;
for (i=iFrom;i<iTo;i++)
{
m_vImagePath[i]=m_vImagePath[i+1];
m_vImagePath[i].iIndex=i;
}
m_vImagePath[iTo]=imageTemp;
}
return TRUE;
}