水平滚动图像

时间:2014-01-13 04:24:07

标签: mfc

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

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);

}

提前致谢

2 个答案:

答案 0 :(得分:1)

最后,我得到了水平滚动图像的解决方案。 在上面给出的代码中,删除该语句                滚屏(-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);

答案 1 :(得分:0)

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

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

相关问题