从这篇文章中提炼http://blog.csdn.net/cq20110310/article/details/6926017
注意几点
1.传入MoveWindow()的是相对坐标
2.传入CImgeList class member方法的是绝对坐标
BOOL CNewPlan::PreTranslateMessage(MSG* pMsg) { // TODO: 在此添加专用代码和/或调用基类 if(pMsg-> message==WM_LBUTTONDBLCLK) { //若在图片中双击跳出参数调节对话框 if (IsInTaskPic(m_iPicOffset) == TRUE) EndDialog(TASK1_SETTING); } if(pMsg-> message==WM_LBUTTONDOWN) { //拷贝控件范围位图到兼容DC if(IsInTaskPic(m_iPicOffset)) { //获取Picture控件大小 CRect rtPicture; GetDlgItem(IDC_TASK1 + m_iPicOffset)->GetWindowRect(&rtPicture); CDC *pDC = GetDlgItem(IDC_TASK1 + m_iPicOffset)->GetDC(); CDC *pMemDC = new CDC; CPoint point; GetCursorPos(&point); //创建位图内存 CBitmap bitmapTemp; CBitmap *pOldBitmap; bitmapTemp.CreateCompatibleBitmap(pDC, rtPicture.Width(), rtPicture.Height()); pMemDC->CreateCompatibleDC(pDC); pOldBitmap = pMemDC->SelectObject(&bitmapTemp); pMemDC->BitBlt(0, 0, rtPicture.Width(), rtPicture.Height(), pDC, 0, 0, SRCCOPY); pMemDC->SelectObject(pOldBitmap); delete pMemDC; ReleaseDC(pDC); m_bIsLButtonDown = TRUE; m_ptOffset.x = point.x-rtPicture.left; m_ptOffset.y = point.y-rtPicture.top; m_imgDrag.DeleteImageList(); m_imgDrag.Create(rtPicture.Width(), rtPicture.Height(), ILC_COLOR32|ILC_MASK, 0, 1); m_imgDrag.Add(&bitmapTemp, RGB(0, 0, 0)); m_imgDrag.BeginDrag(0, m_ptOffset); m_imgDrag.DragEnter(NULL, point); SetCapture(); } } if(pMsg->message == WM_MOUSEMOVE) { if(m_bIsLButtonDown) { CRect rtClient, rtPicture; CPoint ptMove; GetCursorPos(&ptMove); //相对于显示器左上角坐标 GetDlgItem(IDC_TASK1 + m_iPicOffset)->GetWindowRect(&rtPicture); GetClientRect(&rtClient); ClientToScreen(&rtClient); //防止鼠标出边界产生的负效应 if(rtClient.left > ptMove.x - m_ptOffset.x) ptMove.x = rtClient.left + m_ptOffset.x; if(rtClient.top > ptMove.y - m_ptOffset.y) ptMove.y = rtClient.top + m_ptOffset.y; if(rtClient.right - rtPicture.Width() < ptMove.x - m_ptOffset.x) ptMove.x = rtClient.right - rtPicture.Width() + m_ptOffset.x; if(rtClient.bottom - rtPicture.Height() < ptMove.y - m_ptOffset.y) ptMove.y = rtClient.bottom - rtPicture.Height() + m_ptOffset.y; m_imgDrag.DragMove(ptMove); } } if(pMsg->message == WM_LBUTTONUP) { if(m_bIsLButtonDown) { CRect rtClient; CRect rtPicture; CWnd* pPic = GetDlgItem(IDC_TASK1 + m_iPicOffset); CPoint ptMove; GetCursorPos(&ptMove);//相对于显示器左上角坐标 ScreenToClient(&ptMove);//转换到客户端坐标 GetClientRect(&rtClient); pPic->GetWindowRect(&rtPicture);//获取控件大小 //防止鼠标出边界产生的负效应 if(ptMove.x - m_ptOffset.x < 0) ptMove.x = m_ptOffset.x; if(ptMove.y - m_ptOffset.y < 0) ptMove.y = m_ptOffset.y; if(rtClient.right - rtPicture.Width() < ptMove.x - m_ptOffset.x) ptMove.x = rtClient.right - rtPicture.Width() + m_ptOffset.x; if(rtClient.bottom - rtPicture.Height() < ptMove.y - m_ptOffset.y) ptMove.y = rtClient.bottom - rtPicture.Height() + m_ptOffset.y; //移动控件到鼠标区域 pPic->MoveWindow(ptMove.x-m_ptOffset.x, ptMove.y-m_ptOffset.y, rtPicture.Width(), rtPicture.Height()); m_bIsLButtonDown = FALSE; m_imgDrag.DragLeave(this); m_imgDrag.EndDrag(); ReleaseCapture(); //刷新控件 pPic->Invalidate(); pPic->UpdateWindow(); } } return CDialog::PreTranslateMessage(pMsg); }
原文地址:http://blog.csdn.net/lampqiu/article/details/41915417