滚动条缩略图跟踪长度 基于图像的水平适合调整窗口大小时出现问题

scroll bar ThumbTrack length Issue when resizing window Based on Horizontal fit for Image

本文关键字:调整 水平 窗口 问题 小时 跟踪 略图 图像 于图像 滚动条      更新时间:2023-10-16
    int ActualImageWidth = 1000;  
    int ActualImageHeight = 1200;  
    int WindowWidth = 800;  
    int WindowHeight = 500;  
    float resizedPercent;  

为了达到水平最佳拟合,我想在不更改实际图像纵横比的情况下将实际图像宽度减小或增加为窗口宽度。并且我还应该根据新的调整大小的图像(窗口调整大小)保持滚动条拇指轨道长度。为此,我进行了以下计算

 switch(FitType)  
     {  
      case FITHORIZONTAL:  
          //find out whether resize percentage is decrease or increase  
          if(ActualImageWidth > WindowWidth) //resize decreasing  
          {  
              //find out the pecentage of decreased value  
              resizedPercent = WindowWidth/ActualImageWidth;  
          } else //resize increasing  
          {  
              //find out the pecentage of increased value  
              resizedPercent = ActualImageWidth/WindowWidth;  
          }  
          ResizedWidth = iWndwidth;  
          ResizedHeight = ActualImageHeight * resizedPercent;  
          break;  
     }  

为了设置滚动条,wm_size事件我使用以下代码

LRESULT CALLBACK my_wnd_proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)  
      {  
          static SCROLLINFO si;  
          static RECT rect;  
          int WndWidth = 0;  
          int WndHeight = 0;  
          case WM_CREATE :  
              {  
                  //getting image data from here.  
                  return 0;  
              }  
          case WM_SIZE :  
              {  
                  GetWindowRect(hWnd, &rect);  
                  WndWidth = rect.right - rect.left;  
                  WndHeight = rect.bottom - rect.top;  
                  GetScrollInfo(hWnd, SB_VERT, &si);  
                  int yMaxScroll = max((int)ResizedHeight - WndHeight, 0);  
                  int yCurrentScroll = min(si.nPos, yMaxScroll);  
                  si.cbSize = sizeof(si);  
                  si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;  
                  si.nMin  = 0;  
                  si.nMax  = ResizedHeight;  
                  si.nPage = WndHeight;  
                  si.nPos  = 0;  
                  SetScrollInfo(hWnd, SB_VERT, &si, TRUE);  
                  InvalidateRect(hWnd, &rect, true);  
                  return 0;  
              }  
          case WM_PAINT :  
              {  
                  //try to draw image here..  
                  return 0;  
              }          
      }  

我的问题是我无法获得确切的滚动条拇指轨道,因此我无法滚动图像直到图像末尾。请用样本建议正确的逻辑

注意:如果您以@o_weisman开头的评论,我将收到针对我的评论通知。在我的代码中,我也像这样覆盖 OnVScroll,它对我有用(您需要调整它以处理窗口的滚动消息:

void CMagDialog::OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar )
{
int nDelta;
int nMaxPos = yMaxScroll;
if(pScrollBar == NULL)  
{
    switch (nSBCode)
    {
    case SB_LINEDOWN:
        if (m_vScrollPos >= nMaxPos)
            return;
        nDelta = min(nMaxPos/100,nMaxPos-m_vScrollPos);
        break;
    case SB_LINEUP:
        if (m_vScrollPos <= 0)
            return;
        nDelta = -min(nMaxPos/100,m_vScrollPos);
        break;
    case SB_PAGEDOWN:
        if (m_vScrollPos >= nMaxPos)
            return;
        nDelta = min(nMaxPos/5,nMaxPos-m_vScrollPos);
        break;
    case SB_THUMBPOSITION:
        nDelta = (int)nPos - m_vScrollPos;
        break;
    case SB_PAGEUP:
        if (m_vScrollPos <= 0)
            return;
        nDelta = -min(nMaxPos/5, m_vScrollPos);
        break;
    default:
        return;
    }
    m_vScrollPos += nDelta;
    SetScrollPos(SB_VERT,m_vScrollPos,TRUE);
    ScrollWindow(0,-nDelta);
}

}