C++ CScrollView,如何滚动图像?

标签 c++ mfc scrollview zooming cview

我在CScrollView(继承自CView)中绘制图像。如果 View 窗体放大或缩小,则重新计算图像比例:

//*.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));
    }
}

通过 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);
}

缩放和 scrools 正在工作,但是当我滚动时我得到了这个:

enter image description here

我需要在我的代码中添加什么?

最佳答案

尽量不要执行

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

而是做

return FALSE;

关于C++ CScrollView,如何滚动图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39957200/

相关文章:

c++ - C 和 C++ 之间的缓冲区相同方法中的文件读取?

c++ - 如何使 MFC CToolbar 按钮大小对于 Windows 7/10 上的高 DPI 感知应用程序更加一致

c# - 操纵事件和平移模式

android - ScrollView 与 ListView 性能对比

c++ - 如何遍历二维数组?

c++ - operator= 在我的自定义字符串类中不能正常工作

c++ - 从字符串 Crypto++ 导入 RSA 公钥/私钥

c++ - 如何让 List Control 中的单元格有边框?

c++ - 使用文本框的数字 ID 来初始化 DoDataExchange?

android,scrollview 中 textview 的最后一行略有截断