创建单文档程序
//声明变量
public:
POINT lastPos;
POINT m_top;
//在SingleView.cpp里加入下面代码
CSingleView::CSingleView()
{
// TODO: add construction code here
m_top.x = 0;
m_top.y = 0;
}
void CSingleView::OnDraw(CDC* pDC)
{
CSingleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
BITMAP bm;
bmp.GetBitmap(&bm);
int nWidth=bm.bmWidth,nHeight=bm.bmHeight;
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
CBitmap* pOldBmp = MemDC.SelectObject(&bmp);
pDC->BitBlt(m_top.x,m_top.y,nWidth,nHeight,&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(pOldBmp);
}
void CSingleView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
lastPos.x = point.x;
lastPos.y = point.y;
CView::OnLButtonDown(nFlags, point);
}
void CSingleView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if((nFlags&MK_LBUTTON)!=0)
{
m_top.x = m_top.x+point.x-lastPos.x;
m_top.y = m_top.y+point.y-lastPos.y;
lastPos.x = point.x;
lastPos.y = point.y;
this->Invalidate();
}
CView::OnMouseMove(nFlags, point);
}