Clarification of Question by
kyle1973-ga
on
24 May 2003 11:32 PDT
ok the code is rather simple, it is currently being modified to use
the traditional screensaver function. However right now it just uses
the winmain function. Here is the code.
#include "stdafx.h"
#include "Life.h"
#include "Display.h"
#include <scrnsave.h>
#include <windows.h>
#include <eh.h>
#define CANCEL 1000
#define SUBMIT 1001
#define MAX_LOADSTRING 100
#define CYCLES 1300
#define CODE_LENGTH 20
#define CONFIGURATION 1002
#define SCREENSAVER 1003
#define PREVIEW 1004
extern BOOL fChildPreview;
int MouseAlreadyMoved=FALSE;
BOOL go=true;
//#include <unistd.h>
//unsigned int sleep(unsigned int sec);
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];// the main window class name
BYTE registration_code[CODE_LENGTH]="1_=a-z5Vg1_=a-z5Vg3";
//TCHAR registration[MAX_LOADSTRING] = "Registration"; // the main
window class name
int mode = SCREENSAVER;
BOOL dialog=FALSE;
CEdit *pEdit;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR string);
BOOL InitInstance(HINSTANCE, int, LPTSTR cmmndLn);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK Configurator(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ScreenSaver(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK PreviewSaver(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK RegProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
char *commandLine;
void exitSystem(HKEY hKey);
void exitSystem();
void registerLife(HWND hWnd);
BOOL isRegistered(HWND hWnd);
BOOL CALLBACK MyDialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM
lParam);
void setPreview(char const *string);
BOOL previewRunning();
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
commandLine=(char *)lpCmdLine;
if(strcmp(lpCmdLine,"/s")==0) {
mode=SCREENSAVER;
} else if(strcmp(lpCmdLine,"/c")==0) {
mode=CONFIGURATION;
} else if(strncmp(lpCmdLine,"/p ",2)==0) {
mode=PREVIEW;
}
//MessageBox(hWnd,"1","1",1);
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_LIFE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance,lpCmdLine);
//MessageBox((HWND)atoi(&lpCmdLine[3]),"nonfailure","nonfailure",1);
if (!InitInstance (hInstance, nCmdShow,lpCmdLine))
{
//MessageBox((HWND)atoi(&lpCmdLine[3]),"failure","failure",1);
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_LIFE);
//MessageBox((HWND)atoi(&lpCmdLine[3]),"1failure","1failure",1);
// Main message loop:
//get the ball rolling
if(mode==PREVIEW) {
MessageBox((HWND)atoi(&lpCmdLine[3]),"1","1",1);
//FindWindow(NULL,"Display Properties");
//setPreview("true");
SendMessage((HWND)atoi(&lpCmdLine[3]),WM_PAINT,0,0);
WndProc((HWND)atoi(&lpCmdLine[3]),WM_PAINT,0,0);
UpdateWindow((HWND)atoi(&lpCmdLine[3]));
//PostMessage((HWND)atoi(&lpCmdLine[3]),WM_PAINT,0,0);
//EnableWindow((HWND)atoi(&lpCmdLine[3]),true);
}
//UpdateWindow((HWND)atoi(&lpCmdLine[3]));
//MessageBox((HWND)atoi(&lpCmdLine[3]),"test","test",1);
MessageBox((HWND)atoi(&lpCmdLine[3]),"2","2",1);
while (GetMessage(&msg, NULL, 0, 0))
{
MessageBox((HWND)atoi(&lpCmdLine[3]),"2failure","2failure",1);
//Sleep(1000);
if(mode==PREVIEW)
MessageBox((HWND)atoi(&lpCmdLine[3]),"2","2",1);
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
if(mode==PREVIEW) {
MessageBox((HWND)atoi(&lpCmdLine[3]),"3","3",1);
}
MessageBox((HWND)atoi(&lpCmdLine[3]),"1","1",1);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if(mode==PREVIEW) {
MessageBox((HWND)atoi(&lpCmdLine[3]),"1","1",1);
}
return 0;
}
void exitSystem(HWND hwnd) {
ShowCursor(TRUE);
if(isRegistered(hwnd)||MessageBox(hwnd,"Do You Wish to Register
Life?","",MB_ICONQUESTION|MB_YESNO)==IDNO) {
DestroyWindow(hwnd);
PostQuitMessage(0);
setPreview("false");
ShowCursor(TRUE);
go=false;
exit(0);
} else {
//MessageBox(hwnd,"register","registration",0);
ShowCursor(TRUE);
DestroyWindow(hwnd);
mode=CONFIGURATION;
InitInstance(AfxGetInstanceHandle(),SW_SHOW,"registration");
}
}
void exitSystem() {
ShowCursor(TRUE);
PostQuitMessage(0);
setPreview("false");
ShowCursor(TRUE);
go=false;
exit(0);
}
void registerLife(HWND hWnd) {
HKEY hKey;
char *value=new char[CODE_LENGTH];
REGSAM samDesired=KEY_QUERY_VALUE;
LPCSTR subKey = "Software\\Life_Saver";
LONG ret = RegCreateKeyEx(HKEY_CURRENT_USER,
subKey,
0,
NULL,
REG_OPTION_NON_VOLATILE,
samDesired,
NULL,
&hKey,
NULL
);
RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Life_Saver",0,KEY_WRITE,&hKey);
ret = RegSetValueEx(hKey,"reg_code",0,REG_SZ,registration_code,CODE_LENGTH);
if(ret==0) {
MessageBox(hWnd,"SUCCESSFUL REGISTRY!","reg",1);
} else {
MessageBox(hWnd,"REGISTRATION UNSUCCESSFUL","reg",1);
}
}
void setPreview(char const *string) {
HKEY hKey;
char *value=new char[CODE_LENGTH];
REGSAM samDesired=KEY_QUERY_VALUE;
LPCSTR subKey = "Software\\Life_Saver";
LONG ret = RegCreateKeyEx(HKEY_CURRENT_USER,
subKey,
0,
NULL,
REG_OPTION_NON_VOLATILE,
samDesired,
NULL,
&hKey,
NULL
);
RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Life_Saver",0,KEY_WRITE,&hKey);
ret = RegSetValueEx(hKey,"preview_running",0,REG_SZ,(BYTE
*)string,strlen(string));
if(ret==0) {
//MessageBox(hWnd,"SUCCESSFUL REGISTRY!","reg",1);
} else {
//MessageBox(hWnd,"REGISTRATION UNSUCCESSFUL","reg",1);
}
}
BOOL previewRunning() {
HKEY hKey;
int TRUEORFALSE = 5;
BYTE *value=new BYTE[TRUEORFALSE];
REGSAM samDesired=KEY_SET_VALUE;
LPCSTR subKey = "Software\\Life_Saver";
DWORD count=TRUEORFALSE;
LONG ret;
RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Life_Saver",0,KEY_READ,&hKey);
//MessageBox(hWnd,(LPCSTR)value,"register",1);
ret = RegQueryValueEx(hKey,"preview_running",0,NULL,value,&count);
//MessageBox(hWnd,(LPCSTR)value,"register",1);
if(ret==ERROR_SUCCESS&&CString(value)==CString("true")) {
//MessageBox(hWnd,"success","MORE",1);
return true;
} else {
//MessageBox(hWnd,"failure","reg",1);
//return false;
return true;
}
}
BOOL isRegistered(HWND hWnd) {
HKEY hKey;
BYTE *value=new BYTE[CODE_LENGTH];
REGSAM samDesired=KEY_SET_VALUE;
LPCSTR subKey = "Software\\Life_Saver";
DWORD count=CODE_LENGTH;
LONG ret;
RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Life_Saver",0,KEY_READ,&hKey);
//MessageBox(hWnd,(LPCSTR)value,"register",1);
ret = RegQueryValueEx(hKey,"reg_code",0,NULL,value,&count);
//MessageBox(hWnd,(LPCSTR)value,"register",1);
if(ret==ERROR_SUCCESS&&CString(value)==CString(registration_code)) {
//MessageBox(hWnd,"success","MORE",1);
return true;
} else {
//MessageBox(hWnd,"failure","reg",1);
return false;
}
}
BOOL CALLBACK MyDialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM
lParam)
{
return TRUE;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this
code
// to be compatible with Win32 systems prior to the
'RegisterClassEx'
// function that was added to Windows 95. It is important to call
this function
// so that the application will get 'well formed' small icons
associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR string)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_LIFE);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
//wcex.lpszMenuName = (LPCTSTR)IDC_LIFE;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global
variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, LPTSTR cmmndln)
{
pEdit = new CEdit;
HWND hWnd;
if(strncmp(cmmndln,"/p ",3)==0) {
hWnd=(HWND)atoi(&cmmndln[3]);
hInst = hInstance; // Store instance handle in our global variable
SetParent(hWnd,GetParent(hWnd));
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
}else if(strcmp(cmmndln,"/s")==0) {
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass,
"",
WS_POPUP,
0,
0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
} else if(strcmp("/c",cmmndln)==0||strcmp("registration",cmmndln)==0)
{
hInst = hInstance; // Store instance handle in our global variable
if(hWnd) {
DestroyWindow(hWnd);
}
hWnd = CreateWindow(
szWindowClass,
"",
WS_POPUP,
(GetSystemMetrics(SM_CXSCREEN))/2-250,
(GetSystemMetrics(SM_CYSCREEN))/2-20,
500,
40,
NULL,
NULL,
hInstance,
NULL);
CWnd *c = CWnd::FromHandle(hWnd);
pEdit->Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
CRect(250, 0, 500, 20), c, 1);
CStatic *text = new CStatic;
text->Create(_T("Enter you're registration code here."),
WS_CHILD|WS_VISIBLE|SS_CENTER,
CRect(0,0,250,20), c);
CButton *submit = new CButton();
submit->Create("submit",WS_CHILD|WS_VISIBLE,CRect(0,20,250,40),c,SUBMIT);
CButton *cancel = new CButton();
cancel->Create("cancel",WS_CHILD|WS_VISIBLE,CRect(250,20,500,40),c,CANCEL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
}
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
char buffer[20];
if(mode==SCREENSAVER) {
return ScreenSaver(hWnd,message,wParam,lParam);
} else if(mode==CONFIGURATION) {
return Configurator(hWnd,message,wParam,lParam);
} else if(mode==PREVIEW){
return PreviewSaver(hWnd,message,wParam,lParam);
} else {
return DefScreenSaverProc(hWnd,message,wParam,lParam);
}
}
LRESULT CALLBACK ScreenSaver(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
//same ratio as the screen
int width=GetSystemMetrics(SM_CXSCREEN)/10;
while(GetSystemMetrics(SM_CXSCREEN)%width!=0) {
width++;
}
int height=GetSystemMetrics(SM_CYSCREEN)/10;
while(GetSystemMetrics(SM_CYSCREEN)%height!=0) {
height++;
}
Board *b =new Board(width,height);
Display *d = new Display(b,hWnd,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
int i=0;
MSG msg;
switch (message)
{
case WM_SYSCOMMAND:
if(wParam==SC_SCREENSAVE) {
//do nothing this screen saver is already executing
} else if(wParam==SC_MONITORPOWER) {
//dont run when the monitor is not on
exitSystem(hWnd);
} else {
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
while(go) {
i=0;
while(i<CYCLES) {
//check if a close message exists
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
d->display();
b->react();
EndPaint(hWnd, &ps);
//check to see if the quit message was sent
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return TRUE;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
i++;
}
b->reset();
}
return DefWindowProc(hWnd,message,wParam,lParam);
break;
case WM_LBUTTONDOWN:
exitSystem(hWnd);
break;
case WM_RBUTTONDOWN:
exitSystem(hWnd);
break;
case WM_MOUSEMOVE:
//get the first movement. A first movement is detected even though
the mouse has not moved. I am not sure why this is?
if(MouseAlreadyMoved) {
exitSystem(hWnd);
} else {
MouseAlreadyMoved=TRUE;
}
break;
case WM_KEYDOWN:
exitSystem(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK PreviewSaver(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
if(FindWindow("LIFE.SCR",NULL)!=NULL) {
return 0;
}
char buffer6[20];
MessageBox(hWnd,itoa(message,buffer6,10),"test",1);
if(previewRunning()) {
//return DefWindowProc(hWnd,message,wParam,lParam);
} else {
setPreview("true");
}
setPreview("true");
PAINTSTRUCT ps;
HDC hdc;
//same ratio as the screen
RECT rect;
GetClientRect(hWnd,&rect);
char buffer3[10];
itoa(message,buffer3,10);
MessageBox(hWnd,CString(buffer3),CString(buffer3),1);
//SetParent(hWnd,FindWindow(NULL,"Display Properties"));
int width=rect.right/2;
int height=rect.bottom/2;
Board *b =new Board(width,height);
Display *d = new Display(b,hWnd,rect.right,rect.bottom);
ShowWindow(hWnd,SW_SHOW);
int i=0;
MSG msg;
int id=LOWORD(wParam);
switch (message)
{
case WM_SYSCOMMAND:
if(wParam==SC_SCREENSAVE) {
//do nothing this screen saver is already executing
} else if(wParam==SC_MONITORPOWER) {
//dont run when the monitor is not on
//setPreview("false");
//exitSystem(previewWnd);
} else {
//setPreview("false");
//exitSystem(hWnd);/ch (wParam) {
//case IDOK: case IDCANCEL:
//EndDialog(GetParent(hWnd),0);
//setPreview("false");
//exitSystem(hWnd);
break;
}
case WM_PAINT:
while(go) {
i=0;
while(i<CYCLES) {
//check if a close message exists
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
d->display();
b->react();
EndPaint(hWnd, &ps);
//check to see if the quit message was sent
{
setPreview("false");
/*if (msg.message == WM_QUIT||msg.message==WM_DESTROY) {
return TRUE;
}*/
TranslateMessage(&msg);
DispatchMessage(&msg);
}
i++;
}
b->reset();
}
//return DefWindowProc(hWnd,message,wParam,lParam);
break;
case WM_SHOWWINDOW:
setPreview("false");
break;
case WM_QUIT:
setPreview("false");
exitSystem(hWnd);
break;
case WM_DESTROY:
setPreview("false");
exitSystem(hWnd);
break;
default:
setPreview("false");
return DefWindowProc(hWnd, message, wParam, lParam);
}
setPreview("false");
return 0;
}
//
//
// Configuration screen
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK Configurator(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_SYSCOMMAND:
if(wParam==SC_SCREENSAVE) {
//do nothing this screen saver is already executing
} else if(wParam==SC_MONITORPOWER) {
//dont run when the monitor is not on
exitSystem(hWnd);
} else {
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
ShowWindow(hWnd,SW_SHOW);
break;
case WM_LBUTTONDOWN:
break;
case WM_RBUTTONDOWN:
break;
case WM_MOUSEMOVE:
break;
case WM_KEYDOWN:
break;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED)
{
switch (LOWORD(wParam))
{
case CANCEL:
exit(0);
break;
case SUBMIT:
CString string;
pEdit->GetWindowText(string);
//MessageBox(hWnd,"test","test",1);
if(string == CString(registration_code)) {
registerLife(hWnd);
}
DestroyWindow(hWnd);
PostQuitMessage(0);
ShowCursor(TRUE);
exit(0);
break;
}
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
// Message handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM
lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}