2014-01-13 46 views
-1

我想使用滚动条滚动图像,但是当我在OnHScroll方法中使用scrollwindow()函数时,它仅滚动出现在对话框中的按钮而不是图像。 我已经使用bitblt和stretchblt函数来放大图像使用设备上下文。我认为通过使用直流信息我们可以滚动图像,但我不知道我这样做。水平滚动图像

OnHScroll函数的代码如下给出:

void CImgVeiwer::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pcrollBar) 
{ 
// TODO: Add your message handler code here and/or call default 
    int minpos; 
    int maxpos; 

    GetScrollRange(SB_HORZ, &minpos, &maxpos); 
    maxpos = GetScrollLimit(SB_HORZ); 
    CurPos = GetScrollPos(SB_HORZ); 
switch (nSBCode) 
{ 
case SB_LEFT:  // Scroll to far left. 
    CurPos = minpos; 
    break; 

    case SB_RIGHT:  // Scroll to far right. 
    CurPos = maxpos; 
    break; 

    case SB_ENDSCROLL: // End scroll. 
    break; 

    case SB_LINELEFT:  // Scroll left. 
    if (CurPos > minpos) 
    CurPos--; 
    break; 

    case SB_LINERIGHT: // Scroll right. 
    if (CurPos < maxpos) 
    CurPos++; 
    break; 

    case SB_PAGELEFT: // Scroll one page left. 
    { 
    // Get the page size. 
    SCROLLINFO info; 
    GetScrollInfo(SB_HORZ, &info, SIF_ALL); 

    if (CurPos > minpos) 
     CurPos = max(minpos, CurPos - (int) info.nPage); 
    } 
    break; 

    case SB_PAGERIGHT:  // Scroll one page right. 
    { 
    // Get the page size. 
    SCROLLINFO info; 
    GetScrollInfo(SB_HORZ, &info, SIF_ALL); 

    if (CurPos < maxpos) 
     CurPos = min(maxpos, CurPos + (int) info.nPage); 
} 
    break; 
    case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position 
     CurPos = nPos;  // of the scroll box at the end of the drag operation. 
     break; 

    case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is the 
     CurPos = nPos;  // position that the scroll box has been dragged to. 
    break; 
    } 

// Set the new position of the thumb (scroll box). 
m_HsrollFlag = TRUE; 
SetScrollPos(SB_HORZ,CurPos); 
ScrollWindow(-CurPos,0,0,0); 
Invalidate(); 
CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar); 

}

在此先感谢

回答

0

通过调用Invalidate(),你会重新绘制整个对话框。所以ScrollWindow调用被浪费了:你在后续的WM_PAINT中重画它。通常你会使用ScrollWindow滚动图像的可见部分,然后在WM_PAINT中,只需要绘制图像滚动留下的未覆盖边。

你应该为ScrollWindow提供一个lpRect参数,也可能是lpClipRect。这将指定您想要滚动的区域,并且如果它们在图像之外,也可以防止像按钮一样滚动子窗口。

+0

在传递lpRect,lpClipRect参数时,它将滚动图像以及出现在对话框中的按钮。 – user3148898

+0

如何在OnPaint mehtod – user3148898

+0

中重新绘制图像需要更多细节。图像如何显示? (它是在单独的控件中,还是绘制在对话框背景中,或者绘制在OnPaint中?)图像顶部的按钮是? –

1

最后,我得到了水平滚动图像的解决方案。 在上面给出的代码中,删除语句 ScrollWindow(-CurPos,0,0,0);

并在OnPaint()方法中添加以下语句,m_nWidth和nHeight是要滚动的图像的宽度和高度。

dc.StretchBlt(ZERO - CurPos,FIFTY ,m_nWidth +1000, 
      nHeight+ 1000, 
      &memDC,ZERO,ZERO,m_nWidth, 
      m_nHeight,SRCCOPY);