home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windoware
/
WINDOWARE_1_6.iso
/
winutil
/
adg_4_6
/
scrnblnk.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-02-21
|
13KB
|
404 lines
/****************************************************************************
Module name: ScrnBlnk.C
Programmer : Jeffrey M. Richter.
*****************************************************************************/
#include "..\nowindws.h"
#undef NOCOLOR
#undef NOCTLMGR
#undef NOGDI
#undef NOHELP
#undef NOKERNEL
#undef NOLSTRING
#undef NOMB
#undef NOMENUS
#undef NOMINMAX
#undef NOMSG
#undef NORASTEROPS
#undef NOSHOWWINDOW
#undef NOSYSMETRICS
#undef NOUSER
#undef NOWH
#undef NOWINOFFSETS
#undef NOWINMESSAGES
#undef NOWINSTYLES
#include <windows.h>
#include <stdlib.h>
#include <math.h>
#include "dialog.h"
#include "SB-DLL.h"
char _szAppName[] = "ScrnBlnk";
char _szBlnkClass[] = "ScrnBlnkPopup";
HANDLE _hInstance = NULL; // Our instance handle
#define IDM_PREFERENCES (0x0110) // Must be < 0xF000
#define IDM_ABOUT (0x0120) // Must be < 0xF000
static WORD _wMinutes = 5; // Default to 5 minute idle time.
static HWND _hWnd, _hWndBlnk;
BOOL NEAR PASCAL RegisterWndClasses (HANDLE hInstance);
LONG FAR PASCAL AppWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam);
LONG FAR PASCAL BlnkWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam);
DWORD FAR PASCAL JrnlRcrdHookFunc (int nCode, WORD wParam, LPEVENTMSGMSG lpEventMsg);
BOOL FAR PASCAL PrefProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam);
BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam);
// ***************************************************************************
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) {
MSG msg;
_hInstance = hInstance;
if (hPrevInstance != NULL) return(0);
if (!RegisterWndClasses(hInstance)) return(0);
// Create application's main window. Show only as iconic (WS_MINIMIZE).
_hWnd = CreateWindow(_szAppName, _szAppName,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZE,
0, 0, 0, 0, NULL, NULL, hInstance, 0);
if (_hWnd == NULL) return(0);
ShowWindow(_hWnd, SW_MINIMIZE);
UpdateWindow(_hWnd);
// Create hidden unowned-popup window that covers user's entire screen.
_hWndBlnk = CreateWindow(_szBlnkClass, NULL,
WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, 0);
if (_hWndBlnk == NULL) return(0);
// Send WM_TIMER messages to unowned-popup window every second.
if (0 == SetTimer(_hWndBlnk, 1, 1000, NULL)) {
MessageBox(_hWnd, "Not enough timers!", _szAppName, MB_OK);
return(0);
}
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
KillTimer(_hWndBlnk, 1);
DestroyWindow(_hWndBlnk);
return(0);
}
// ***************************************************************************
BOOL NEAR PASCAL RegisterWndClasses (HANDLE hInstance) {
WNDCLASS WndClass;
WndClass.style = 0;
WndClass.lpfnWndProc = AppWndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInstance;
WndClass.hIcon = LoadIcon(hInstance, _szAppName);
WndClass.hCursor = NULL;
WndClass.hbrBackground = NULL;
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = _szAppName;
if (!RegisterClass(&WndClass)) return(0);
WndClass.style = 0;
WndClass.lpfnWndProc = BlnkWndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInstance;
WndClass.hIcon = LoadIcon(hInstance, _szAppName);
WndClass.hCursor = NULL;
// Unowned-popup window's background is always BLACK so that
// contents of screen is removed.
WndClass.hbrBackground = GetStockObject(BLACK_BRUSH);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = _szBlnkClass;
return(RegisterClass(&WndClass));
}
// ***************************************************************************
// This function processes all messages sent to ScrnBlnk's main window.
LONG FAR PASCAL AppWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
BOOL fCallDefProc = FALSE;
LONG lResult = 0;
HMENU hMenu;
FARPROC fpProc;
WORD wIdleTime;
switch (wMsg) {
case WM_CREATE:
// Append the "Preferences" and "About" options to the system menu.
hMenu = GetSystemMenu(hWnd, 0);
AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
AppendMenu(hMenu, MF_STRING, IDM_PREFERENCES, "&Preferences...");
AppendMenu(hMenu, MF_STRING, IDM_ABOUT, "A&bout...");
DrawMenuBar(hWnd);
// Install the WH_JOURNALRECORD hook filter function.
InstallJrnlHook(TRUE);
break;
case WM_DESTROY:
// Remove the WH_JOURNALRECORD hook filter function.
InstallJrnlHook(FALSE);
PostQuitMessage(0);
break;
case WM_QUERYOPEN:
// Do not allow the application to open, show only as icon.
lResult = 0;
break;
case WM_SYSCOMMAND:
switch (wParam & 0xfff0) {
case IDM_PREFERENCES:
// Prompt user for idle time in minutes.
fpProc = MakeProcInstance(PrefProc, _hInstance);
// The last parameter is the current settings of _wMinutes.
wIdleTime = DialogBoxParam(_hInstance, "Preferences",
hWnd, fpProc, _wMinutes);
FreeProcInstance(fpProc);
// If wIdleTime == -1, the user pressed "Cancel"
// else wIdleTime is the idle time.
if ((int) wIdleTime != -1) _wMinutes = wIdleTime;
break;
case IDM_ABOUT:
fpProc = MakeProcInstance(AboutProc, _hInstance);
DialogBox(_hInstance, "About", hWnd, fpProc);
FreeProcInstance(fpProc);
break;
default:
fCallDefProc = TRUE; break;
}
break;
default:
fCallDefProc = TRUE; break;
}
if (fCallDefProc)
lResult = DefWindowProc(hWnd, wMsg, wParam, lParam);
return(lResult);
}
// ***************************************************************************
#define GETRANDOM(Min, Max) ((rand() % (int)(((Max)+1) - (Min))) + (Min))
#define DEGTORAD(Deg) ((Deg * 3.14159) / 180)
LONG FAR PASCAL BlnkWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
BOOL fCallDefProc = FALSE, fForceStop = FALSE;
LONG lResult = 0;
WORD wXCenter, wYCenter, wCircle, wRadius, wTheta, wInc;
WORD wRed, wGreen, wBlue;
RECT rc;
HPEN hPen, hOldPen;
HDC hDC;
MSG msg;
switch (wMsg) {
case WM_TIMER:
// If windows is already visible, animate drawing.
if (!IsWindowVisible(hWnd)) {
// Every second, check if the idle time has elapsed.
// Is system time minus the time of the last user event
// greater than the number of minutes the user specified?
if (GetTickCount() - GetLastEventTime() > _wMinutes * 60000ul) {
ShowWindow(hWnd, SW_SHOW);
ShowCursor(0);
}
break;
}
// Animate the circle drawing in the blank window.
GetWindowRect(hWnd, &rc);
// Select random center point for circle.
wXCenter = GETRANDOM(0, rc.right - 1);
wYCenter = GETRANDOM(0, rc.bottom - 1);
// Select random radius length for circle
wRadius = GETRANDOM(rc.right / 20, rc.right - 1);
// Select random theta angle.
wInc = GETRANDOM(1, 5);
hDC = GetDC(hWnd);
SetBkMode(hDC, TRANSPARENT);
// Draw circle twice:
// 1st time: Circle is drawn in multicolors
// 2nd time: Circle is draw in all black effectively removing it.
for (wCircle = 0; !fForceStop && wCircle < 2; wCircle++) {
for (wTheta = 0; wTheta < 360; wTheta += wInc) {
// Drawing takes a long time, so check if user becomes active
// again by pressing a mouse button or key on the keyboard.
fForceStop =
PeekMessage(&msg, hWnd, WM_KEYFIRST, WM_KEYLAST,
PM_NOYIELD | PM_NOREMOVE) ||
PeekMessage(&msg, hWnd, WM_LBUTTONDOWN, WM_MOUSELAST,
PM_NOYIELD | PM_NOREMOVE);
// If either type of event (mouse or keyboard) in queue,
// stop drawing the circle prematurely.
if (fForceStop) break;
// Select a random color for the spoke of the circle.
// Do not allow a spoke to be BLACK (RGB(0, 0, 0)).
do {
wRed = GETRANDOM(0, 32);
wGreen = GETRANDOM(0, 32);
wBlue = GETRANDOM(0, 32);
} while (wRed == 0 && wGreen == 0 && wBlue == 0);
// Create a pen using a cycling style and the random color.
hPen = CreatePen((wTheta % 3), 1,
RGB(wRed * 7, wGreen * 7, wBlue * 7));
hOldPen = SelectObject(hDC, hPen);
// Start at the center of the circle and draw the spoke.
MoveTo(hDC, wXCenter, wYCenter);
LineTo(hDC,
(int) (wRadius * cos(DEGTORAD(wTheta))) + wXCenter,
(int) (wRadius * sin(DEGTORAD(wTheta))) + wYCenter);
// Restore original pen in hDC and delete the created pen.
SelectObject(hDC, hOldPen);
DeleteObject(hPen);
}
// For the 2nd time the circle is drawn, set the ROP2 code
// R2_BLACK. This causes all of the spokes to be painted black
// no matter what colors were used to create the pen.
SetROP2(hDC, R2_BLACK);
}
ReleaseDC(hWnd, hDC);
break;
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
// If any of the above events occur, the user is active and we must
// show the mouse cursor and hide our self.
ShowCursor(1);
ShowWindow(hWnd, SW_HIDE);
break;
default:
fCallDefProc = TRUE; break;
}
if (fCallDefProc)
lResult = DefWindowProc(hWnd, wMsg, wParam, lParam);
return(lResult);
}
// ***************************************************************************
// This function processes all messages sent to the Preferences dialog box.
BOOL FAR PASCAL PrefProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
BOOL fProcessed = TRUE, fTranslated;
WORD wMinutes;
switch (wMsg) {
case WM_INITDIALOG:
// The lParam parameter contains the current settings of the
// _wMinutes variable. This should be shown as the default value.
SetDlgItemInt(hDlg, ID_MINUTES, (WORD) lParam, FALSE);
break;
case WM_COMMAND:
switch (wParam) {
case IDOK:
if (HIWORD(lParam) != BN_CLICKED) break;
wMinutes =
GetDlgItemInt(hDlg, ID_MINUTES, &fTranslated, FALSE);
if (!fTranslated || wMinutes == 0) {
MessageBox(hDlg, "Invalid value for minutes.",
_szAppName, MB_OK); break;
}
// Return the new value that the user entered.
EndDialog(hDlg, wMinutes);
break;
case IDCANCEL:
if (HIWORD(lParam) != BN_CLICKED) break;
// Return -1 indicating that the user pressed "Cancel".
EndDialog(hDlg, -1);
break;
default:
break;
}
break;
default:
fProcessed = FALSE; break;
}
return(fProcessed);
}
// ***************************************************************************
// This function processes all messages sent to the About dialog box.
BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
BOOL fProcessed = TRUE;
char szBuffer[100];
switch (wMsg) {
case WM_INITDIALOG:
// Set version static window to have date and time of compilation.
wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
break;
case WM_COMMAND:
switch (wParam) {
case IDOK: case IDCANCEL:
if (HIWORD(lParam) == BN_CLICKED)
EndDialog(hDlg, wParam);
break;
default:
break;
}
break;
default:
fProcessed = FALSE; break;
}
return(fProcessed);
}