Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / MFC

A Slider Control for C++

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
30 May 2012CPOL3 min read 22.9K   941   7  
This post describes a slider control for C++ which make your application more attractive.
// SlideHolder.cpp : implementation file
//

#include "stdafx.h"
#include "SlideHolder.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSlideHolder

CSlideHolder::CSlideHolder()
{
	m_nOffset = 0;
	m_nCurPage = 0;
	m_bTimerAlive = FALSE;
}

CSlideHolder::~CSlideHolder()
{
}


BEGIN_MESSAGE_MAP(CSlideHolder, CStatic)
	//{{AFX_MSG_MAP(CSlideHolder)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSlideHolder message handlers

void CSlideHolder::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	if( nIDEvent == 1 ) {
		CRect rect;
		GetWindowRect(rect);	

		m_nMoveAmount = (m_nOffsetEnd - m_nOffset) / 7;
		if( 0 < m_nMoveAmount && m_nMoveAmount < 5 ) {
			m_nMoveAmount = 5;
		}
		if( 0 > m_nMoveAmount && m_nMoveAmount > -5 ) {
			m_nMoveAmount = -5;
		}
		
		m_nOffset += m_nMoveAmount;
		
		//CString s;
		//s.Format("m_nOffset = %d, m_nOffsetEnd = %d, m_nMoveAmount = %d", m_nOffset, m_nOffsetEnd, m_nMoveAmount);
		//OutputDebugString(s);

		BOOL bFin = FALSE;
		if( m_nMoveAmount > 0 ) {
			if( m_nOffset >= m_nOffsetEnd ) {
				m_nOffset = m_nOffsetEnd;
				bFin = TRUE;
			}
		} else if( m_nMoveAmount == 0 ) {
			m_nOffset = m_nOffsetEnd;
			bFin = TRUE;
		} else {
			if( m_nOffset <= m_nOffsetEnd ) {
				m_nOffset = m_nOffsetEnd;
				bFin = TRUE;
			}			
		}

		int nFirst = GetFirstVisibleWnd(m_nOffset);
		if( nFirst != -1 && (nFirst+1) < m_aryWnd.GetSize() ) {
			//CString s;
			//s.Format("nFirst = %d", nFirst);
			//OutputDebugString(s);
			
			for( int i = 0; i < m_aryWnd.GetSize(); i++ ) {
				if( i != nFirst && i != (nFirst + 1) ) {
					CWnd* pWnd = (CWnd*)m_aryWnd.GetAt(i);
					if( pWnd ) {
						if( pWnd->IsWindowVisible() ) {
							pWnd->ShowWindow(SW_HIDE);
						}
					}
				}
			}

			
			CWnd* pWnd = (CWnd*)m_aryWnd.GetAt(nFirst);
			if( pWnd ) {
				pWnd->SetWindowPos(NULL, m_nOffset + (nFirst*rect.Width()), 0, 0, 0, SWP_NOSIZE|SWP_NOZORDER|SWP_SHOWWINDOW);
			}
			
			if( (nFirst + 1) < m_aryWnd.GetSize() ) {
				pWnd = (CWnd*)m_aryWnd.GetAt(nFirst+1);
				if( pWnd ) {
					pWnd->SetWindowPos(NULL, m_nOffset + ((nFirst+1)*rect.Width()), 0, 0, 0, SWP_NOSIZE|SWP_NOZORDER|SWP_SHOWWINDOW);
					//pWnd->Invalidate( FALSE );
				}
			}
		}
		
		
		if(bFin){
			m_bTimerAlive = FALSE;
			KillTimer(nIDEvent);
		}
	}
	
	CStatic::OnTimer(nIDEvent);
}

void CSlideHolder::AddPage(CWnd *pPage)
{
	CRect rect;
	GetWindowRect(rect);
	m_aryWnd.Add( pPage );
	pPage->SetWindowPos( NULL, 0, 0, rect.Width(), rect.Height(), SWP_NOMOVE|SWP_NOZORDER );
}

void CSlideHolder::MoveNext()
{
	CRect rect;
	GetWindowRect(rect);

	m_nCurPage++;
	if( m_nCurPage < 0 ) {
		m_nCurPage = 0;
	}
	if( m_nCurPage >= m_aryWnd.GetSize() ) {
		m_nCurPage = m_aryWnd.GetSize()-1;
	}

	m_nOffsetEnd = m_nCurPage * rect.Width() * (-1);
	//CString s;
	//s.Format("m_nOffsetEnd = %d", m_nOffsetEnd);
	//OutputDebugString(s);
	if( m_bTimerAlive == FALSE ) {
		m_bTimerAlive = TRUE;
		SetTimer( 1, 10, NULL );
	}
}

void CSlideHolder::MovePrev()
{
	CRect rect;
	GetWindowRect(rect);

	// 0 1 2
	// 3
	m_nCurPage--;
	if( m_nCurPage < 0 ) {
		m_nCurPage = 0;
	}
	if( m_nCurPage >= m_aryWnd.GetSize() ) {
		m_nCurPage = m_aryWnd.GetSize()-1;
	}

	m_nOffsetEnd = m_nCurPage * rect.Width() * (-1);
	if( m_bTimerAlive == FALSE ) {
		m_bTimerAlive = TRUE;
		SetTimer( 1, 10, NULL );
	}
}

int CSlideHolder::GetFirstVisibleWnd(int nOffset)
{
	CRect rect;
	GetWindowRect(rect);

	if( nOffset <= 0 ) { // +n
		// ___________________
		// |          |
		// 0          width
		for( int i = 0; ; i++ ) {
			int nLeft = rect.Width() * (i+1) * (-1);
			int nRight = nLeft + rect.Width();
			if( nLeft <= nOffset && nOffset <= nRight ) {
				return i;
			}
		}
	}

	return -1;
}

void CSlideHolder::SetCurrentPage(int nPage)
{
	CRect rect;
	GetWindowRect(rect);

	m_nCurPage = nPage;
	if( m_nCurPage < 0 ) {
		m_nCurPage = 0;
	}
	if( m_nCurPage >= m_aryWnd.GetSize() ) {
		m_nCurPage = m_aryWnd.GetSize()-1;
	}

	m_nOffsetEnd = m_nCurPage * rect.Width() * (-1);
	
	if( m_bTimerAlive == FALSE ) {
		m_bTimerAlive = TRUE;
		SetTimer( 1, 10, 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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions