Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C++

Clock Screen Saver

Rate me:
Please Sign up or sign in to vote.
4.73/5 (42 votes)
27 Jun 2004CPOL4 min read 154.8K   4.5K   48  
A mouse trailing clock screen saver written in MFC
This project is a screen saver application that I originally started as a way to pick up C++/MFC. In the years since, I’ve added some features to it like Outlook Calendar and MAPI support for signaling when new mail arrives while the screen saver is active.
// RotatingTextSprite.cpp : implementation file
//

#include "stdafx.h"
#include "RotatingTextSprite.h"
#include "AlternatingGlyph.h"

#include <cmath>

extern double pi;

// CRotatingTextSprite

CRotatingTextSprite::CRotatingTextSprite(const double height, const double width, COLORREF color) :	
											CCircularSprite( height, width, color ),					
											m_ForceLeftToRight( false )											
{
}

CRotatingTextSprite::~CRotatingTextSprite()
{
}


// CRotatingTextSprite member functions

void CRotatingTextSprite::SetText( CString strText )
{
	ASSERT( m_paGlyphs );

	CString str;
	CString str2;

	CString reversedText( strText );
	reversedText.MakeReverse();

	//add each character to the array as a glyph
	for ( int i = 0, l = strText.GetLength(); i < l; i++ ) 
	{
		str = strText.GetAt(i);
		str2 = reversedText.GetAt(i);

		m_paGlyphs->Add( new CAlternatingGlyph( i, str, str2 ) );
	}

	if ( GetGlyphCount() > 0 )
		m_rSplit = 360.0 / GetGlyphCount();
}

void CRotatingTextSprite::SetForceLeftToRight( bool value )
{
	m_ForceLeftToRight = value;
}

double CRotatingTextSprite::GetTickX()
{
	return ::GetTickCount() * m_rRotateSpeed;
}

double CRotatingTextSprite::GetTickY()
{
	return ::GetTickCount() * m_rRotateSpeed;
}

void CRotatingTextSprite::TrackPoint(const CPoint* ppoint, const double trackingSpeed)
{
	m_Point = *ppoint;
	CSprite::TrackPoint( ppoint, trackingSpeed );
}

void CRotatingTextSprite::ReDraw(CDC* pDC)
{	
 	if ( m_ForceLeftToRight )
	{
		COLORREF oldColor = pDC->SetTextColor( m_Color );

		CAlternatingGlyph* glyph;

		for ( int i=0, l=m_paGlyphs->GetCount(); i < l; i++ )
		{
			glyph = (CAlternatingGlyph*)((*m_paGlyphs)[i]);
			
			//set the glyph to its final location by
			//calling CalcX and CalcY which returns the custom 
			//animation's relative location from the derived class
			glyph->SetPoint( glyph->x + CalcX( glyph ), glyph->y + CalcY( glyph ) );

			DrawGlpyh( pDC, glyph );
		}

		pDC->SetTextColor( oldColor );
	}
	else
	{
		CSprite::ReDraw( pDC );
	}
}

void CRotatingTextSprite::DrawGlpyh( CDC* pDC, CAlternatingGlyph* pGlyph )
{
	const double FADE_ANGLE = 23.0;

	// get the polar angle of the glyph
	double theta = ::atan2( (double)(pGlyph->y - m_Point.y), (double)(pGlyph->x - m_Point.x) ) * ( 180 / pi );

	COLORREF color = m_Color;

	// if the glyph is approaching 180 or 0 degrees fade its color
	// as a function of its distance from those values
	if ( abs( theta ) < FADE_ANGLE )
		color = Darken( color, ::abs( theta ) / FADE_ANGLE );
	else if ( 180.0 - ::abs( theta ) < FADE_ANGLE )
		color = Darken( color, ( 180.0 - ::abs( theta ) ) / FADE_ANGLE );

	pDC->SetTextColor( color );

	if ( theta > 0.0 && theta < 180.0 )
		pGlyph->DrawAlternate( pDC );
	else
		pGlyph->Draw( pDC );
}

COLORREF CRotatingTextSprite::Darken( COLORREF color, double pct )
{
	return RGB( GetRValue( color ) * pct, GetGValue( color ) * pct, GetBValue( color ) * pct );
}

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
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions