我正在尝试在 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/