Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / MFC

ControlObjectList

Rate me:
Please Sign up or sign in to vote.
3.00/5 (6 votes)
16 Aug 2002CPOL 62.6K   2.1K   25  
Add/Remove/Scroll/Sort Object List (CButton, CStatic, etc..)
// ScrollDialog.cpp : implementation file
//

#include "stdafx.h"
#include "ScrollDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// The following definitions specify how many points 
// are scrolled at each press of the scrollbar arrows
#define HORZ_PTS 8
#define VERT_PTS 4


/////////////////////////////////////////////////////////////////////////////
// CScrollDialog dialog

CScrollDialog::CScrollDialog(UINT nIDD, CWnd* pParent /*=NULL*/)
	: CDialog(nIDD, pParent)
{
	//{{AFX_DATA_INIT(CScrollDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CScrollDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CScrollDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CScrollDialog, CDialog)
	//{{AFX_MSG_MAP(CScrollDialog)
	ON_WM_VSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScrollDialog message handlers

BOOL CScrollDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();


    // Get the initial dimensions of the dialog
    GetClientRect(&m_ClientRect);

    // Set Initial Scroll Positions
    m_nVscrollPos = 0;
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CScrollDialog::ResetScrollbars()
{
    // Reset our window scrolling information
    ScrollWindow(0, m_nVscrollPos*VERT_PTS, NULL, NULL);
    m_nVscrollPos = 0;
    SetScrollPos(SB_VERT, m_nVscrollPos, TRUE);
}


void CScrollDialog::SetupScrollbars()
{
    CRect tempRect;
    GetClientRect(&tempRect);

    m_nVertInc = (m_ClientRect.Height() - tempRect.Height())/VERT_PTS;

    m_nVscrollMax = max(0, m_nVertInc);
    m_nVscrollPos = min(m_nVscrollPos, m_nVscrollMax);
    SetScrollRange(SB_VERT, 0, m_nVscrollMax, FALSE);
    SetScrollPos(SB_VERT, m_nVscrollPos, TRUE);

}

void CScrollDialog::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
    // Handle vertical scrollbar messages
    // These can be tweaked to better fit the implementation
    int nInc;
    switch (nSBCode)
    {
        case SB_TOP:        nInc = -m_nVscrollPos;               break;
        case SB_BOTTOM:     nInc = m_nVscrollMax-m_nVscrollPos;  break;
        case SB_LINEUP:     nInc = -1;                           break;
        case SB_LINEDOWN:   nInc = 1;                            break;
        case SB_PAGEUP:     nInc = min(-1, -m_nVertInc);         break;
        case SB_PAGEDOWN:   nInc = max(1, m_nVertInc);           break;
        case SB_THUMBTRACK: nInc = nPos - m_nVscrollPos;         break;
        default:            nInc = 0;
    }

    nInc = max(-m_nVscrollPos, min(nInc, m_nVscrollMax - m_nVscrollPos));

    if (nInc)
    {
        m_nVscrollPos += nInc;
        int iMove = -VERT_PTS * nInc;
        ScrollWindow(0, iMove, NULL, NULL);
        SetScrollPos(SB_VERT, m_nVscrollPos, TRUE);
    }

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


void CScrollDialog::SetNewScrollRange(int cy) 
{
	m_ClientRect.bottom = cy;
    ResetScrollbars();
    SetupScrollbars();
}

void CScrollDialog::ScrollToPos(int nPos)
{
	nPos = (nPos*m_nVscrollMax)/m_ClientRect.bottom;

	if (nPos > m_nVscrollMax/2)
		nPos+= 2*VERT_PTS;
	else
		nPos-= 2*VERT_PTS;

	OnVScroll(SB_THUMBTRACK, nPos, NULL);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Canada Canada

Comments and Discussions