单文档中拖动图片的方法

创建单文档程序
//声明变量 
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); 
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值