home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 17 / bugs / pcscrlib.c < prev    next >
C/C++ Source or Header  |  1992-09-24  |  5KB  |  167 lines

  1. //========================================================================
  2. //     PCScrLib.c - Common screen saver routines
  3. //  
  4. //        Douglas Boling
  5. //
  6. //        Copyright (c) 1992 Doulgas Boling
  7. //========================================================================
  8.  
  9. #include <windows.h>
  10. #include "pcscrlib.h"
  11.  
  12. //------------------------------------------------------------------------
  13. //Function prototypes used by all screen savers
  14. //------------------------------------------------------------------------
  15. BOOL    RegisterDialogClasses (HANDLE hInst);
  16. LONG FAR PASCAL ScreenSaverProc(HWND, WORD, WORD, LONG);
  17. extern BOOL FAR PASCAL ScreenSaverConfigureDialog(HWND hDlg, WORD msg, 
  18.                                                   WORD wParam, LONG lParam);
  19. LONG FAR PASCAL DefScreenSaverProc(HWND, WORD, WORD, LONG);
  20.  
  21. //------------------------------------------------------------------------
  22. //Global Data for all screen savers
  23. //------------------------------------------------------------------------
  24. extern HANDLE    hMainInstance;
  25. extern HWND        hMainWindow;
  26. extern char        szAppName[];
  27.  
  28. //========================================================================
  29. //WinMain - This routine provides the WinMain routine for a Windows
  30. //          screen saver.
  31. //========================================================================
  32. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  33.                     LPSTR lpszCmdParam, int nCmdShow) {
  34.  
  35.    HWND            hMainWindow;
  36.    MSG            msg;
  37.    WNDCLASS        wndclass;
  38.     char            cSwitch;
  39.     FARPROC        lpfnDlgProc;
  40.  
  41.     hMainInstance = hInstance;
  42.  
  43.     if (hPrevInstance) 
  44.         return 1;
  45.  
  46.     //----------------------------------------------------------------
  47.     //If the command line has a -s, /s, or s, start screen saver
  48.     //----------------------------------------------------------------
  49.  
  50.     cSwitch = lpszCmdParam[0];
  51.     if (cSwitch == '/' || cSwitch == '-') 
  52.         cSwitch = lpszCmdParam[1];
  53.  
  54.     if (cSwitch == 's' || cSwitch == 'S') {
  55.  
  56.         wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  57.         wndclass.lpfnWndProc    = ScreenSaverProc;
  58.         wndclass.cbClsExtra     = 0;
  59.         wndclass.cbWndExtra     = 0;
  60.         wndclass.hInstance      = hInstance;
  61.         wndclass.hIcon          = LoadIcon (hInstance, MAKEINTATOM (ID_APP));
  62.         wndclass.hCursor        = NULL;
  63.         wndclass.hbrBackground  = GetStockObject(BLACK_BRUSH);
  64.         wndclass.lpszMenuName   = NULL;
  65.         wndclass.lpszClassName  = szAppName;
  66.       
  67.         if (!RegisterClass (&wndclass))
  68.             return 1;
  69.  
  70.         hMainWindow = CreateWindow (szAppName, szAppName, 
  71.                                  WS_POPUP | WS_VISIBLE, 0, 0,
  72.                                  GetSystemMetrics (SM_CXSCREEN),
  73.                                 GetSystemMetrics (SM_CYSCREEN),
  74.                                  NULL, NULL, hInstance, NULL);
  75.         if (!hMainWindow)
  76.             return 1;
  77.  
  78.     //-------------------------------------------------------------------
  79.     //Use the following code for a standard dispatch loop.
  80.     //-------------------------------------------------------------------
  81.     //    while (GetMessage(&msg,NULL,0,0)) {
  82.     //        TranslateMessage(&msg);
  83.     //        DispatchMessage(&msg);
  84.     //    }
  85.     //    return msg.wParam;
  86.  
  87.     //-------------------------------------------------------------------
  88.     //Bugs uses a modified dispatch loop to process bugs whenever 
  89.     //there isn't a message in the queue.
  90.     //-------------------------------------------------------------------
  91.         while (TRUE) {
  92.             if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  93.                 if (msg.message == WM_QUIT) break;
  94.                 TranslateMessage(&msg);
  95.                 DispatchMessage(&msg);
  96.             } else {
  97.                 BugSmarts(hMainWindow);
  98.             }
  99.         }
  100.         return msg.wParam;
  101.  
  102.     } else {
  103.     //----------------------------------------------------------------
  104.     //   If no cmd line switch, display config dialog box.
  105.     //----------------------------------------------------------------
  106.         lpfnDlgProc = MakeProcInstance(ScreenSaverConfigureDialog, 
  107.                                        hInstance);
  108.         if (RegisterDialogClasses (hInstance)) {
  109.             DialogBox (hInstance, DLG_SCRNSAVECONFIGURE, NULL, lpfnDlgProc);
  110.             FreeProcInstance (lpfnDlgProc);
  111.         }
  112.         return 0;
  113.     } 
  114. }
  115.  
  116. //========================================================================
  117. //Default Windows proc for a screen saver
  118. //
  119. //The task of the default screen saver proc is simple, quit if the user
  120. //  provides any input to the machine.
  121. //========================================================================
  122. LONG FAR PASCAL DefScreenSaverProc (HWND hwnd, WORD message, 
  123.                                     WORD wParam, LONG lParam) {
  124.     static    POINT pLastMousePos = {-1, -1};
  125.     POINT     pCurrMousePos;
  126.  
  127.     switch (message) {
  128.     
  129.         case WM_ACTIVATE:
  130.             if (wParam == 0)
  131.                 PostMessage (hwnd, WM_CLOSE, 0, 0);
  132.             else
  133.                 GetCursorPos (&pLastMousePos);
  134.             break;
  135.  
  136.         case WM_MOUSEMOVE:
  137.             SetCursor (0);
  138.             GetCursorPos (&pCurrMousePos);
  139.  
  140.             if (pLastMousePos.x == -1 || pLastMousePos.y == -1) {
  141.                 GetCursorPos (&pLastMousePos);
  142.                 break;
  143.             }
  144.             if (pCurrMousePos.x != pLastMousePos.x)
  145.                 PostMessage (hwnd, WM_CLOSE, 0, 0);
  146.  
  147.             if (pCurrMousePos.y != pLastMousePos.y)
  148.                 PostMessage (hwnd, WM_CLOSE, 0, 0);
  149.  
  150.             break;
  151.  
  152.         case WM_LBUTTONDOWN:
  153.         case WM_MBUTTONDOWN:
  154.         case WM_RBUTTONDOWN:
  155.         case WM_SYSKEYDOWN:
  156.         case WM_KEYDOWN:
  157.             PostMessage (hwnd, WM_CLOSE, 0, 0);
  158.             break;
  159.  
  160.         case WM_DESTROY:
  161.             PostQuitMessage (0);
  162.             return 0;
  163.     }
  164.     return DefWindowProc (hwnd, message, wParam, lParam);
  165. }
  166.  
  167.