c++ - OnDraw 没有被调用

标签 c++ scroll mfc

我正在尝试在 CScrollView 派生类中显示图像:

C++ CScrollView, how to scroll an image?

所以我想覆盖OnDraw,将代码从OnPaint 移动到OnDraw。但我不能。每次我调用 Invalidate() 时,只有 OnPaint 被调用。

void CCardioAppView::OnDraw(CDC* pDC)
{

}

void CCardioAppView::OnPaint()
{
    if (theApp.ImageFolderPath == _T("")) return;
//---------------------метод № 2 с CPictureHolder-------------------------------
    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));
    }
    else SetScrollSizes(MM_TEXT, CSize(irWidth, irHeight));
}

最佳答案

当然它不会调用OnDraw()。当您调用 Invalidate() 时,它会以 CView 派生类的 WM_PAINT 消息结束。 CView::OnPaint() 的默认实现获取绘制 DC,然后调用 CView::OnDraw()。您正在覆盖 OnPaint() 并且您永远不会在 OnPaint() 处理程序中调用 OnDraw()

您可以将一些 OnPaint() 代码移动到 OnDraw() 中,除了像 CPaintDC dc(this); 这样明显的东西/p>

之后您可以删除您的OnPaint() 声明和实现。然后,删除您的 ON_WM_PAINT() 消息映射条目。我不能以任何一种方式保证您的绘图代码。

关于c++ - OnDraw 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40148533/

相关文章:

C++ 这个数组的静态分配和动态分配有什么区别?

c++ - Windows::Storage::ApplicationData::Current 在 C++ 中找不到

c++ - 不理解 sscanf_s 行为

javascript - 向下滚动 50px 后如何让导航栏出现并锁定?

c++ - 将 MFC Cimage 初始化为纯色

c++ - 在 Qt 中渲染巨大的图像

android - 垂直 Android TabLayout 不能垂直滚动

javascript - 使用javascript滚动到元素时添加平滑效果

c++ - MFC中如何在非矩形窗口中绘制边框

c++ - OpenThemeData 函数有哪些可能的类?