www.pudn.com > VCShell_Samples.rar > MainFrm.cpp
/*******************************************************************************
* File : mainfrm.cpp
********************************************************************************
* Program : ScreenSaverTrayIcon
********************************************************************************
* Version : 1.0 * Author : T.Maurel
* Date : 04/26/1999
*
*
*
*
*
* Main frame window implementation
*******************************************************************************/
#include "stdafx.h"
#include "ScreenSaverTrayIcon.h"
#include "mainfrm.h"
#include "Splash.h"
// Message ID used for tray notifications
#define WM_MY_TRAY_NOTIFICATION WM_USER+0
// Timer ID
#define ID_SCREENSAVERTASKBAR_TIMER 39
IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_MESSAGE(WM_MY_TRAY_NOTIFICATION, OnTrayNotification)
ON_WM_CREATE()
ON_COMMAND(IDM_APP_START_SAVER, OnAppStartSaver)
ON_COMMAND(IDM_TRAY_ENABLESCREENSAVER, OnTrayEnableScreensaver)
ON_COMMAND(IDM_APP_DISPLAY_PROPERTIES, OnAppDisplayProperties)
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(IDM_QUICK_ACC_0,IDM_QUICK_ACC_0+100,OnQuickAcc)
END_MESSAGE_MAP()
////////////////////////////////////////////////////////////////
//
//
CMainFrame::CMainFrame()
: m_trayIcon(IDR_TRAYICON)
{
}
////////////////////////////////////////////////////////////////
//
//
CMainFrame::~CMainFrame()
{
}
////////////////////////////////////////////////////////////////
//
//
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// Use the specific class name we established earlier
cs.lpszClass = _T("ScreenSaverTrayIconAppClass");
return CFrameWnd::PreCreateWindow(cs);
}
////////////////////////////////////////////////////////////////
//
//
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// Set up tray icon
m_trayIcon.SetNotificationWnd(this, WM_MY_TRAY_NOTIFICATION);
BOOL bEnabled;
BOOL bRet = ::SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &bEnabled, 0);
ChangeIcon(bEnabled);
// CG: The following line was added by the Splash Screen component.
//CSplashWnd::ShowSplashScreen(this);
m_muPop.LoadMenu(IDR_TRAYICON);
EnumAllSreenSaver();
return 0;
}
////////////////////////////////////////////////////////////////
// Handle notification from tray icon: display a message.
//
LRESULT CMainFrame::OnTrayNotification(WPARAM uID, LPARAM lEvent)
{
// NOTE : THIS CODE HAS BEEN MOVED FROM CTrayIcon CLASS FROM Paul DiLascia.
// let tray icon do default stuff
if (m_trayIcon.OnTrayNotification(uID, lEvent) == 0L) {
return 0L;
} else {
// If there's a resource menu with the same ID as the icon, use it as
// the right-button popup menu. we will interprets the first
// item in the menu as the default command for WM_LBUTTONDBLCLK
//
// by wenyy
//CMenu menu;
//if (!menu.LoadMenu(uID))
// return 0;
CMenu* pSubMenu = m_muPop.GetSubMenu(0);
if (!pSubMenu)
return 0;
BOOL bEnabled;
::SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &bEnabled, 0);
CString string;
string.LoadString(bEnabled ? IDI_DISABLE_SCREENSAVER : IDI_ENABLE_SCREENSAVER);
pSubMenu->ModifyMenu(IDM_TRAY_ENABLESCREENSAVER, MF_BYCOMMAND | MF_STRING, IDM_TRAY_ENABLESCREENSAVER, string);
if (lEvent==WM_RBUTTONUP) {
// Make first menu item the default (bold font)
::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);
// Display the menu at the current mouse location. There's a "bug"
// (Microsoft calls it a feature) in Windows 95 that requires calling
// SetForegroundWindow. To find out more, search for Q135788 in MSDN.
//
CPoint mouse;
GetCursorPos(&mouse);
::SetForegroundWindow(m_hWnd);
::TrackPopupMenu(pSubMenu->m_hMenu, 0, mouse.x, mouse.y, 0,
m_hWnd, NULL);
} else { // double click: execute first menu item
SendMessage(WM_COMMAND, pSubMenu->GetMenuItemID(0), 0);
}
}
return 1L;
}
////////////////////////////////////////////////////////////////
//
//
void CMainFrame::ChangeIcon(BOOL bChange)
{
// Pass the good icon ID to the tray object.
m_trayIcon.SetIcon(bChange ? IDI_SCREENSAVER_TASK : IDI_NO_SCREENSAVER);
}
////////////////////////////////////////////////////////////////
//
//
void CMainFrame::OnAppStartSaver()
{
// To avoid some mouse movement before starting screensaver, we
// send a timer message. The message SC_SCREENSAVE will be posting
// at this moment.
SetTimer(ID_SCREENSAVERTASKBAR_TIMER, 1039, NULL);
}
////////////////////////////////////////////////////////////////
//
//
void CMainFrame::OnAppDisplayProperties()
{
// Start the display properties dialog
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
CreateProcess( NULL, // No module name (use command line).
"control.exe desk.cpl", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi); // Pointer to PROCESS_INFORMATION structure.
}
////////////////////////////////////////////////////////////////
//
//
void CMainFrame::OnTimer(UINT nIDEvent)
{
// Ok, we received the timer msg, so kill the timer and start
// the screensaver.
if (nIDEvent == ID_SCREENSAVERTASKBAR_TIMER)
{
KillTimer(ID_SCREENSAVERTASKBAR_TIMER);
GetDesktopWindow()->PostMessage(WM_SYSCOMMAND, SC_SCREENSAVE, 0L);
}
CFrameWnd::OnTimer(nIDEvent);
}
////////////////////////////////////////////////////////////////
//
//
void CMainFrame::OnTrayEnableScreensaver()
{
// Switch the state of the screensaver (on/off)
// We need to control this state at each call because
// the user could change it 'directly'. The change is
// volatile, and will be updated in the ExitInstance().
BOOL bEnabled;
BOOL bRet = ::SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &bEnabled, 0);
bEnabled = !bEnabled;
bRet = ::SystemParametersInfo( SPI_SETSCREENSAVEACTIVE,
bEnabled,
NULL,
SPIF_SENDCHANGE);
if (bRet)
ChangeIcon(bEnabled);
}
////////
// by wenyy
void CMainFrame::EnumAllSreenSaver(void)
{
m_arrSS.RemoveAll();
char szDir[256];
GetWindowsDirectory(szDir,255);
ListSS(szDir);
GetSystemDirectory(szDir,255);
ListSS(szDir);
CMenu* pSubM=m_muPop.GetSubMenu(0);
CMenu* pSubM2=pSubM->GetSubMenu(2);
pSubM2->DeleteMenu(0,MF_BYPOSITION);
}
void CMainFrame::ListSS(LPCSTR pszDir)
{
CString szFile;
szFile=pszDir;
szFile+="\\*.scr";
CFileFind ff;
CMenu* pSubM=m_muPop.GetSubMenu(0);
CMenu* pSubM2=pSubM->GetSubMenu(2);
if(ff.FindFile(szFile,0))
{
while(ff.FindNextFile())
{
m_arrSS.Add(ff.GetFilePath());
pSubM2->AppendMenu(MF_STRING,
IDM_QUICK_ACC_0 + m_arrSS.GetUpperBound(),
ff.GetFileTitle());
}
}
}
void CMainFrame::OnQuickAcc( UINT nID )
{
CString szSS;
szSS=m_arrSS.GetAt(nID - IDM_QUICK_ACC_0);
szSS+=" s";
char szCmd[255];
strcpy(szCmd,szSS);
/* CreateProcess( NULL, // No module name (use command line).
szCmd, //"control.exe desk.cpl", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent’s environment block.
NULL, // Use parent’s starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi); // Pointer to PROCESS_INFORMATION structure.
*/
WinExec(szCmd,SW_SHOW);
}