2012-10-19 28 views
1

我是新來的MFC滾動視圖,我在我的ScrollView類的函數onPaint中顯示大圖像。這裏是在初始更新功能,下面的代碼MFC CScrollView滾動不起作用

void CCenterImageView::OnInitialUpdate() 
{ 
    CScrollView::OnInitialUpdate(); 

    // TODO: Add your specialized code here and/or call the base class 

    CSize sizeTotal; 
    // TODO: calculate the total size of this view 
    sizeTotal.cx = m_matImage.cols; 
    sizeTotal.cy = m_matImage.rows; 
    SetScrollSizes(MM_TEXT, sizeTotal,sizeTotal); 

} 

void CCenterImageView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{ 
    // TODO: Add your message handler code here and/or call default 

    CScrollView::OnVScroll(nSBCode, nPos, pScrollBar); 

    //Invalidate(); 
} 

void CCenterImageView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{ 
    // TODO: Add your message handler code here and/or call default 

    CScrollView::OnHScroll(nSBCode, nPos, pScrollBar); 
    UpdateData(); 
    //Invalidate(); 
} 

void CCenterImageView::OnPaint() 
{ 
    CPaintDC dc(this); // device context for painting 
    // TODO: Add your message handler code here 
    // Do not call CView::OnPaint() for painting messages 

    if(m_CVvImageObj.GetImage()) 
    {  
     //this function blit the bitmap on the screen height and width are more than 1000 pixels 
     m_CVvImageObj.Show(dc.GetSafeHdc(),0,0,m_CVvImageObj.Width(),m_CVvImageObj.Height()); 
    } 


} 

使用該http://www.functionx.com/visualc/views/scrollview.htm我可以看到垂直和水平滾動,但按他們不改變對圖什麼。請引導我如何在用戶點擊時移動滾動並更改視圖?

回答

1

CScrollView的行爲就好像您正在使用SetScrollSizes確定的大型畫布上繪畫一樣。您設置的大小通常是整個圖像的大小,很可能大於顯示的大小。

然後,當您處理OnDraw時,您可以像完成一樣繪製整個圖像,也可以只繪製顯示的部分,您可以從GetClipBox中找到該部分。後者僅對於效率是必需的,因爲CScrollView將剪切不可見的部分。

根本不需要OnXScroll處理程序。你應該刪除它們,讓CScrollView處理滾動,如果這是你做的所有響應滾動。

如果圖片尺寸> = 2^15,將會出現問題,但聽起來不像您的情況。

0

您的代碼看起來很可疑(第3個參數)。但是,如果它涉及到32K以上的滾動問題,請參閱知識庫文章'PRB:CScrollView Scroll Range Limited to 32K'(文章ID:166473)。