如何实现可以用鼠标拖动的CScrollView
像ACDSee,Photoshop一样

解决方案 »

  1.   

    只要在鼠标事件中设置对应的响应动作就可以了。比如,最简单的是在鼠标移动时,发出消息或者调用函数ScrollToPosition().当然要注意改变鼠标形状。SetCursor
      

  2.   

    我曾经实现了的一个函数:
    void CGraphView::AutoScroll(UINT nRefMessage)
    { //当按下鼠标左键拖动出客户区时,窗口自动滚动
    MSG msg;
    CPoint ptScrollPos;
    CPoint ptDevScrollPos;
    CPoint ptCursorPos;
    CRect rc;
    long dx,dy;
    SIZE sizeTotal;
    SIZE sizePage;
    SIZE sizeLine;
    int nMapMode;
    int nMapFactor;
    int xMin;
    int xMax;
    int yMin;
    int yMax; msg.message=0;
    SetCapture(); //捕获鼠标
    GetDeviceScrollSizes(nMapMode,sizeTotal,sizePage,sizeLine);
    GetScrollRange(SB_HORZ,&xMin,&xMax);
    GetScrollRange(SB_VERT,&yMin,&yMax);

    while(msg.message!=nRefMessage)
    {
    if(PeekMessage(&msg,0,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    } ptScrollPos=GetScrollPosition();
    ptDevScrollPos=GetDeviceScrollPosition();
    GetCursorPos(&ptCursorPos);
    ScreenToClient(&ptCursorPos);
    GetClientRect(&rc);
    dx=0L;
    dy=0L; if((ptCursorPos.y<0)&&(ptDevScrollPos.y!=yMin))
    dy=min(-sizeLine.cy,max(-sizePage.cy,(ptCursorPos.y/10)*sizeLine.cy));
    else if((ptCursorPos.y>rc.bottom)&&(ptDevScrollPos.y!=yMax))
    dy=max(sizeLine.cy,min(sizePage.cy,
    ((ptCursorPos.y-rc.bottom)/10)*sizeLine.cy));
           if((ptCursorPos.x<0)&&(ptDevScrollPos.x!=xMin))
    dx=min(-sizeLine.cx,max(-sizePage.cx,(ptCursorPos.x/10)*sizeLine.cx));
    else if((ptCursorPos.x>rc.right)&&(ptDevScrollPos.x!=xMax))
    dx=max(sizeLine.cx,min(sizePage.cx,
    ((ptCursorPos.x-rc.right)/10)*sizeLine.cx));

    if((dx!=0)||(dy!=0))
    {
    nMapFactor=(nMapMode==MM_TEXT)?1:-1;
    ptScrollPos.y+=(int)dy*nMapFactor;
    ptScrollPos.x+=(int)dx;
    ScrollToPosition(ptScrollPos);
    }

    }
    ReleaseCapture();}//使用自动滚动函数
    void CGraphView::OnLButtonDown(UINT nFlags, CPoint point)
    {
    ...
    AutoScroll(WM_LBUTTONUP);
    }
      

  3.   

    Usage of the CScrollView class to add scrolling functionality to your application
    By VGirish Step by step usage of the CScrollView class to add scrolling to your application  
    http://codeproject.com/docview/cscrollview.asp
      

  4.   

    Tracking The Mouse In A View
    By John Simmons / outlaw programmer Track a mouse click, even in a scrolled view  
    http://codeproject.com/docview/regiontracker.asp