MFC动态添加滚动条
当我们再使用软件的时候,当前界面不够显示所有的界面信息时候,要自动在左侧添加左侧滚动条好显示所有的界面信息。
定义
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
//在资源视图中看到的对话框的大小
CRect m_rectOriginal;
// 滚动条实际位置
int m_nScrollPos;
// 对话框实际高度
int m_nCurrentHeight;
事件
ON_WM_VSCROLL()
ON_WM_SIZE()
ON_WM_MOUSEWHEEL()
实现函数
BOOL CPageFileSendOutCfg::OnInitDialog()
{
CPageBase::OnInitDialog();
{
// 给对话框添加滚动条 SB_VERT:垂直方向,SB_HORZ:水平方向
//存储对话框初始大小
GetWindowRect(m_rectOriginal);
//初始滚动条位置
m_nScrollPos = 0;
}
}
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void xxxxxxxCfg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
int nDelta;
int nMaxPos = m_rectOriginal.Height() - m_nCurrentHeight;
switch (nSBCode)
{
case SB_LINEDOWN:
if (m_nScrollPos >= nMaxPos)
return;
nDelta = min(max(nMaxPos / 20, 5), nMaxPos - m_nScrollPos);
break;
case SB_LINEUP:
if (m_nScrollPos <= 0)
return;
nDelta = -min(max(nMaxPos / 20, 5), m_nScrollPos);
break;
case SB_PAGEDOWN:
if (m_nScrollPos >= nMaxPos)
return;
nDelta = min(max(nMaxPos / 10, 5), nMaxPos - m_nScrollPos);
break;
case SB_THUMBTRACK:
case SB_THUMBPOSITION:
nDelta = (int)nPos - m_nScrollPos;
break;
case SB_PAGEUP:
if (m_nScrollPos <= 0)
return;
nDelta = -min(max(nMaxPos / 10, 5), m_nScrollPos);
break;
default:
return;
}
m_nScrollPos += nDelta;
SetScrollPos(SB_VERT, m_nScrollPos, TRUE);
ScrollWindow(0, -nDelta);
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
void xxxxxxxCfg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
m_nCurrentHeight = cy;
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
si.nMin = 0;
si.nMax = m_rectOriginal.Height()*1.2;
si.nPage = cy;
si.nPos = 0;
SetScrollInfo(SB_VERT, &si, TRUE);
}
BOOL xxxxxxxCfg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
int nMaxPos = m_rectOriginal.Height() - m_nCurrentHeight;
if (zDelta < 0)
{
if (m_nScrollPos < nMaxPos)
{
zDelta = min(max(nMaxPos / 20, 5), nMaxPos - m_nScrollPos);
m_nScrollPos += zDelta;
SetScrollPos(SB_VERT, m_nScrollPos, TRUE);
ScrollWindow(0, -zDelta);
}
}
else
{
if (m_nScrollPos > 0)
{
zDelta = -min(max(nMaxPos / 20, 5), m_nScrollPos);
m_nScrollPos += zDelta;
SetScrollPos(SB_VERT, m_nScrollPos, TRUE);
ScrollWindow(0, -zDelta);
}
}
return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}