2013-05-11 67 views
0

基於mfc對話框,CDispView從CScrollView驅動。左鍵點擊時需要放大點,右鍵點擊時放大點。以下部分工作。任何方式使它工作更好?相應地調整滾動條的大小,放大點等。如何放大和縮小CScrollView表面

xzfac = 1; 
yzfac = 1; 

void CDispView::OnInitialUpdate() 
{ 
    SetScrollSizes(MM_TEXT, CSize(cWidth, cHeight)); 
    CScrollView::OnInitialUpdate(); 
} 

void CDispView::OnDraw(CDC* pDC) 
{ 
StretchDIBits(pDC->GetSafeHdc(), 0, 0, 
(xzfac * pBmpInfo->bmiHeader.biWidth), 
(yzfac * pBmpInfo->bmiHeader.biHeight), 
0, 0, pBmpInfo->bmiHeader.biWidth, 
pBmpInfo->bmiHeader.biHeight, 
imageBuf, pBmpInfo, DIB_RGB_COLORS, 
SRCCOPY); 
} 

void CDispView::refresh() 
{ 
    OnInitialUpdate(); 

} 

void CDispView::OnLButtonDown(UINT nFlags, CPoint point) 
{ 
    yzfac = yzfac + 1; 
    xzfac = xzfac + 1; 

    refresh(); 
    RedrawWindow(); 

    CScrollView::OnLButtonDown(nFlags, point); 
} 

void CDispView::OnRButtonDown(UINT nFlags, CPoint point) 
{ 
    yzfac = yzfac - 1; 
    if (yzfac < 1) yzfac = 1; 
    xzfac = xzfac - 1; 
    if (xzfac < 1) xzfac = 1; 

    refresh(); 
    RedrawWindow(); 

    CScrollView::OnRButtonDown(nFlags, point); 
} 

回答

-1

您可以重寫CView :: OnPrepareDC方法。它在OnDraw之前被調用,並且是將CDC調整爲不同比例因子和偏移以提供縮放效果的地方。例如,打印時使用此功能。通過改變CDC的尺寸,它可以使OnDraw在屏幕顯示和打印方面都相同。

+0

它會在這種情況下工作,CDispView從CScrollView驅動?我試試。任何例子? – user2045525 2013-05-11 05:58:48

0

基於mfc對話框:使用此代碼,它放大圖像的右下部分,無論在哪裏點擊放大。CDispView都是從CScrollView派生而來的。

int sWidth = imgWidth; 
int sHeight = imgHeight; 
int PtX = 0; 
int PtY = 0; 
int cHeight; //client 
int cWidth; //client 
int vWidth = imgWidth; 
int vHeight = imgHeight; 

void CDispView::OnInitialUpdate() 
{ 
    SetScrollSizes(MM_TEXT, CSize(cWidth, cHeight)); 
    CScrollView::OnInitialUpdate(); 
} 

void CDispView::OnDraw(CDC* pDC) 
{ 
StretchDIBits( pDC->GetSafeHdc(), 
0, 0, 
cWidth, 
cHeight, 
0, 0, 
vWidth, 
vHeight, 
imgBuffer, 
pBmpInfo, 
IB_RGB_COLORS, 
SRCCOPY); 
} 

void CDispView::InitBitmapInfo() 
{ 
    pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
    pBmpInfo->bmiHeader.biWidth = vWidth; 
    pBmpInfo->bmiHeader.biHeight = vHeight; 
    ..etc.. 
} 

void CDispView::refresh() 
{ 
    OnInitialUpdate(); 

} 

void CDispView::OnLButtonDown(UINT nFlags, CPoint pt) 
{ 
    long x, y; 
    x= PtX + (pt.x/cWidth * vWidth); 
    y= PtY + (pt.y/cHeight * vHeight); 
    vWidth = (int) (vWidth/2); 
    vHeight = (int) (vHeight/2); 
    PtX= x - (pt.x/cWidth * vWidth); 
    PtY= y - (pt.y/cHeight * vHeight); 

    if (PtX < 0) 
     {PtX= 0;} 
    if (PtY < 0) 
     {PtY= 0;} 

    long temp = sWidth - vWidth; 
    if (PtX > temp) 
    { 
     PtX = temp; 
    } 
    temp= sHeight - vHeight; 
    if (PtY > temp) 
    { 
     PtY = temp; 
    } 
    if (vWidth < 50) 
    { 
     vWidth = sWidth; 
     vHeight = sHeight; 
     PtX = 0; 
     vPt = 0; 
    } 
    refresh(); 
    Invalidate(0); 
    CScrollView::OnLButtonDown(nFlags, pt); 
} 

void CDispView::OnRButtonDown(UINT nFlags, CPoint pt) 
{ 
    PtX = 0; 
    PtY = 0;  
    vWidth = imgWidth; 
    vHeight = imgHeight; 
    refresh(); 
    Invalidate(0); 
    CScrollView::OnRButtonDown(nFlags, pt); 
}