滾動條的處理

平時在處理滾動條程序時,總覺得代碼很長,敲得手都累了,今天在試了試,也還行。

總結起來大致有三步:

  1. 設置滾動條的範圍之類的基本信息,如可以再WM_SIZE消息中處理
  2. 響應滾動條消息並設置變化中的滾動條
  3. 更新界面變化

 看個實例代碼,當然,代碼還是很長,可以想辦法幫封裝一下:

case WM_SIZE:
  cx = LOWORD(lp);
  cy = HIWORD(lp);

  si.cbSize = sizeof(SCROLLINFO);
  si.fMask = SIF_RANGE | SIF_PAGE;
  si.nMin = 0;
  si.nMax = NUMLINES - 1;
  si.nPage = cy / cyChar;
  SetScrollInfo( hwnd, SB_VERT, &si, TRUE );

  si.fMask = SIF_RANGE | SIF_PAGE;
  si.nMin = 0;
  si.nMax = 2 + iMaxWidth / cxChar;
  si.nPage = cx / cxChar;
  SetScrollInfo( hwnd, SB_HORZ, &si, TRUE );
  return 0;

 case WM_VSCROLL:
  si.cbSize = sizeof(si);
  si.fMask = SIF_ALL;
  GetScrollInfo( hwnd, SB_VERT, &si );
  iVertPos = si.nPos;
  switch(LOWORD(wp))
  {
  case SB_TOP:
   si.nPos = si.nMin;
   break;

  case SB_BOTTOM:
   si.nPos = si.nMax;
   break;

  case SB_LINEUP:
   si.nPos -= 1;
   break;
   
  case SB_LINEDOWN:
   si.nPos += 1;
   break;

  case SB_PAGEUP:
   si.nPos -= si.nPage;
   break;

  case SB_PAGEDOWN:
   si.nPos += si.nPage;
   break;
   
  case SB_THUMBTRACK:
   si.nPos = si.nTrackPos;
   break;

  default:
   break;
  }
  si.fMask = SIF_POS;
  SetScrollInfo( hwnd, SB_VERT, &si, TRUE );
  GetScrollInfo( hwnd, SB_VERT, &si);

  if ( si.nPos != iVertPos )
  {
   ScrollWindow( hwnd, 0, cxChar * (iVertPos - si.nPos), NULL, NULL );
   UpdateWindow( hwnd );
  }

  return 0;

 case WM_HSCROLL:
  si.cbSize = sizeof(si);
  si.fMask = SIF_ALL;
  GetScrollInfo( hwnd, SB_HORZ, &si );
  iHorPos = si.nPos;
  switch(LOWORD(wp))
  {
  case SB_LINELEFT:
   si.nPos -= 1;
   break;

  case SB_LINERIGHT:
   si.nPos += 1;
   break;

  case SB_PAGELEFT:
   si.nPos -= si.nPage;
   break;

  case SB_PAGERIGHT:
   si.nPos += si.nPage;
   break;

  case SB_THUMBPOSITION:
   si.nPos = si.nTrackPos;
   break;

  default:
   break;
  }
  si.fMask = SIF_POS;
  SetScrollInfo( hwnd, SB_HORZ, &si, TRUE );
  GetScrollInfo( hwnd, SB_HORZ, &si);

  if ( si.nPos != iHorPos )
  {
   ScrollWindow( hwnd, 0, cxChar * (iHorPos - si.nPos), NULL, NULL );
   UpdateWindow( hwnd );
  }

  return 0;


發佈了29 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章