home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Desktop Works 1995 - 1996
/
desktopworks1995-1996.iso
/
scrnsave
/
winsaver
/
winsaver.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-01-01
|
18KB
|
603 lines
/* winsaver.c RHS 1/25/91
*/
#include<windows.h>
#include<stdio.h>
#include<mem.h>
#include<string.h>
#include<stdlib.h>
#define MAXWINDOWSPARM 256
#define MAXFILENAME 80
#define MAXWINS 256
#define ICONIC SW_SHOWMINNOACTIVE
#define SC_USER (0xF000-1)
#define SC_ABOUT SC_USER
#define SC_AUTOSAVE SC_USER-1
#define SC_AUTOLOAD SC_USER-2
#define MYTIMER 1
#define MYTIMER2 2
typedef struct _windows
{
RECT rect;
int winState;
HWND hWnd;
char filename[MAXFILENAME];
} Wins;
Wins windows[MAXWINS];
long FAR PASCAL WndProc(HWND, WORD, WORD, LONG);
void SaveSession(void);
void RestoreSession(void);
void GetWindowList(void);
void PaintWindowList(void);
int ReadProfile(void);
void LoadPrograms(int maxwins);
void RestoreSession(void);
void Execute(char *command, int ncmdshow);
HWND GetWindowHandle(char *name);
BOOL HandleUsed(HWND hWnd);
void ActivateProgMan(void);
void UpdateSystemMenu(void);
void Autoload(void);
int getint(char **p);
BOOL bufmatch(char *str, char *winbuf, char *fullname);
static char szCopyRight[] = " WinSaver, Copyright (C) Richard Hale Shaw, 1991 ";
static char szWinSaver[] = "WinSaver";
static char szAutoSave[] = "Autosave";
static char szAutoLoad[] = "Autoload";
char szAppName[MAXFILENAME];
BOOL AutoSave = FALSE;
BOOL AutoLoad = FALSE;
#define DEFAULTTIMER 1000
WORD TimerVal = DEFAULTTIMER;
HCURSOR hNormalCursor, hHourGlassCursor;
HWND WinSaver;
HANDLE hPrev;
LPSTR CmdLine;
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass;
hPrev = hPrevInstance;
if(!hPrevInstance)
{
wndclass.style = NULL;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, "WINSAVER");
wndclass.hCursor = NULL;
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szWinSaver;
RegisterClass(&wndclass);
}
hNormalCursor = LoadCursor(NULL, IDC_ARROW);
hHourGlassCursor = LoadCursor(NULL, IDC_WAIT);
WinSaver = CreateWindow(szWinSaver,
"Session Saver",
WS_OVERLAPPEDWINDOW,
// 0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
UpdateSystemMenu();
ShowWindow(WinSaver, SW_SHOWMINIMIZED);
UpdateWindow(WinSaver);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
long FAR PASCAL WndProc(HWND hWnd, WORD message,
WORD wParam, LONG lParam)
{
static BOOL done1 = FALSE;
static BOOL done2 = FALSE;
switch(message)
{
case WM_CREATE:
{
WORD w = LOBYTE(GetVersion());
if(w < 3) // if Win 1.0 or Win 2.0
{
MessageBox(hWnd,
"Sorry, WinSaver must run on Windows 3.x or higher!",
szWinSaver,
MB_ICONEXCLAMATION);
SendMessage(hWnd,WM_DESTROY,0,0L);
}
else if(hPrev) // if a previous instance
{
MessageBox(hWnd,
"WinSaver already running!",
szWinSaver,
MB_ICONEXCLAMATION);
SendMessage(hWnd,WM_DESTROY,0,0L);
}
else
SetTimer(hWnd,MYTIMER,50,NULL);
break;
}
case WM_INITMENU: // modify the system menu
CheckMenuItem(GetSystemMenu(WinSaver,0),
SC_AUTOSAVE,
(MF_BYCOMMAND | (AutoSave ? MF_CHECKED : MF_UNCHECKED)));
CheckMenuItem(GetSystemMenu(WinSaver,0),
SC_AUTOLOAD,
(MF_BYCOMMAND | (AutoLoad ? MF_CHECKED : MF_UNCHECKED)));
break;
case WM_TIMER:
{
if(!done1 && wParam == MYTIMER)
{
SetCursor(hHourGlassCursor);
KillTimer(hWnd,MYTIMER);
RestoreSession();
GetModuleFileName(GetClassWord(hWnd,GCW_HMODULE),
szAppName, MAXFILENAME-1);
SetTimer(hWnd,MYTIMER2,TimerVal,NULL);
done1 = TRUE;
}
if(!done2 && wParam == MYTIMER2)
{
char buf[20];
KillTimer(hWnd,MYTIMER2);
SetCursor(hNormalCursor);
done2 = TRUE;
}
}
break;
case WM_QUERYENDSESSION:
if(AutoSave || (MessageBox(hWnd,"Save this session?",szWinSaver,
MB_ICONQUESTION | MB_YESNO) == IDYES))
{
SetCursor(hHourGlassCursor);
GetWindowList();
SaveSession();
}
return -1L;
case WM_QUERYOPEN: // prevent icon from opening
return 0L; // into a window
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SYSCOMMAND:
switch(wParam)
{
case SC_ABOUT:
MessageBox(hWnd,szCopyRight,szWinSaver,
MB_ICONINFORMATION);
break;
case SC_AUTOLOAD:
Autoload();
break;
case SC_AUTOSAVE:
AutoSave = !AutoSave;
CheckMenuItem(GetSystemMenu(WinSaver,0),
SC_AUTOSAVE,(MF_BYCOMMAND |
(AutoSave ? MF_CHECKED : MF_UNCHECKED)));
break;
default:
goto defwindowproc;
}
break;
default:
defwindowproc:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0L;
}
void UpdateSystemMenu(void)
{
HMENU hSysMenu = GetSystemMenu(WinSaver,0);
RemoveMenu(hSysMenu, SC_RESTORE, MF_BYCOMMAND);
RemoveMenu(hSysMenu, SC_MINIMIZE, MF_BYCOMMAND);
RemoveMenu(hSysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
RemoveMenu(hSysMenu, SC_SIZE, MF_BYCOMMAND);
AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hSysMenu, MF_STRING, SC_ABOUT, "About...");
AppendMenu(hSysMenu, MF_STRING, SC_AUTOSAVE, "Autosave");
AppendMenu(hSysMenu, MF_STRING, SC_AUTOLOAD, "Autoload");
}
char outbuf[MAXWINDOWSPARM];
void Autoload(void)
{
char *p;
GetProfileString("windows","load",NULL,outbuf,sizeof(outbuf));
strupr(outbuf);
AutoLoad = !AutoLoad;
// if Auto Load is selected and app name is not in LOAD= list
if(AutoLoad && !strstr(outbuf,szAppName))
{
strcat(outbuf," "); // add blank and name to
strcat(outbuf,szAppName); // LOAD= list
WriteProfileString("windows","load",outbuf);
}
// if Auto Load is not selected and app name is in LOAD= list
if(!AutoLoad && (p = strstr(outbuf,szAppName)))
{
*p = '\0'; // NULL at start of appname
p += strlen(szAppName); // move past appname
strcat(outbuf,p);