Daily Archives: November 14, 2006

Usage of CScrollView

Generally speaking, we only need to rewrite 2 functions:
1. OnInitialUpdate()
void CSrcView::OnInitialUpdate()
{
 CScrollView::OnInitialUpdate();
 
 CPhotograph2Doc* pDoc = GetDocument();
 CSize sizeTotal;
 // TODO: calculate the total size of this view
 sizeTotal.cx = pDoc->pSrcImg->Width();
 sizeTotal.cy = pDoc->pSrcImg->Height();
 SetScrollSizes(MM_TEXT, sizeTotal);
}
 
2. OnDraw()
// some drawing
 
SetScrollSizes(MM_TEXT, CSize(pDoc->pSrcImg->Width(), pDoc->pSrcImg->Height()));
   
 CChildFrame *pParentFrame = (CChildFrame *)this->GetParentFrame();
    pParentFrame->RecalcLayout();
//    ResizeParentToFit();  
 
Probably, you may want to override the function OnEraseBkgnd();
BOOL CSrcView::OnEraseBkgnd(CDC* pDC)
{
 //   Set   brush   to   desired   background   color            
 CBrush   backBrush(RGB(255,   255,   255));                
 //   Save   old   brush       
 CBrush*   pOldBrush   =   pDC->SelectObject(&backBrush);  
            
 CRect   rect;               
 pDC->GetClipBox(&rect);           //   Erase   the   area   needed                  
 pDC->PatBlt(rect.left,   rect.top,   rect.Width(),   rect.Height(),                     
  PATCOPY);               
 pDC->SelectObject(pOldBrush);  
             
 return   TRUE;  
 //return CScrollView::OnEraseBkgnd(pDC);
}
 
That’s all.