Click here to Skip to main content
15,884,473 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.6K   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.
// Saver.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "saver.h"
#include "ClockSaverWnd.h"

#include "ClockSaverConfigDialog.h"
#include "MyCommandLineInfo.h"

#include <cmath>

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSaverApp

double pi;

BEGIN_MESSAGE_MAP(CSaverApp, CWinApp)
	//{{AFX_MSG_MAP(CSaverApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSaverApp construction

CSaverApp::CSaverApp()
{
	pi = 4.0 * ::atan( 1.0 ); // initialize the global value of pi
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CSaverApp object

CSaverApp theApp;

//*****************************************************************************
/////////////////////////////////////////////////////////////////////////////
// CSaverApp initialization
//-----------------------------------------------------------------------------
BOOL CSaverApp::InitInstance()
{
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

	InitCommonControls();

	::CoInitialize( NULL );

	SetRegistryKey(IDS_REG_KEY);

	CMyCommandLineInfo commandLine;
	CWinApp::ParseCommandLine( commandLine );

	if ( commandLine.m_saver )
		DoSaver();

	else if ( commandLine.m_preview )
		DoPreview( commandLine.m_hWndParent );

	else if ( commandLine.m_config )
		DoConfig( commandLine.m_hWndParent );

	else if ( commandLine.m_password )
		DoChangePassword( commandLine.m_hWndParent );

	return m_pMainWnd != NULL;
}

void CSaverApp::DoSaver()
{
	CClockSaverWnd *pWnd = new CClockSaverWnd();
	pWnd->Create();
	pWnd->ShowWindow( TRUE );

	m_pMainWnd = pWnd;
}

void CSaverApp::DoPreview(HWND hWndParent)
{
	// the preview command is when the window shows up on the screen saver tab
	// on the screen properties dialog. For it to work we must have a valid HWND from
	// the command line
	if ( hWndParent != NULL )
	{
		CWnd *pWndParent = CWnd::FromHandle( hWndParent );
		ASSERT( pWndParent != NULL );

		CClockWnd *pWnd = new CClockWnd();
		CRect rect;
		pWndParent->GetClientRect( &rect );
		pWnd->Create(0, CString( reinterpret_cast<LPCTSTR>(IDS_PREVIEW)), WS_VISIBLE | WS_CHILD, rect, pWndParent );
		pWnd->SetParent( pWndParent );

		m_pMainWnd = pWnd;
	}
}

void CSaverApp::DoConfig(HWND hWndParent)
{
	// this is primarily so we can run the config dialog from the debugger
	if ( hWndParent == NULL )
		hWndParent = ::GetDesktopWindow();

	CClockSaverConfigDialog dlg( CWnd::FromHandle( hWndParent ) );

	if ( dlg.DoModal() == IDOK )
		dlg.Save();
}

void CSaverApp::DoChangePassword(HWND hWndParent)
{
	// This only gets called on Windows 9x, when started with the /a option.
	HINSTANCE hmpr = ::LoadLibrary(_T("MPR.DLL"));

	if (hmpr != NULL)
	{
		typedef VOID (WINAPI *PPWDCHANGEPASSWORD)
			(LPCSTR lpcRegkeyname, HWND hWnd,
			 UINT uiReserved1, UINT uiReserved2);

		PPWDCHANGEPASSWORD pPwdChangePassword =	reinterpret_cast<PPWDCHANGEPASSWORD>(::GetProcAddress(hmpr, "PwdChangePasswordA"));

		if (pPwdChangePassword != NULL)
			pPwdChangePassword("SCRSAVE", hWndParent, 0, 0);
		else
			TRACE(_T("PwdChangeProc not found: cannot change password.\n"));
	
		::FreeLibrary(hmpr);
	}
	else
		TRACE(_T("MPR.DLL not found: cannot change password.\n"));
}

BOOL CSaverApp::ExitInstance(void)
{
	::CoUninitialize();
	return CWinApp::ExitInstance();
}

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