Re: How to add a Dialog in a View
"97612" <97612@discussions.microsoft.com> wrote in message
news:D32E251E-1861-4ADC-B7CF-B206EF129FCB@microsoft.com...
Below is the code that I modified. But it doesn't show the preview of a
image
in the View(CPreView), and the program will crash when exit.
//********* code *************
void CPreView::OnDraw(CDC* pDC)
{
CMyDoc *pDoc =
(CMyDoc*)((CMainFrame*)AfxGetMainWnd())->GetActiveDocument();
LPRECT rect = NULL;
GetClientRect(rect);
There is a very basic C++ error here: You have not created a RECT, only a
pointer to a RECT. So you are passing an invalid pointer to GetClientRect.
In MFC the proper way to do this is:
CRect rect;
GetClientRect(&rect);
if( pDoc->m_pSelectedImage != NULL )
{
Graphics graphics( pDC->m_hDC );
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
graphics.DrawImage( pDoc->m_pSelectedImage,
Rect( rect->left,
rect->top,
rect->right - rect->left,
rect->bottom - rect->top));
}
}
Again, rect is invalid for all these until you make the fix above.
Another question:
I use Invalidate() in the OnUpdate() ( I've overwritten OnUpdate() ) of
the
View(CPreView) that show the preview of a image, or OnDraw() won't be
triggered. Is that OK?
That's the right way to do it. :_)
--
Scott McPhillips [VC++ MVP]