0

这段代码不是做水平滚动而是做垂直滚动,请给我任何建议我做错了什么。我想做水平滚动。

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

int nDelta;
int nMaxPos = m_rcOriginalRect.Width() - m_nCurWidth;

switch (nSBCode)
{
case SB_LINERIGHT:
    if (m_nScrollPos >= nMaxPos)
        return;

    nDelta = min(max(nMaxPos/20,5),nMaxPos-m_nScrollPos);
    break;

case SB_LINELEFT:
    if (m_nScrollPos <= 0)
        return;
    nDelta = -min(max(nMaxPos/20,5),m_nScrollPos);
    break;
case SB_PAGERIGHT:
    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_PAGELEFT:
    if (m_nScrollPos <= 0)
        return;
    nDelta = -min(max(nMaxPos/10,5),m_nScrollPos);
    break;

     default:
    return;
}
m_nScrollPos += nDelta;
SetScrollPos(SB_HORZ,m_nScrollPos,TRUE);
ScrollWindow(0,-nDelta);

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

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

// TODO: Add your message handler code here
m_nCurWidth = cx;

SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL; 
si.nMin = 0;
si.nMax = m_rcOriginalRect.Width();
si.nPage = cx;
si.nPos = 0;
SetScrollInfo(SB_HORZ, &si, TRUE); 
}
4

1 回答 1

0

我希望这对你有用:

BEGIN_MESSAGE_MAP(AddTabDlg, CDialog)
	ON_WM_SIZE()
	ON_WM_VSCROLL()
	ON_WM_HSCROLL()

END_MESSAGE_MAP()


// AddTabDlg message handlers

BOOL AddTabDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	GetWindowRect(m_rect);
	m_nScrollPos = 0;
	m_nScrollPosH = 0;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

	// TODO: Add your message handler code here.
	m_nCurHeight = cy;

	m_nCurWidth = cx;

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

	int nScrollMaxH;
	if (cx < m_rect.Width())
	{
		nScrollMaxH = m_rect.Width() - cx;
	}
	else
		nScrollMaxH = 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);

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

void AddTabDlg::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);
}

void AddTabDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default.
	int nDeltaH;
	int nMaxPosH = m_rect.Width() - m_nCurWidth;

	switch (nSBCode)
	{
	case SB_LINERIGHT:
		if (m_nScrollPosH >= nMaxPosH)
			return;
		nDeltaH = min(nMaxPosH / 100, nMaxPosH - m_nScrollPosH);
		break;

	case SB_LINELEFT:
		if (m_nScrollPosH <= 0)
			return;
		nDeltaH = -min(nMaxPosH / 100, m_nScrollPosH);
		break;

	case SB_RIGHT:
		if (m_nScrollPosH >= nMaxPosH)
			return;
		nDeltaH = min(nMaxPosH / 10, nMaxPosH - m_nScrollPosH);
		break;

	case SB_THUMBPOSITION:
		nDeltaH = (int)nPos - m_nScrollPosH;
		break;

	case SB_LEFT:
		if (m_nScrollPosH <= 0)
			return;
		nDeltaH = -min(nMaxPosH / 10, m_nScrollPosH);
		break;

	default:
		return;
	}
	m_nScrollPosH += nDeltaH;
	SetScrollPos(SB_HORZ, m_nScrollPosH, TRUE);
	ScrollWindow(-nDeltaH, 0);
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

它同时工作,水平和垂直滚动条;)

于 2014-12-12T00:44:41.907 回答