对话框子窗口的滚动

父类
void CDlgXXXX::InitUIRes(CString strCatCode)
{
	if (NULL == m_pDlgSub_)
	{
		m_pDlgSub_ = new CDlgFilterSub(this);
		m_pDlgSub_->Create(CDlgFilterSub::IDD, this);
	}
	
	m_pDlgSub_->InitUI(strCatCode);

	CRect rtClient;
	GetWindowRect(&rtClient);

	CRect rtDlg;
	m_pDlgSub_->GetClientRect(&rtDlg);
	rtDlg.MoveToY(40);
	rtDlg.left = 0;
	rtDlg.right = rtClient.Width();
	m_pDlgSub_->MoveWindow(rtDlg);

	m_pDlgSub_->GetWindowRect(&rtDlg);
	rtClient.bottom = rtDlg.bottom + 10;
	MoveWindow(rtClient);

	if (!m_pDlgSub_->IsWindowVisible())
	{
		m_pDlgSub_->ShowWindow(SW_SHOW);
	}
}

  子窗口

void CDlgFilterSub::InitUI(CString strCatCode)
{
	。。。。动态添加按钮

CRect rtClient;
GetClientRect(rtClient);

rtCtrl最后一排控件的位置

SCROLLINFO si;
si.cbSize = sizeof(si);
GetScrollInfo(SB_VERT, &si);
si.nMax = max(rtCtrl.bottom + SPACE, rtClient.bottom);
si.nPos = 0;
SetScrollInfo(SB_VERT, &si);

static int iHeight = rtClient.bottom;
if (rtClient.bottom < rtCtrl.bottom + SPACE)
{
rtClient.bottom = iHeight;
}
else
{
rtClient.bottom = rtCtrl.bottom + SPACE;
}
MoveWindow(rtClient);

}

  子窗口的初始化

BOOL CDlgFilterSub::OnInitDialog()
{
	CBCGPDialog::OnInitDialog();
	COLORREF bgColor = RGB(248, 248, 248);
	SetBackgroundColor(bgColor);

	CRect rtClient;
	GetClientRect(rtClient);
	SCROLLINFO si;
	si.cbSize = sizeof(SCROLLINFO);
	si.fMask = SIF_ALL;
	si.nPos = 0;
	si.nMin = 0;
	si.nMax = rtClient.bottom;
	SetScrollInfo(SB_VERT, &si);

	return TRUE;
}

  

void CDlgFilterSub::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	SCROLLINFO si;
	si.cbSize = sizeof(si);
	si.fMask = SIF_ALL;
	GetScrollInfo(SB_VERT, &si);
	INT nVscroll = si.nPos;
	switch(nSBCode)
	{  
	case SB_LINEDOWN:
		{
			nVscroll += 20;
			if (nVscroll > (si.nMax - si.nMin - si.nPage ))
			{
				nVscroll = si.nMax - si.nMin - si.nPage;
			}
		}
		break;
	case SB_LINEUP:
		{
			nVscroll -= 20;
			if (nVscroll < si.nMin)
			{
				nVscroll = 0;
			}
		}
		break;
	case SB_PAGEDOWN:
		{
			nVscroll += si.nPage;
			if (nVscroll > (si.nMax - si.nMin - si.nPage))
			{
				nVscroll = si.nMax - si.nMin - si.nPage;
			}
		}
		break;
	case SB_PAGEUP:
		{
			nVscroll -= si.nPage;
			if (nVscroll < si.nMin)
			{
				nVscroll = 0;  
			}
		}
		break;
	case SB_THUMBTRACK:
		{
			nVscroll = nPos;
		}
		break;
	}

	if (si.nPos != nVscroll)
	{
		int yAmount = -(nVscroll - si.nPos);
		ScrollWindow(0, yAmount, NULL,NULL);
	}

	si.nPos = nVscroll;
	SetScrollInfo(SB_VERT, &si);

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

  

ON_WM_VSCROLL()

  

afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

  

原文地址:https://www.cnblogs.com/XiHua/p/13914988.html