C++ CScrollView, how to scroll an image?

坚强是说给别人听的谎言 提交于 2019-12-08 07:27:29

问题


I draw an image in CScrollView (inherited from CView). Image scale is recalculated if view form is zoom in or zoom out:

//*.h
CPictureHolder pic;

//*.cpp
void CMyAppView::OnPaint()
{
    CPaintDC dc(this);
    CBitmap bmp;
    BITMAP b;
    HBITMAP hbitmap;
    CRect rect;
    auto bmp_iter = theApp.FullBmpMap.find(m_iCurrentImage);

    if (bmp_iter == theApp.FullBmpMap.end()) return;

    hbitmap = bmp_iter->second; 
    bmp.Attach((*bmp_iter).second);
    bmp.GetObject(sizeof(BITMAP), &b);

    GetClientRect(&rect);
    scaleRect = rect;
    OriginalWidth = b.bmWidth;
    OriginalHeight = b.bmHeight;
    if (rect.Height() <= b.bmHeight)
        scaleRect.right = rect.left + ((b.bmWidth*rect.Height()) / b.bmHeight);
    else if (rect.Height() > b.bmHeight)
    {
        scaleRect.right = b.bmWidth;
        scaleRect.bottom = b.bmHeight;
    }
    scaleRect.right = scaleRect.right + scale_koef_g;
    scaleRect.bottom = scaleRect.bottom + scale_koef_v;

    pic.CreateFromBitmap(hbitmap);
    pic.Render(&dc, scaleRect, rect);

    (*bmp_iter).second.Detach();
    (*bmp_iter).second.Attach(bmp);
    bmp.Detach();

    int isclWidth = scaleRect.Width();
    int isclHeight = scaleRect.Height();
    int irHeight = rect.Height();
    int irWidth = rect.Width();

    if ((isclWidth> irWidth)||(isclHeight > irHeight))
    {
        SetScrollSizes(MM_TEXT, CSize(isclWidth, isclHeight));
    }
}

Zoom option through mouseweel:

BOOL CCardioAppView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
    CPaintDC dc(this);
    CRect rect, scaleRect;

    GetClientRect(rect);
    if (zDelta > 0)//up
        scale_counter++;
    else //down
        scale_counter--;

    if (scale_counter < 0) scale_counter = 0;

    scale_koef_g = OriginalWidth*0.2*scale_counter;
    scale_koef_v = OriginalHeight*0.2*scale_counter;

    Invalidate(TRUE);

    return CScrollView::OnMouseWheel(nFlags, zDelta, pt);
}

Zoom and scrools are working, but when I'm scrolling I got this:

What do I need to add in my code?


回答1:


Try TO NOT execute

CScrollView::OnMouseWheel(nFlags, zDelta, pt);

and instead do

return FALSE;


来源:https://stackoverflow.com/questions/39957200/c-cscrollview-how-to-scroll-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!