www.pudn.com > VCShell_Samples.rar > ScreenSaverTrayIcon.cpp


/******************************************************************************* 
* File : ScreenSaverTrayIcon.cpp 
******************************************************************************** 
* Program : ScreenSaverTrayIcon 
******************************************************************************** 
* Version : 1.0                       * Author : T.Maurel 
* Date    : 04/26/1999 
* 
* 
* 
* 
* 
* Application main class implementation 
******************************************************************************* 
* Parameters: 
* ---------- 
*	'start'		: Start the screensaver and close the app 
*		: install the trayicon 
* 
*******************************************************************************/ 
#include "stdafx.h" 
#include "ScreenSaverTrayIcon.h" 
#include "mainfrm.h" 
 
#include  
#include "Splash.h" 
 
 
//////////////////////////////////////////////////////////////// 
CScreenSaverTrayIconApp theApp; 
BOOL bClassRegistered = FALSE; 
 
//////////////////////////////////////////////////////////////// 
// 
// 
BEGIN_MESSAGE_MAP(CScreenSaverTrayIconApp, CWinApp) 
	//{{AFX_MSG_MAP(CScreenSaverTrayIconApp) 
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
 
//////////////////////////////////////////////////////////////// 
// 
// 
BOOL CScreenSaverTrayIconApp::InitInstance() 
{ 
	// If the command line is 'start', start the screensaver and close the app. 
	if (stricmp(m_lpCmdLine, "start") == 0) 
	{ 
		::PostMessage(::GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0L);		 
		return FALSE; 
	} 
 
	// If a previous instance of the application is already running, 
	// then activate it and return FALSE from InitInstance to 
	// end the execution of this instance. 
	if(!FirstInstance()) 
		return FALSE; 
 
 
	// CG: The following block was added by the Splash Screen component. 
	CCommandLineInfo cmdInfo; 
	ParseCommandLine(cmdInfo); 
	CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash); 
 
 
	// Register our unique class name that we wish to use 
	WNDCLASS wndcls; 
	memset(&wndcls, 0, sizeof(WNDCLASS));   // start with NULL defaults 
 
	wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; 
	wndcls.lpfnWndProc = ::DefWindowProc; 
	wndcls.hInstance = AfxGetInstanceHandle(); 
	wndcls.hIcon = LoadIcon(IDR_MAINFRAME); // or load a different icon 
	wndcls.hCursor = LoadCursor( IDC_ARROW ); 
	wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); 
	wndcls.lpszMenuName = NULL; 
 
	// Specify our own class name for using FindWindow later 
	wndcls.lpszClassName = _T("ScreenSaverTrayIconAppClass"); 
 
	// Register new class and exit if it fails 
	if(!AfxRegisterClass(&wndcls)) 
	{ 
		TRACE("Class Registration Failed\n"); 
		return FALSE; 
	} 
	bClassRegistered = TRUE; 
 
	// Create main frame window (don't use doc/view stuff) 
	CMainFrame* pMainFrame = new CMainFrame; 
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) 
		return FALSE; 
	pMainFrame->ShowWindow(SW_HIDE); 
	pMainFrame->UpdateWindow(); 
	m_pMainWnd = pMainFrame; 
	return TRUE; 
} 
 
 
//////////////////////////////////////////////////////////////// 
// 
// 
int CScreenSaverTrayIconApp::ExitInstance() 
{ 
    // Save the screensaver's state 
    BOOL bEnabled; 
	::SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &bEnabled, 0); 
    ::SystemParametersInfo(	SPI_SETSCREENSAVEACTIVE, 
							bEnabled, 
							NULL, 
							SPIF_UPDATEINIFILE); 
 
    // Release the wndclass name 
    if(bClassRegistered) 
		::UnregisterClass(_T("ScreenSaverTrayIconAppClass"),AfxGetInstanceHandle()); 
	return CWinApp::ExitInstance(); 
}  
 
 
//////////////////////////////////////////////////////////////// 
// 
// 
BOOL CScreenSaverTrayIconApp::FirstInstance() 
{  
	// Determine if another window with our class name exists... 
	if (CWnd::FindWindow(_T("ScreenSaverTrayIconAppClass"),NULL) != NULL) 
		return FALSE; 
	else 
		// First instance. Proceed as normal. 
		return TRUE; 
}  
 
 
//////////////////////////////////////////////////////////////// 
// 
// 
void CScreenSaverTrayIconApp::OnAppAbout() 
{ 
	CDialog(IDD_ABOUTBOX).DoModal(); 
} 
 
 
 
BOOL CScreenSaverTrayIconApp::PreTranslateMessage(MSG* pMsg) 
{ 
	// CG: The following lines were added by the Splash Screen component.
	if (CSplashWnd::PreTranslateAppMessage(pMsg))
		return TRUE;

	return CWinApp::PreTranslateMessage(pMsg); 
}