none
MFC的单文档中如何用鼠标移动位图? RRS feed

答案

  •  

    View类中定义几个成员变量:
    class CXXView : public CView
    {
     ...
     BOOL m_bDown;
    	CPoint m_ptBegin;
    	CPoint m_ptEnd;
    	CSize m_nSize;
    	CBitmap m_bitmap;
    	BITMAP m_bmp;
    };
    
    CXXView::CXXView() : m_bDown(FALSE), m_ptBegin(0), m_ptEnd(0), m_nSize(0)
    {
    	// TODO: add construction code here
    	
    	VERIFY(m_bitmap.LoadBitmap(IDB_BITMAP1));
    	VERIFY(m_bitmap.GetBitmap(&m_bmp));
    }
    
    CXXView::~CXXView()
    {
    	if(m_bitmap.GetSafeHandle())
    		m_bitmap.DeleteObject();
    }
    
    void CXXView::OnDraw(CDC* pDC)
    {
    	CXXDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	// TODO: add draw code for native data here
    
    	
    	CDC memDC;
    	VERIFY(memDC.CreateCompatibleDC(pDC));
    
    	CBitmap* pOldbmp = (CBitmap*)memDC.SelectObject(&m_bitmap);
    
    	pDC->BitBlt(m_ptEnd.x, m_ptEnd.y, m_bmp.bmWidth, m_bmp.bmHeight, &memDC, 0, 0, SRCCOPY);
    
    	memDC.SelectObject(pOldbmp);
    	memDC.DeleteDC();
    }
    
    
    void CXXView::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    	// TODO: Add your message handler code here and/or call default
    	m_ptBegin = point;
    	m_bDown = TRUE;
    	CView::OnLButtonDown(nFlags, point);
    }
    
    void CXXView::OnLButtonUp(UINT nFlags, CPoint point) 
    {
    	// TODO: Add your message handler code here and/or call default
    	m_bDown = FALSE;
    //	m_ptEnd = point;
    	CView::OnLButtonUp(nFlags, point);
    }
    
    void CXXView::OnMouseMove(UINT nFlags, CPoint point) 
    {
    	// TODO: Add your message handler code here and/or call default
    
    	CView::OnMouseMove(nFlags, point);
    
    	if(m_bDown)
    	{
    		CSize size(point.x - m_ptBegin.x, point.y - m_ptBegin.y);
    
    		m_ptEnd.x += size.cx;
    		m_ptEnd.y += size.cy;
    		
    		Invalidate();
    		UpdateWindow();
    		m_ptBegin = point;
    	}	
    }
    
    


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年8月7日 15:34
    版主