home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM User 1995 January / CDuser6Jan95.iso / DYNASTY / WING / PALANIM.C_ / PALANIM.C
C/C++ Source or Header  |  1994-06-25  |  24KB  |  744 lines

  1. /*
  2.  *    PALANIM.C
  3.  *
  4.  *    (C) Copyright Microsoft Corp. 1994.  All rights reserved.
  5.  *
  6.  *    You have a royalty-free right to use, modify, reproduce and 
  7.  *    distribute the Sample Files (and/or any modified version) in 
  8.  *    any way you find useful, provided that you agree that 
  9.  *    Microsoft has no warranty obligations or liability for any 
  10.  *    Sample Application Files which are modified. 
  11.  */
  12.  
  13. #include <windows.h>
  14. #include <commdlg.h>
  15. #include <wing.h>
  16.  
  17. #include "dib.h"
  18. #include "palanim.h"
  19.  
  20. /*----------------------------------------------------------------------------*\
  21. |                                                                              |
  22. |   g l o b a l   v a r i a b l e s                                            |
  23. |                                                                              |
  24. \*----------------------------------------------------------------------------*/
  25. static  char    szAppName[]="WinG Palette Animation App";
  26.  
  27. static  HINSTANCE hInstApp;
  28. static  HWND      hwndApp;
  29. static    HPALETTE  hpalApp;
  30. static    BOOL      fAppActive;
  31.  
  32. static HDC hdcOffscreen;
  33. void far *gpBits;
  34. struct {
  35.     BITMAPINFOHEADER InfoHeader;
  36.     RGBQUAD ColorTable[256];
  37. } gInfo;
  38.  
  39. int fAnimatePalette = 0;        // Don't animate
  40. int fIncludeStatic = 0;            // Use the static color entries
  41. enum {Red, Green, Blue} gWashColor = Red;
  42.  
  43. PALETTEENTRY aPalette[256];
  44.  
  45. extern HBITMAP ghBitmapMonochrome;
  46.  
  47. #ifdef WIN32
  48.     #define _export
  49. #endif
  50.  
  51. /*----------------------------------------------------------------------------*\
  52. |                                                                              |
  53. |   f u n c t i o n   d e f i n i t i o n s                                    |
  54. |                                                                              |
  55. \*----------------------------------------------------------------------------*/
  56.  
  57. LONG FAR PASCAL _export AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
  58. int  ErrMsg (LPSTR sz,...);
  59. LONG AppCommand (HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
  60.  
  61. void AppExit(void);
  62. BOOL AppIdle(void);
  63.  
  64. /***************************************************************************
  65.     Internal functions for the animation
  66. */
  67.  
  68. void CreateWashPalette(void);
  69. void DibCreateWash(BITMAPINFOHEADER far *Info, void far *pBits);
  70. void DibHorizontalLine(BITMAPINFOHEADER far *Info, void far *pBits,
  71.     int y, char unsigned color);
  72.  
  73. /***************************************************************************
  74.     Sample functions from wing.hlp
  75. */
  76.  
  77. HDC Create100x100WinGDC(void);
  78. void Destroy100x100WinGDC(HDC hWinGDC);
  79.  
  80. void AppActivate(BOOL fActive);
  81.  
  82. void ClearSystemPalette(void);
  83. HPALETTE CreateIdentityPalette(RGBQUAD aRGB[], int nColors);
  84.  
  85. /*----------------------------------------------------------------------------*\
  86. |   AppAbout( hDlg, uiMessage, wParam, lParam )                                |
  87. |                                                                              |
  88. |   Description:                                                               |
  89. |       This function handles messages belonging to the "About" dialog box.    |
  90. |       The only message that it looks for is WM_COMMAND, indicating the use   |
  91. |       has pressed the "OK" button.  When this happens, it takes down         |
  92. |       the dialog box.                                                        |
  93. |                                                                              |
  94. |   Arguments:                                                                 |
  95. |       hDlg            window handle of about dialog window                   |
  96. |       uiMessage       message number                                         |
  97. |       wParam          message-dependent                                      |
  98. |       lParam          message-dependent                                      |
  99. |                                                                              |
  100. |   Returns:                                                                   |
  101. |       TRUE if message has been processed, else FALSE                         |
  102. |                                                                              |
  103. \*----------------------------------------------------------------------------*/
  104. BOOL FAR PASCAL _export AppAbout(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  105. {
  106.     switch (msg)
  107.     {
  108.         case WM_COMMAND:
  109.         if (LOWORD(wParam) == IDOK)
  110.             {
  111.                 EndDialog(hwnd,TRUE);
  112.             }
  113.             break;
  114.  
  115.         case WM_INITDIALOG:
  116.             return TRUE;
  117.     }
  118.     return FALSE;
  119. }
  120.  
  121. /*----------------------------------------------------------------------------*\
  122. |   AppInit( hInst, hPrev)                                                     |
  123. |                                                                              |
  124. |   Description:                                                               |
  125. |       This is called when the application is first loaded into               |
  126. |       memory.  It performs all initialization that doesn't need to be done   |
  127. |       once per instance.                                                     |
  128. |                                                                              |
  129. |   Arguments:                                                                 |
  130. |       hInstance       instance handle of current instance                    |
  131. |       hPrev           instance handle of previous instance                   |
  132. |                                                                              |
  133. |   Returns:                                                                   |
  134. |       TRUE if successful, FALSE if not                                       |
  135. |                                                                              |
  136. \*----------------------------------------------------------------------------*/
  137. BOOL AppInit(HINSTANCE hInst,HINSTANCE hPrev,int sw,LPSTR szCmdLine)
  138. {
  139.     WNDCLASS cls;
  140.     int      dx,dy;
  141.     HBITMAP hbmOffscreen;
  142.     HMENU hMenu;
  143.     HDC Screen;
  144.  
  145.     /* Save instance handle for DialogBoxs */
  146.     hInstApp = hInst;
  147.  
  148.     /* Refuse to run if this is a non-palettized device */
  149.     Screen = GetDC(0);
  150.     if (Screen)
  151.     {
  152.         int PaletteDevice = GetDeviceCaps(Screen, RASTERCAPS) & RC_PALETTE;
  153.         ReleaseDC(0, Screen);
  154.         if (!PaletteDevice)
  155.         {
  156.             MessageBox(0,
  157.                 "Palette animation requires a palettized display device!",
  158.                 "Non-palettized Display",
  159.                 MB_OK);
  160.             return FALSE;
  161.         }
  162.     }
  163.  
  164.     if (!hPrev)
  165.     {
  166.         /*
  167.          *  Register a class for the main application window
  168.          */
  169.         cls.hCursor        = LoadCursor(NULL,IDC_ARROW);
  170.         cls.hIcon          = LoadIcon(hInst,"AppIcon");
  171.         cls.lpszMenuName   = "AppMenu";
  172.         cls.lpszClassName  = szAppName;
  173.         cls.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  174.         cls.hInstance      = hInst;
  175.         cls.style          = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
  176.         cls.lpfnWndProc    = (WNDPROC)AppWndProc;
  177.         cls.cbWndExtra     = 0;
  178.         cls.cbClsExtra     = 0;
  179.  
  180.         if (!RegisterClass(&cls))
  181.             return FALSE;
  182.     }
  183.  
  184.     dx = GetSystemMetrics (SM_CXSCREEN) / 2;
  185.     dy = GetSystemMetrics (SM_CYSCREEN) / 2;
  186.  
  187.     hwndApp = CreateWindow (szAppName,    // Class name
  188.                             szAppName,              // Caption
  189.                             WS_OVERLAPPEDWINDOW,    // Style bits
  190.                             CW_USEDEFAULT, 0,       // Position
  191.                 dx,dy,            // Size
  192.                             (HWND)NULL,             // Parent window (no parent)
  193.                             (HMENU)NULL,            // use class menu
  194.                             hInst,                  // handle to window instance
  195.                             (LPSTR)NULL             // no params to pass on
  196.                            );
  197.     ShowWindow(hwndApp,sw);
  198.  
  199.     //*** Create the WinGDC  (actually 256x256)
  200.     hdcOffscreen = Create100x100WinGDC();
  201.  
  202.     //*** Check the menu to reflect initial palette (red)
  203.     hMenu =