home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / scrnsave / skeleton.c < prev    next >
C/C++ Source or Header  |  1997-09-11  |  9KB  |  297 lines

  1. #define WIN31
  2. #include <windows.h>
  3. #include <scrnsave.h>
  4. #include "cparrow.h"
  5. #include "comstrin.h"
  6. #include "uniconv.h"
  7.  
  8.  
  9. /* Global variables... */
  10. HANDLE  hMainInst;
  11. HWND    hMainWindow;
  12.  
  13. TCHAR   szName[TITLEBARNAMELEN];
  14. TCHAR   szAppName[APPNAMEBUFFERLEN];       // Section name in CONTROL.INI
  15. TCHAR   szIniFile[MAXFILELEN];
  16. TCHAR   szScreenSaver[22];
  17. TCHAR   szHelpFile[MAXFILELEN];
  18. TCHAR   szNoHelpMemory[BUFFLEN];
  19. UINT    MyHelpMessage;
  20.  
  21.  
  22. /* Local Function definitions... */
  23. BOOL AppInit           (HANDLE hInst, HANDLE hPrev, WORD sw, LPTSTR szCmdLine);
  24. int  DoConfigureDialog (HANDLE hInst, BOOL fParent);
  25.  
  26. HCURSOR hcurOld;
  27.  
  28. HHOOK hhkNextMsgFilterHookFunc = NULL;
  29.  
  30. LRESULT CALLBACK HelpMessageFilterHookFunction (int nCode, WPARAM wParam, LPMSG lpMsg);
  31.  
  32. #define THRESHOLD   3
  33.  
  34.  
  35. //***************************************************************************
  36.  
  37. BOOL AppInit (HANDLE hInst, HANDLE hPrev, WORD sw, LPTSTR szCmdLine)
  38. {
  39.     WNDCLASS cls;
  40.     int    dx, dy;
  41.  
  42.     if (hPrev != NULL)
  43.         return FALSE;
  44.  
  45.     /*
  46.      *  Register a class for the main application window
  47.      */
  48.     cls.hCursor        = NULL;
  49.     cls.hIcon          = LoadIcon (hInst, MAKEINTATOM (ID_APP));
  50.     cls.lpszMenuName   = NULL;
  51.     cls.lpszClassName  = TEXT("WindowsScreenSaverClass");
  52.     cls.hbrBackground  = GetStockObject (BLACK_BRUSH);
  53.     cls.hInstance      = hInst;
  54.     cls.style          = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
  55.     cls.lpfnWndProc    = ScreenSaverProc;
  56.     cls.cbWndExtra     = 0;
  57.     cls.cbClsExtra     = 0;
  58.  
  59.     if (!RegisterClass (&cls))
  60.         return FALSE;
  61.  
  62.     dx = GetSystemMetrics (SM_CXSCREEN);
  63.     dy = GetSystemMetrics (SM_CYSCREEN);
  64.     hMainWindow = CreateWindowEx (WS_EX_TOPMOST,
  65.                             TEXT("WindowsScreenSaverClass"), // Class name
  66.                             TEXT("\0"),                      // Caption
  67.                             WS_POPUP | WS_VISIBLE |          // Style bits
  68.                             WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  69.                             0, 0,                            // Position
  70.                             dx, dy,                          // Size
  71.                             (HWND)NULL,                      // Parent window (no parent)
  72.                             (HMENU)NULL,                     // use class menu
  73.                             (HANDLE)hInst,                   // handle to window instance
  74.                             (LPVOID)NULL                     // no params to pass on
  75.                                );
  76.     if (hMainWindow)
  77.         return TRUE;
  78.  
  79.     return FALSE;
  80. }
  81.  
  82.  
  83. //***************************************************************************
  84.  
  85. int _CRTAPI1 main (USHORT argc, CHAR **argv)
  86. {
  87.     HANDLE   hInst;
  88.     HANDLE   hPrev     = NULL;
  89.     LPTSTR   szCmdLine = GetCommandLine();
  90.     WORD     sw        = SW_SHOWNORMAL;
  91.     MSG      msg;
  92.     LPTSTR   lpT;
  93.  
  94.     hInst = GetModuleHandle (NULL);
  95.  
  96.     /* Save instance handle for DialogBoxs */
  97.     hMainInst = hInst;
  98.  
  99.     // This has to be loaded first so we know who we are and to find if we
  100.     // already around.
  101.  
  102.     LoadString (hInst, idsAppName, szAppName, CharSizeOf(szAppName));
  103.  
  104.     //=================================================================
  105.     // on NT, szCmdLine's first string includes its own name, remove this
  106.     // to make it exactly like the windows command line.
  107.  
  108.     if (*szCmdLine)
  109.     {
  110.         lpT = _tcschr (szCmdLine, TEXT(' '));   // skip self name
  111.         if (lpT)
  112.         {
  113.             szCmdLine = lpT;
  114.             while (*szCmdLine == TEXT(' '))
  115.                 szCmdLine++;            // skip spaces to end or first cmd
  116.         }
  117.         else
  118.         {
  119.             szCmdLine += lstrlen(szCmdLine);   // point to NULL
  120.         }
  121.     }
  122.     //=====================================================================
  123.  
  124.     MyHelpMessage = RegisterWindowMessage (szAppName);
  125.  
  126.     if (MyHelpMessage)
  127.         hhkNextMsgFilterHookFunc = SetWindowsHook (WH_MSGFILTER,
  128.                                       (HOOKPROC)HelpMessageFilterHookFunction);
  129.     /* Parse through the command line.  If the parameter is -s, /s, or s, then
  130.        bring up a configure dialog box.  Otherwise, bring up the normal
  131.        window...*/
  132.     if (!((!_tcsnicmp (szCmdLine, TEXT("/s"), 2) || !_tcsicmp (szCmdLine, TEXT("-s"))) ||
  133.         !_tcsicmp (szCmdLine, TEXT("s"))))
  134.     {
  135.         if (!((!_tcsicmp (szCmdLine, TEXT("/c")) || !_tcsicmp (szCmdLine, TEXT("-c"))) ||
  136.             !_tcsicmp (szCmdLine, TEXT("c"))))
  137.             return DoConfigureDialog (hInst, FALSE);
  138.         else
  139.             return DoConfigureDialog (hInst, TRUE);
  140.     }
  141.  
  142.     /* Call initialization procedure */
  143.     if (!AppInit (hInst, hPrev, sw, szCmdLine))
  144.         return FALSE;
  145.  
  146.     /* Message Loop for the program... */
  147.     while (GetMessage (&msg, NULL, 0, 0))
  148.     {
  149.         TranslateMessage (&msg);
  150.         DispatchMessage (&msg);
  151.     }
  152.  
  153.     if (MyHelpMessage)
  154.         UnhookWindowsHook (WH_MSGFILTER, (HOOKPROC)HelpMessageFilterHookFunction);
  155.  
  156.     return msg.wParam;
  157. }
  158.  
  159.  
  160. //***************************************************************************
  161.  
  162.  
  163. LONG DefScreenSaverProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  164. {
  165.     static BOOL     fHere = FALSE;
  166.     static POINT    ptLast;
  167.     static BOOL     bDialogUp = FALSE;
  168.     POINT           ptCursor, ptCheck;
  169.  
  170.     switch (msg)
  171.     {
  172.     case WM_SYSCOMMAND:
  173.         if ((wParam==SC_SCREENSAVE) || (wParam==SC_CLOSE))
  174.             return FALSE;
  175.         break;
  176.  
  177.     case WM_DESTROY:
  178.         PostQuitMessage (0);
  179.         break;
  180.  
  181.     case WM_SETCURSOR:
  182.         SetCursor (NULL);
  183.         break;
  184.  
  185.     case WM_NCACTIVATE:
  186.         if (wParam==FALSE && !bDialogUp)
  187.             return FALSE;
  188.         break;
  189.  
  190.     case WM_ACTIVATE:
  191.     case WM_ACTIVATEAPP:
  192.         if (wParam != FALSE)        // only fall through if we are
  193.             break;                  // losing the focus...
  194.  
  195.     case WM_MOUSEMOVE:
  196.         if (!fHere)
  197.         {
  198.             GetCursorPos (&ptLast);
  199.             fHere = TRUE;
  200.         }
  201.         else
  202.         {
  203.             GetCursorPos (&ptCheck);
  204.             if (ptCursor.x = ptCheck.x - ptLast.x)
  205.             {
  206.                 if (ptCursor.x < 0)
  207.                     ptCursor.x *= -1;
  208.             }
  209.             if (ptCursor.y = ptCheck.y - ptLast.y)
  210.             {
  211.                 if (ptCursor.y < 0)
  212.                     ptCursor.y *= -1;
  213.             }
  214.             if ((ptCursor.x + ptCursor.y) > THRESHOLD)
  215.                 goto SHOWDIALOG;
  216.         }
  217.         break;
  218.  
  219.     case WM_LBUTTONDOWN:
  220.     case WM_MBUTTONDOWN:
  221.     case WM_RBUTTONDOWN:
  222.         GetCursorPos (&ptCursor);
  223.         ptCursor.x ++;
  224.         ptCursor.y ++;
  225.         SetCursorPos (ptCursor.x, ptCursor.y);
  226.         GetCursorPos (&ptCheck);
  227.         if (ptCheck.x != ptCursor.x && ptCheck.y != ptCursor.y)
  228.             ptCursor.x -= 2;
  229.         ptCursor.y -= 2;
  230.         SetCursorPos (ptCursor.x, ptCursor.y);
  231.  
  232.         // fall thru
  233.  
  234.     case WM_KEYDOWN:
  235.     case WM_SYSKEYDOWN:
  236. SHOWDIALOG:
  237.         PostMessage (hWnd, WM_CLOSE, 0, 0l);
  238.         break;
  239.     }
  240.     return DefWindowProc (hWnd, msg, wParam, lParam);
  241. }
  242.  
  243.  
  244. //***************************************************************************
  245.  
  246. int    DoConfigureDialog (HANDLE hInst, BOOL fParent )
  247. {
  248.     HWND    hWndParent;
  249.  
  250.     if (fParent)
  251.         hWndParent = GetActiveWindow ();
  252.     else
  253.         hWndParent = NULL;
  254.  
  255.     if (RegisterDialogClasses (hInst))
  256.     {
  257.         hMainWindow = NULL;
  258.         DialogBox (hInst, MAKEINTRESOURCE (DLG_SCRNSAVECONFIGURE),
  259.                           hWndParent, (WNDPROC) ScreenSaverConfigureDialog);
  260.     }
  261.  
  262.     return TRUE;
  263. }
  264.  
  265. //***************************************************************************
  266.  
  267. LRESULT CALLBACK HelpMessageFilterHookFunction (int nCode, WPARAM wParam, LPMSG lpMsg)
  268. {
  269.  
  270.     if (nCode < 0)
  271.         goto DefHook;
  272.  
  273.     if (nCode == MSGF_DIALOGBOX)
  274.         if (lpMsg->message == WM_KEYDOWN && lpMsg->wParam == VK_F1)
  275.         {
  276.             HWND hTemp;
  277.             HWND hParent = lpMsg->hwnd;
  278.  
  279.             while (hParent != NULL)
  280.             {
  281.                 hTemp = hParent;
  282.                 if (!(GetWindowLong(hTemp, GWL_STYLE) & WS_CHILD))
  283.                     break;
  284.                 hParent = (HWND) GetWindowLong (hParent, GWL_HWNDPARENT);
  285.             }
  286.  
  287.             PostMessage (hTemp, MyHelpMessage, 0, 0L);
  288.             return TRUE;
  289.         }
  290.         else
  291. DefHook:
  292.         return (int)DefHookProc(nCode, wParam, (LONG)lpMsg, &hhkNextMsgFilterHookFunc);
  293.  
  294.     return 0;
  295. }
  296.  
  297.