home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1990 / number3 / ms.c < prev    next >
Text File  |  1990-06-13  |  14KB  |  370 lines

  1. /**********************************************
  2.      Monitor Saver For Microsoft Windows 3.0
  3.  (c) Copyright 1990 By Moon Valley Software Inc.
  4. ***********************************************/
  5.  
  6. #include <windows.h>   /* Required for all Windows applications */
  7. #include "ms.h"        /* Specific to this program */
  8. #include <stdlib.h>    /* Needed for random */
  9. #define TIMER_QSEC 1   /* Timer ID for 1/4 sec intervals */
  10. #define TIMER_ICON 2   /* Timer for moving cursor on the screen */
  11. #define T_SEC 250      /* 1/4 second interval timer */
  12. #define MAXMINUTES 15  /* Default delay in minutes */
  13.  
  14. /********************
  15.  Global Variables
  16. ********************/
  17.  
  18. HANDLE hInst;                  /* Current instance */
  19. RECT winrect;                  /* Client area */
  20. WNDCLASS  wc;                  /* Window class */
  21. HCURSOR savcurs,actcurs;       /* Handle to cursor */
  22. HWND hWnd,prevwnd;             /* Handles to windows */
  23. HDC hDC;                       /* Handle to device context */
  24. PAINTSTRUCT pc;                /* Paint client area */
  25. int x=0,y=0,t_paint=0;         /* Window coordinates to paint */
  26.                                /* Time and icon vars */
  27. int maxmin=MAXMINUTES,minutes,restored=1,qsecs,isicon;
  28. int xScreen, yScreen;          /* x and y window coordinates */
  29. POINT oldpos,curpos;           /* Area of cursor location */
  30. char szAppName[] = "Monitor Saver";
  31.  
  32. /* Forward Declarations */
  33. long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  34. BOOL InitApplication(HANDLE);
  35. BOOL InitInstance(HANDLE, int);
  36. BOOL FAR PASCAL About(HWND, unsigned, WORD, LONG);
  37. BOOL FAR PASCAL Input(HWND, unsigned, WORD, LONG);
  38. DWORD FAR PASCAL KeyHook(int, WORD, LONG);
  39. FARPROC lpKeyHook, lpOldHook;
  40.  
  41. /******************************************************************
  42.  FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  43.  PURPOSE: calls initialization function, processes message loop
  44. *******************************************************************/
  45.  
  46. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  47. HANDLE hInstance;        /* Current instance */
  48. HANDLE hPrevInstance;    /* Previous instance */
  49. LPSTR lpCmdLine;         /* Command line */
  50. int nCmdShow;            /* Show window type Full or Iconic */
  51.  
  52.  {
  53.    MSG msg;   /* Windows message  */
  54.               /* Test for an existing instance */
  55.               /* Only one instance is allowed  */
  56.        if(hPrevInstance) {
  57.         MessageBox(hWnd,"Monitor Saver Is Already Active!",
  58.        szAppName,MB_ICONEXCLAMATION | MB_OK);
  59.     return(FALSE);
  60. }
  61.      if (!hPrevInstance)
  62.        if  (!InitApplication(hInstance)) /* Init shared resources   */
  63.            return (FALSE);               /* Exit if unable to init  */
  64.        /* Perform initialazations that apply to a specific instance */
  65.        if (!InitInstance(hInstance, nCmdShow))
  66.        return (FALSE);
  67.  
  68.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  69.            /* Dispatch messages until WM_QUIT is received */
  70.            TranslateMessage(&msg);
  71.            DispatchMessage(&msg);
  72.    }
  73.  
  74.     return (msg.wParam);  /* Returns value from PostQuitMessage */
  75. }
  76.  
  77. /******************************************************************
  78.   FUNCTION: InitApplication(HANDLE)
  79.   PURPOSE: Initialize window data and registers window class
  80. ******************************************************************/
  81.  
  82. BOOL InitApplication(hInstance)
  83. HANDLE hInstance;                       /* Current instance */
  84. {
  85.    wc.style = NULL;                     /* Class style(s) */
  86.    wc.lpfnWndProc = MainWndProc;        /* Func to receive msg's */
  87.                                         /*  for this class */
  88.    wc.cbClsExtra = 0;                   /* No per-class extra data */
  89.    wc.cbWndExtra = 0;                   /* No per-window data */
  90.    wc.hInstance = hInstance;            /* App that owns window class */
  91.    wc.hIcon = LoadIcon(hInstance,"Ready"); /* Icon for when minimized */
  92.    wc.hCursor = NULL;                      /* Cursor to use */
  93.    wc.hbrBackground = GetStockObject(WHITE_BRUSH);  /* Bkground color */
  94.    wc.lpszMenuName =  "MsMenu";          /* Menu name in rc file */
  95.    wc.lpszClassName = szAppName;         /* Name used in call window */
  96.  
  97.    /* Register the windows class and return success/failure */
  98.    return (RegisterClass(&wc));
  99. }
  100.  
  101. /*******************************************************************
  102.  FUNCTION:  InitInstance(HANDLE, int)
  103.  PURPOSE:  Saves instance handle and creates main window
  104. *******************************************************************/
  105.  
  106. BOOL InitInstance(hInstance, nCmdShow)
  107.     HANDLE  hInstance;  /* Current instance */
  108.     int     nCmdShow;   /* Param for first ShowWindow() call */
  109. {
  110.    HWND     hWnd;       /* Main win handle */
  111.    hInst = hInstance;   /* Save the instance handle in a static var */
  112.     xScreen = GetSystemMetrics(SM_CXSCREEN); /* Get current sys. info */
  113.     yScreen = GetSystemMetrics(SM_CYSCREEN);
  114.     /* Cursor to use when screen is blank */
  115.     actcurs = LoadCursor(hInstance,"Active");
  116.     hWnd = CreateWindow(     /* Create main win for this instance */
  117.          szAppName,        /* Registered class */
  118.         "Monitor Saver",     /* Caption Bar Text */
  119.          WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
  120.          CW_USEDEFAULT,       /* Initial x Pos */
  121.          CW_USEDEFAULT,       /* Initial y Pos */
  122.          165,                 /* Initial x size */
  123.          37,                  /* Initial y size */
  124.          NULL,                /* Overlapped have no parent win */
  125.          NULL,                /* Use the window class menu */
  126.          hInstance,           /* This instance owns the window */
  127.          NULL);               /* Pointer not needed */
  128.  
  129.    if (!hWnd)                 /* If window could not be created */
  130.       return (FALSE);         /* Fail */
  131.                          /* Show window in minimized active state */
  132.     ShowWindow(hWnd,SW_SHOWMINNOACTIVE);
  133.     UpdateWindow(hWnd);  /* Sends WM_PAINT Message to window */
  134.                          /* Only 16 timers total are permitted */
  135.     while(!SetTimer(hWnd,TIMER_QSEC,T_SEC,NULL))
  136.        if(IDCANCEL == MessageBox(hWnd,"Too Many Clocks Or Timers!",
  137.           szAppName,MB_ICONEXCLAMATION | MB_RETRYCANCEL)) /* Error box */
  138.     return(FALSE);                 /* Fail */
  139.  
  140.     lpKeyHook = MakeProcInstance((FARPROC)KeyHook,hInst);
  141.     /* catch all keyboard messages: */
  142.     lpOldHook = SetWindowsHook(WH_KEYBOARD,lpKeyHook);
  143.      return (TRUE); /* Return value from PostQuitMessage */
  144.  
  145. }
  146.  
  147. /*******************************************************************
  148.  FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  149.  PURPOSE:  Processes messages
  150. *******************************************************************/
  151.  
  152. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  153. HWND hWnd;
  154. unsigned message;
  155. WORD wParam;
  156. LONG lParam;
  157. {
  158.  
  159.   FARPROC lpProcAbout,lpProcInput;
  160.   DWORD savcol;
  161.  
  162.   switch (message) {
  163.     case WM_CREATE:
  164.          winrect.right = xScreen;
  165.          winrect.bottom = yScreen;
  166.                                    /* Check win.ini for delay time */
  167.          maxmin = GetProfileInt(szAppName,"delay",MAXMINUTES);
  168.          break;
  169.  
  170.   case WM_COMMAND:
  171.        switch(wParam) {
  172.  
  173.   case SET_TIMER:
  174.        lpProcInput = MakeProcInstance(Input, hInst);
  175.        DialogBox(hInst,"Input",hWnd,lpProcInput);
  176.        FreeProcInstance(lpProcInput);
  177.        break;
  178.  
  179.   case ABOUT:
  180.        lpProcAbout = MakeProcInstance(About, hInst);
  181.        DialogBox(hInst,"About",hWnd,lpProcAbout);
  182.        FreeProcInstance(lpProcAbout);
  183.        break;
  184.        }
  185.        break;
  186.  
  187.   case WM_KEYDOWN:
  188.   case WM_SYSKEYDOWN:
  189.   case WM_LBUTTONDOWN:
  190.   case WM_RBUTTONDOWN:
  191.        if(!restored) {
  192.           hDC = GetWindowDC(hWnd); /* Get handle to the device context */
  193.           FillRect(hDC,&winrect,GetStockObject(WHITE_BRUSH));
  194.           ValidateRect(hWnd,NULL);
  195.           ReleaseDC(hWnd,hDC);     /* Release the device context */
  196.  
  197.           if(isicon) {             /* If window was minimized */
  198.              MoveWindow(hWnd, 0, 0, 165, 37,  1);
  199.              ShowWindow(hWnd,SW_SHOWMINNOACTIVE);     /* Minimize it */
  200.              SetClassWord(hWnd,GCW_HICON,LoadIcon(hInst,"Restore"));
  201.              UpdateWindow(hWnd);
  202.  
  203.         } else {
  204.             MoveWindow(hWnd, 0, 0, 165, 37, 1);
  205.             ShowWindow(prevwnd, SW_SHOWMINNOACTIVE); /* Show window */
  206.             UpdateWindow(hWnd);                      /* Update it */
  207.         }
  208.       SetCursor(savcurs);        /* Restore cursor */
  209.       restored = 1;              /* Show the screen as being restored */
  210.       KillTimer(hWnd,TIMER_ICON);/* Re-set movement timer */
  211.       minutes = qsecs = 0;
  212.       SetActiveWindow(prevwnd);  /* Restore previous window */
  213.       ShowWindow(hWnd,SW_HIDE);  /* Return focus to last App */
  214.       /* Redisplay window from hidden: */
  215.       ShowWindow(hWnd,SW_SHOWMINNOACTIVE);
  216.       UpdateWindow(hWnd);        /* Update the window */
  217.    }
  218.    return (DefWindowProc(hWnd, message, wParam, lParam));
  219.  
  220.   case WM_TIMER:
  221.        switch(wParam) {
  222.  
  223.   case TIMER_QSEC:
  224.        GetCursorPos(&curpos);
  225.         if(curpos.x == oldpos.x && curpos.y == oldpos.y) {
  226.            qsecs++;         /* If cursor's not moved, add to counter */
  227.         if(qsecs >= 240) {  /* If counter is more then 1 minute */
  228.            minutes++;       /* Add one to minutes counter */
  229.            qsecs = 0;
  230.         }
  231.  
  232.  if(minutes >= maxmin) {             /* If minutes >= timeout limit? */
  233.    if((curpos.x == oldpos.x && curpos.y == oldpos.y) && restored) {
  234.      if((isicon = IsIconic(hWnd)))   /* Is it an icon? */
  235.          OpenIcon(hWnd);             /* Restore icon */
  236.          prevwnd = SetActiveWindow(hWnd);  /* Make this window active */
  237.          MoveWindow(hWnd,0,0,9900,9900,1); /* Make screen blanker win*/
  238.          hDC = GetWindowDC(hWnd);          /* Get device context */
  239.                                  /* Fill the entire window with black */
  240.          FillRect(hDC,&winrect,GetStockObject(BLACK_BRUSH));
  241.          ValidateRect(hWnd,NULL);
  242.          ReleaseDC(hWnd,hDC);
  243.          savcurs = SetCursor(actcurs); /* Change cursor */
  244.          restored = 0;   /* Display the screen as being blanked */
  245.          /* Start 5 sec cursor timer: */
  246.          SetTimer(hWnd,TIMER_ICON,5000,NULL);
  247.          oldpos.x = curpos.x = x = 20 + (rand() % (winrect.right - 64));
  248.          oldpos.y = curpos.y = y = 50 + (rand() % (winrect.bottom - 96));
  249.          SetCursor(actcurs);      /* Change cursor to screen saver */
  250.          SetCursorPos(x,y);       /* Draw cursor on screen */
  251.          minutes = qsecs = 0;
  252.          }
  253.       }
  254.   } else {
  255.     if(!restored)
  256.        SendMessage(hWnd,WM_RBUTTONDOWN,0,(DWORD)0);
  257.        oldpos.x = curpos.x;
  258.        oldpos.y = curpos.y;
  259.        qsecs = minutes = 0;
  260.        }
  261.        break;
  262.  
  263.  
  264.   case TIMER_ICON:   /* Timer to place cursor at random locations */
  265.        if(!IsIconic(hWnd)) {  /* If not minimized ... */
  266.           oldpos.x = curpos.x = x = 20 + (rand() % (winrect.right - 64));
  267.           oldpos.y = curpos.y = y = 50 + (rand() % (winrect.bottom - 96));
  268.           SetCursor(actcurs); /* Change cursor to screen saver */
  269.           SetCursorPos(x,y);  /* Draw cursor on screen */
  270.        }
  271.      break;
  272.    }
  273.   break;
  274.  
  275.   case WM_DESTROY:
  276.        KillTimer(hWnd,TIMER_QSEC);
  277.        /* Be SURE to unhook the keyboard or Windows Will CRASH with the */
  278.        UnhookWindowsHook(WH_KEYBOARD,lpKeyHook);  /* next keystroke! */
  279.        PostQuitMessage(0);
  280.        break;
  281.  
  282.        default:
  283.        return (DefWindowProc(hWnd, message, wParam, lParam));
  284.     }
  285.     return (0L);
  286. }
  287.  
  288. /*****************
  289.  The "About Box"
  290. ******************/
  291.  
  292. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  293. HWND hDlg;
  294. unsigned message;
  295. WORD wParam;
  296. LONG lParam;
  297. {
  298. int i;
  299.  
  300. switch (message) {
  301.   case WM_INITDIALOG:
  302.        return (TRUE);
  303.  
  304.   case WM_COMMAND:
  305.        if (wParam == OK_BUT) {
  306.            EndDialog(hDlg, TRUE);
  307.            return (TRUE);
  308.            }
  309.            break;
  310.       }
  311.            return (FALSE);
  312. }
  313.  
  314. /**********************************************
  315.   Get Input From User To Adjust Delay Time.
  316. ***********************************************/
  317.  
  318. BOOL FAR PASCAL Input(hDlg, message, wParam, lParam)
  319. HWND hDlg;
  320. unsigned message;
  321. WORD wParam;
  322. LONG lParam;
  323. {
  324. int i;
  325.  
  326. switch (message) {
  327.   case WM_INITDIALOG:
  328.        SetFocus(hDlg);
  329.        /* Place delay time minute field: */
  330.        SetDlgItemInt(hDlg,E_TEXT,maxmin,0);
  331.        return (TRUE);
  332.  
  333.  case WM_COMMAND:
  334.       if (wParam == OK_BUT
  335.           || wParam == CANCEL) {
  336.  
  337.       if(wParam == OK_BUT) {
  338.          /* Get new delay value: */
  339.          maxmin = GetDlgItemInt(hDlg,E_TEXT,&i,0);
  340.          maxmin = (maxmin > 0)? maxmin : 1;
  341.          }
  342.          EndDialog(hDlg, TRUE);
  343.          return (TRUE);
  344.          }
  345.        break;
  346.      }
  347.       return (FALSE);
  348. }
  349.  
  350. /********************************************************************
  351. FUNCTION: KeyHook
  352. PURPOSE:  Captures keyboard messages to insure screen will not blank
  353.           prematurely
  354. *********************************************************************/
  355.  
  356. DWORD FAR PASCAL KeyHook(iCode,wParam,lParam)
  357. int iCode;
  358. WORD wParam;
  359. LONG lParam;
  360. {
  361.    if(iCode == HC_ACTION)
  362.       /* If this is negative reset counter to zero: */
  363.       if(HIWORD(lParam) & 0x8000)
  364.          qsecs = minutes = 0;      /* Reset counters to 0 */
  365.  
  366.    /* Make sure to return this value! */
  367.    return DefHookProc(iCode,wParam,lParam, &lpOldHook);
  368. }
  369.  
  370.