home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM User 1995 January / CDuser6Jan95.iso / WING / SAMPLES.C_ / SAMPLES.C
Text File  |  1994-06-25  |  7KB  |  300 lines

  1. #include <windows.h>
  2. #include <windowsx.h>
  3.  
  4. #include <wing.h>
  5.  
  6. //*** Creating an identity palette code here
  7.  
  8. HPALETTE CreateIdentityPalette(RGBQUAD aRGB[], int nColors)
  9. {
  10.     int i;
  11.     struct
  12.     {
  13.         WORD Version;
  14.         WORD NumberOfEntries;
  15.         PALETTEENTRY aEntries[256];
  16.     } Palette =
  17.     {
  18.         0x300,
  19.         256
  20.     };
  21.     HDC hdc = GetDC(NULL);
  22.  
  23.     //*** For SYSPAL_NOSTATIC, just copy the color table into
  24.     //*** a PALETTEENTRY array and replace the first and last entries
  25.     //*** with black and white
  26.     if (GetSystemPaletteUse(hdc) == SYSPAL_NOSTATIC)
  27.     {
  28.         //*** Fill in the palette with the given values, marking each
  29.         //*** as PC_RESERVED
  30.         for(i = 0; i < nColors; i++)
  31.         {
  32.             Palette.aEntries[i].peRed = aRGB[i].rgbRed;
  33.             Palette.aEntries[i].peGreen = aRGB[i].rgbGreen;
  34.             Palette.aEntries[i].peBlue = aRGB[i].rgbBlue;
  35.             Palette.aEntries[i].peFlags = PC_RESERVED;
  36.         }
  37.  
  38.         //*** Mark any remaining entries PC_RESERVED
  39.         for (; i < 256; ++i)
  40.         {
  41.             Palette.aEntries[i].peFlags = PC_RESERVED;
  42.         }
  43.  
  44.         //*** Make sure the last entry is white
  45.         //*** This may replace an entry in the array!
  46.         Palette.aEntries[255].peRed = 255;
  47.         Palette.aEntries[255].peGreen = 255;
  48.         Palette.aEntries[255].peBlue = 255;
  49.         Palette.aEntries[255].peFlags = 0;
  50.  
  51.         //*** And the first is black
  52.         //*** This may replace an entry in the array!
  53.         Palette.aEntries[0].peRed = 0;
  54.         Palette.aEntries[0].peGreen = 0;
  55.         Palette.aEntries[0].peBlue = 0;
  56.         Palette.aEntries[0].peFlags = 0;
  57.     }
  58.     else
  59.     //*** For SYSPAL_STATIC, get the twenty static colors into
  60.     //*** the array, then fill in the empty spaces with the
  61.     //*** given color table
  62.     {
  63.         int nStaticColors;
  64.         int nUsableColors;
  65.  
  66.         //*** Get the static colors
  67.         nStaticColors = GetDeviceCaps(hdc, NUMCOLORS);
  68.         GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
  69.  
  70.         //*** Set the peFlags of the lower static colors to zero
  71.         nStaticColors = nStaticColors / 2;
  72.         for (i=0; i<nStaticColors; i++)
  73.             Palette.aEntries[i].peFlags = 0;
  74.  
  75.         //*** Fill in the entries from the given color table
  76.         nUsableColors = nColors - nStaticColors;
  77.         for (; i<nUsableColors; i++)
  78.         {
  79.             Palette.aEntries[i].peRed = aRGB[i].rgbRed;
  80.             Palette.aEntries[i].peGreen = aRGB[i].rgbGreen;
  81.             Palette.aEntries[i].peBlue = aRGB[i].rgbBlue;
  82.             Palette.aEntries[i].peFlags = PC_RESERVED;
  83.         }
  84.  
  85.         //*** Mark any empty entries as PC_RESERVED
  86.         for (; i<256 - nStaticColors; i++)
  87.             Palette.aEntries[i].peFlags = PC_RESERVED;
  88.  
  89.         //*** Set the peFlags of the upper static colors to zero
  90.         for (i = 256 - nStaticColors; i<256; i++)
  91.             Palette.aEntries[i].peFlags = 0;
  92.     }
  93.  
  94.     ReleaseDC(NULL, hdc);
  95.  
  96.     //*** Create the palette
  97.     return CreatePalette((LOGPALETTE *)&Palette);
  98. }
  99.  
  100. //*** Resetting the system palette code here
  101.  
  102. void ClearSystemPalette(void)
  103. {
  104.     //*** A dummy palette setup
  105.     struct
  106.     {
  107.         WORD Version;
  108.         WORD NumberOfEntries;
  109.         PALETTEENTRY aEntries[256];
  110.     } Palette =
  111.     {
  112.         0x300,
  113.         256
  114.     };
  115.  
  116.     HPALETTE ScreenPalette = 0;
  117.     HDC ScreenDC;
  118.     int Counter;
  119.  
  120.     //*** Reset everything in the system palette to black
  121.     for(Counter = 0; Counter < 256; Counter++)
  122.     {
  123.         Palette.aEntries[Counter].peRed = 0;
  124.         Palette.aEntries[Counter].peGreen = 0;
  125.         Palette.aEntries[Counter].peBlue = 0;
  126.  
  127.  
  128.         Palette.aEntries[Counter].peFlags = PC_NOCOLLAPSE;
  129.     }
  130.  
  131.     //*** Create, select, realize, deselect, and delete the palette
  132.     ScreenDC = GetDC(NULL);
  133.     ScreenPalette = CreatePalette((LOGPALETTE *)&Palette);
  134.     ScreenPalette = SelectPalette(ScreenDC,ScreenPalette,FALSE);
  135.     RealizePalette(ScreenDC);
  136.     ScreenPalette = SelectPalette(ScreenDC,ScreenPalette,FALSE);
  137.     DeleteObject(ScreenPalette);
  138.     ReleaseDC(NULL, ScreenDC);
  139. }
  140.  
  141. //*** Setting up SYSPAL_NOSTATIC
  142.  
  143. #define NumSysColors (sizeof(SysPalIndex)/sizeof(SysPalIndex[1]))
  144. #define rgbBlack RGB(0,0,0)
  145. #define rgbWhite RGB(255,255,255)
  146.  
  147. //*** These are the GetSysColor display element identifiers
  148. static int SysPalIndex[] = {
  149.     COLOR_ACTIVEBORDER,
  150.     COLOR_ACTIVECAPTION,
  151.     COLOR_APPWORKSPACE,
  152.     COLOR_BACKGROUND,
  153.     COLOR_BTNFACE,
  154.     COLOR_BTNSHADOW,
  155.     COLOR_BTNTEXT,
  156.     COLOR_CAPTIONTEXT,
  157.     COLOR_GRAYTEXT,
  158.     COLOR_HIGHLIGHT,
  159.     COLOR_HIGHLIGHTTEXT,
  160.     COLOR_INACTIVEBORDER,
  161.  
  162.     COLOR_INACTIVECAPTION,
  163.     COLOR_MENU,
  164.     COLOR_MENUTEXT,
  165.     COLOR_SCROLLBAR,
  166.     COLOR_WINDOW,
  167.     COLOR_WINDOWFRAME,
  168.     COLOR_WINDOWTEXT
  169. };
  170.  
  171. //*** This array translates the display elements to black and white
  172. static COLORREF MonoColors[] = {
  173.     rgbBlack,
  174.     rgbWhite,
  175.     rgbWhite,
  176.     rgbWhite,
  177.     rgbWhite,
  178.     rgbBlack,
  179.     rgbBlack,
  180.     rgbBlack,
  181.     rgbBlack,
  182.     rgbBlack,
  183.     rgbWhite,
  184.     rgbWhite,
  185.     rgbWhite,
  186.     rgbWhite,
  187.     rgbBlack,
  188.     rgbWhite,
  189.     rgbWhite,
  190.     rgbBlack,
  191.  
  192.     rgbBlack
  193. };
  194.  
  195. //*** This array holds the old color mapping so we can restore them
  196. static COLORREF OldColors[NumSysColors];
  197.  
  198. //*** AppActivate sets the system palette use and
  199. //*** remaps the system colors accordingly.
  200. void AppActivate(BOOL fActive)
  201. {
  202.     HDC hdc;
  203.     int i;
  204.  
  205.     //*** Just use the screen DC
  206.     hdc = GetDC(NULL);
  207.  
  208.     //*** If the app is activating, save the current color mapping
  209.     //*** and switch to SYSPAL_NOSTATIC
  210.     if (fActive && GetSystemPaletteUse(hdc) == SYSPAL_STATIC)
  211.  
  212.     {
  213.         //*** Store the current mapping
  214.         for (i=0; i<NumSysColors; i++)
  215.             OldColors[i] = GetSysColor(SysPalIndex[i]);
  216.  
  217.         //*** Switch to SYSPAL_NOSTATIC and remap the colors
  218.         SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
  219.         SetSysColors(NumSysColors, SysPalIndex, MonoColors);
  220.     }
  221.     else if (!fActive && GetSystemPaletteUse(hdc) == SYSPAL_NOSTATIC)
  222.     {
  223.         //*** Switch back to SYSPAL_STATIC and the old mapping
  224.         SetSystemPaletteUse(hdc, SYSPAL_STATIC);
  225.  
  226.         SetSysColors(NumSysColors, SysPalIndex, OldColors);
  227.     }
  228.  
  229.     //*** Be sure to release the DC!
  230.     ReleaseDC(NULL,hdc);
  231. }
  232.  
  233.  
  234. //*** Creating an offscreen buffer (WinGCreateBitmap)
  235.  
  236. HBITMAP ghBitmapMonochrome = 0;
  237.  
  238. HDC Create100x100WinGDC(void)
  239. {
  240.     HDC hWinGDC;
  241.     HBITMAP hBitmapNew;
  242.     struct {
  243.         BITMAPINFOHEADER InfoHeader;
  244.         RGBQUAD ColorTable[256];
  245.     } Info;
  246.     void far *pSurfaceBits;
  247.  
  248.     // Set up an optimal bitmap
  249.     if (WinGRecommendDIBFormat((BITMAPINFO far *)&Info) == FALSE)
  250.         return 0;
  251.  
  252.     // Set the width and height of the DIB but preserve the
  253.     // sign of biHeight in case top-down DIBs are faster
  254.  
  255.     // NOTE: Changed 100 to 256 for my purposes...
  256.     Info.InfoHeader.biHeight *= 256;
  257.  
  258.     Info.InfoHeader.biWidth = 256;
  259.  
  260.     //*** DONT FORGET A COLOR TABLE! ***
  261.     //*** COLOR TABLE CODE HERE ***
  262.  
  263.     // Create a WinGDC and Bitmap, then select away
  264.     hWinGDC = WinGCreateDC();
  265.     if (hWinGDC)
  266.     {
  267.         hBitmapNew = WinGCreateBitmap(hWinGDC,
  268.             (BITMAPINFO far *)&Info, &pSurfaceBits);
  269.         if (hBitmapNew)
  270.         {
  271.             ghBitmapMonochrome = (HBITMAP)SelectObject(hWinGDC,
  272.                 hBitmapNew);
  273.         }
  274.         else
  275.         {
  276.             DeleteDC(hWinGDC);
  277.             hWinGDC = 0;
  278.  
  279.         }
  280.     }
  281.  
  282.     return hWinGDC;
  283. }
  284.  
  285. void Destroy100x100WinGDC(HDC hWinGDC)
  286. {
  287.     HBITMAP hBitmapOld;
  288.  
  289.     if (hWinGDC && ghBitmapMonochrome)
  290.     {
  291.         // Select the stock 1x1 monochrome bitmap back in
  292.         hBitmapOld = (HBITMAP)SelectObject(hWinGDC,                 
  293.         ghBitmapMonochrome);
  294.         DeleteObject(hBitmapOld);
  295.         DeleteDC(hWinGDC);
  296.     }
  297. }
  298.  
  299.  
  300.