0

I'm trying to display an image in CScrollView-derived class:

C++ CScrollView, how to scroll an image?

So I want to override OnDraw, to move the code from OnPaint to OnDraw. But I can't. Every time I call Invalidate() only OnPaint is getting called.

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));
}
Community
  • 1
  • 1
Nika_Rika
  • 613
  • 2
  • 6
  • 29
  • Don't be afraid to read the documentation ([Drawing in a View](https://msdn.microsoft.com/en-us/library/ssdhfz2t.aspx)). – IInspectable Oct 22 '16 at 16:33

1 Answers1

1

Of course it doesn't call OnDraw(). When you call Invalidate() it ends up with a WM_PAINT message for the CView derived class. The default implementation of CView::OnPaint() gets a paint DC and then it later calls CView::OnDraw(). You are overriding OnPaint() and you never call OnDraw() in your OnPaint() handler.

You can move some of your OnPaint() code into OnDraw() except for obvious stuff like CPaintDC dc(this);

After that you can delete your OnPaint() declaration and implementation. Then, delete your ON_WM_PAINT() message map entry. I can't vouch either way for your drawing code.

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29