################### This is the Source File ###############
#include "SaverDetect.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// Construction/Destruction
const LPCTSTR CSaverDetect::SS_WINDOW_CLASS = "WindowsScreenSaverClass" ;
const LPCTSTR CSaverDetect::SS_EXT = ".SCR" ;
const int CSaverDetect::SS_EXT_LEN = lstrlen(SS_EXT) ;
const LPTSTR CSaverDetect::SS_DESK = "screen-saver" ;
CSaverDetect::CSaverDetect() : OS_VERSION(GetOSVersion())
{
}
CSaverDetect::~CSaverDetect()
{
}
DWORD CSaverDetect::GetOSVersion()
{
OSVERSIONINFO verInfo;
verInfo.dwOSVersionInfoSize = sizeof(verInfo);
GetVersionEx(&verInfo);
return verInfo.dwPlatformId;
}
bool CSaverDetect::IsWindowFullScr(HWND hWnd)
{
if ( IsWindowVisible(hWnd) )
{
WINDOWPLACEMENT wp ;
wp.length = sizeof(wp) ;
if ( GetWindowPlacement(hWnd, &wp) && wp.showCmd != SW_SHOWMINIMIZED )
{
CScr scr ;
RECT r ;
return GetWindowRect(hWnd, &r) && r.left <= 0 && r.right >= scr.W && r.top <= 0 && r.bottom >= scr.H ;
}
}
return FALSE;
}
bool CSaverDetect::SSActive32()
{
HWND hWnd = GetActiveWindow();
if ( IsNotConfigureOrPreview(hWnd) )
{
HINSTANCE hInstance = reinterpret_cast<HINSTANCE> (GetWindowLong(hWnd, GWL_HINSTANCE)) ;
if ( hInstance )
{
TCHAR szBuffer[MAX_PATH] ;
GetModuleFileName(hInstance, szBuffer, sizeof(szBuffer)) ;
int length = lstrlen(szBuffer);
return length >= SS_EXT_LEN && lstrcmpi(szBuffer+length-SS_EXT_LEN, SS_EXT) == 0 ;
}
}
return FALSE;
}
bool CSaverDetect::SSActiveNT()
{
HDESK hDesktop = OpenDesktop(SS_DESK, 0, FALSE, MAXIMUM_ALLOWED);
if ( hDesktop )
{
CloseDesktop(hDesktop);
return TRUE;
}
return GetLastError() == ERROR_ACCESS_DENIED ;
}
################ This is the Header File #################
#if !defined(AFX_SAVERDETECT_H__727D72F7_C16C_4165_AD8C_C3B9E4F02649__INCLUDED_)
#define AFX_SAVERDETECT_H__727D72F7_C16C_4165_AD8C_C3B9E4F02649__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CSaverDetect
{
public:
CSaverDetect();
virtual ~CSaverDetect();
bool IsScreenSaverActive(void) { return IsNotConfigureOrPreview(FindWindow(SS_WINDOW_CLASS, NULL)) ; } ;
private:
DWORD GetOSVersion() ;
bool IsWindowFullScr(HWND) ;
bool IsWindowOnTop(HWND hWnd) { return (GetWindowLong(hWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0 ; }
bool IsNotConfigureOrPreview(HWND hWnd) { return IsWindow(hWnd) && (GetParent(hWnd) == NULL || IsWindowOnTop(hWnd) || IsWindowFullScr(hWnd)) ; } ;
bool SSActive32(void) ;
bool SSActiveNT(void) ;
private:
struct CScr
{
const int H, W;
CScr() : W (GetSystemMetrics(SM_CXSCREEN)), H (GetSystemMetrics(SM_CYSCREEN)) {}
};
static const LPCTSTR SS_WINDOW_CLASS ;
static const LPCTSTR SS_EXT ;
static const int SS_EXT_LEN ;
static const LPTSTR SS_DESK ;
const DWORD OS_VERSION ;
};
#endif
###################### End of Code ########################
To use it simply create and instance of the class and then call the IsScreenSaverActive() function.
William
Software Engineer
ICQ No. 56047340
#include "SaverDetect.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// Construction/Destruction
const LPCTSTR CSaverDetect::SS_WINDOW_CLASS = "WindowsScreenSaverClass" ;
const LPCTSTR CSaverDetect::SS_EXT = ".SCR" ;
const int CSaverDetect::SS_EXT_LEN = lstrlen(SS_EXT) ;
const LPTSTR CSaverDetect::SS_DESK = "screen-saver" ;
CSaverDetect::CSaverDetect() : OS_VERSION(GetOSVersion())
{
}
CSaverDetect::~CSaverDetect()
{
}
DWORD CSaverDetect::GetOSVersion()
{
OSVERSIONINFO verInfo;
verInfo.dwOSVersionInfoSize = sizeof(verInfo);
GetVersionEx(&verInfo);
return verInfo.dwPlatformId;
}
bool CSaverDetect::IsWindowFullScr(HWND hWnd)
{
if ( IsWindowVisible(hWnd) )
{
WINDOWPLACEMENT wp ;
wp.length = sizeof(wp) ;
if ( GetWindowPlacement(hWnd, &wp) && wp.showCmd != SW_SHOWMINIMIZED )
{
CScr scr ;
RECT r ;
return GetWindowRect(hWnd, &r) && r.left <= 0 && r.right >= scr.W && r.top <= 0 && r.bottom >= scr.H ;
}
}
return FALSE;
}
bool CSaverDetect::SSActive32()
{
HWND hWnd = GetActiveWindow();
if ( IsNotConfigureOrPreview(hWnd) )
{
HINSTANCE hInstance = reinterpret_cast<HINSTANCE> (GetWindowLong(hWnd, GWL_HINSTANCE)) ;
if ( hInstance )
{
TCHAR szBuffer[MAX_PATH] ;
GetModuleFileName(hInstance, szBuffer, sizeof(szBuffer)) ;
int length = lstrlen(szBuffer);
return length >= SS_EXT_LEN && lstrcmpi(szBuffer+length-SS_EXT_LEN, SS_EXT) == 0 ;
}
}
return FALSE;
}
bool CSaverDetect::SSActiveNT()
{
HDESK hDesktop = OpenDesktop(SS_DESK, 0, FALSE, MAXIMUM_ALLOWED);
if ( hDesktop )
{
CloseDesktop(hDesktop);
return TRUE;
}
return GetLastError() == ERROR_ACCESS_DENIED ;
}
################ This is the Header File #################
#if !defined(AFX_SAVERDETECT_H__727D72F7_C16C_4165_AD8C_C3B9E4F02649__INCLUDED_)
#define AFX_SAVERDETECT_H__727D72F7_C16C_4165_AD8C_C3B9E4F02649__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CSaverDetect
{
public:
CSaverDetect();
virtual ~CSaverDetect();
bool IsScreenSaverActive(void) { return IsNotConfigureOrPreview(FindWindow(SS_WINDOW_CLASS, NULL)) ; } ;
private:
DWORD GetOSVersion() ;
bool IsWindowFullScr(HWND) ;
bool IsWindowOnTop(HWND hWnd) { return (GetWindowLong(hWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0 ; }
bool IsNotConfigureOrPreview(HWND hWnd) { return IsWindow(hWnd) && (GetParent(hWnd) == NULL || IsWindowOnTop(hWnd) || IsWindowFullScr(hWnd)) ; } ;
bool SSActive32(void) ;
bool SSActiveNT(void) ;
private:
struct CScr
{
const int H, W;
CScr() : W (GetSystemMetrics(SM_CXSCREEN)), H (GetSystemMetrics(SM_CYSCREEN)) {}
};
static const LPCTSTR SS_WINDOW_CLASS ;
static const LPCTSTR SS_EXT ;
static const int SS_EXT_LEN ;
static const LPTSTR SS_DESK ;
const DWORD OS_VERSION ;
};
#endif
###################### End of Code ########################
To use it simply create and instance of the class and then call the IsScreenSaverActive() function.
William
Software Engineer
ICQ No. 56047340