vc怎样为窗口添加滚动条

 

Selecting the "Horizontal Scroll" and "Verticla Scroll" styles among the
properties of your dialog box in the resource editor, you can add  scroll
bars
to the dialog box. Remember also to select the 'resizing' border style.

However for adding functionality to the scroll bars, you need to override
the
WM_VSCROLL and WM_HSCROLL message handlers. Also, override the WM_SIZE
handler
to set the scroll bar range if the size is reduced than the original. So you
get the original size of the dialog in your OninitDialog(). The code would
look something like this. Modify to your needs.

1. To OnInitDialog(), add the following line.

          GetWindowRect(m_rect);
           m_nScrollPos = 0;

    to get the original window size. Make m_rect a member variable of your
dialog. Add another variable m_nScrollPos and initialize its value to zero.
It
stores the current vertical scroll position.
CRect m_rect;
        int m_nScrollPos;

2. Here is the WM_SIZE handler for setting the scroll bar range. Set range 0
if
size is increased more than original.

  void CCharlesDlg::OnSize(UINT nType, int cx, int cy)
{
 CDialog::OnSize(nType, cx, cy);

 // TODO: Add your message handler code here

 m_nCurHeight = cy;
 int nScrollMax;
 if (cy < m_rect.Height())
 {
  nScrollMax = m_rect.Height() - cy;
 }
 else
  nScrollMax = 0;

 SCROLLINFO si;

 si.cbSize = sizeof(SCROLLINFO);
 si.fMask = SIF_ALL; // SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS;
 si.nMin = 0;
 si.nMax = nScrollMax;
 si.nPage = si.nMax/10;
 si.nPos = 0;
 SetScrollInfo(SB_VERT, &si, TRUE);
 
}

You need m_nCurHeight to store the current height of the dialog and use it
to
handle the scrolling in OnVScroll. m_ncurHeight is also a member variable of
the dialog.
3. Here is the handler for WM_VSCROLL.

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

 int nDelta;
 int nMaxPos = m_rect.Height() - m_nCurHeight;

 switch (nSBCode)
 {
 case SB_LINEDOWN:

  if (m_nScrollPos >= nMaxPos)
   return;

  nDelta = min(nMaxPos/100,nMaxPos-m_nScrollPos);

  break;

 case SB_LINEUP:
  if (m_nScrollPos <= 0)
   return;

  nDelta = -min(nMaxPos/100,m_nScrollPos);
  break;

 case SB_PAGEDOWN:
  if (m_nScrollPos >= nMaxPos)
   return;

  nDelta = min(nMaxPos/10,nMaxPos-m_nScrollPos);
  break;

 case SB_THUMBPOSITION:
  nDelta = (int)nPos - m_nScrollPos;
  break;

 case SB_PAGEUP:
  if (m_nScrollPos <= 0)
   return;

  nDelta = -min(nMaxPos/10,m_nScrollPos);
  break;

 default:

  return;
 }

 m_nScrollPos += nDelta;
 SetScrollPos(SB_VERT,m_nScrollPos,TRUE);

 ScrollWindow(0,-nDelta);

 CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
 
}

The above code handles the vertical scrolling. For horizontal scrolling add
the
WM_HSCROLL similarly and add the necessary code to OnSize and OnInitDialog.
Information provided in this document and any software that may
accompany this document is provided "as is" without warranty of any
kind, either expressed or implied, including but not limited to the
implied warranties of merchantability and/or fitness for a particular
purpose. The user assumes the entire risk as to the accuracy and the
use of this information.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值