home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / PALETTE / PALETTE.C_ / PALETTE.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  7.3 KB  |  297 lines

  1. /*
  2.     palette.c
  3.  
  4.     This is a shell for a Windows 3.1 app
  5.  
  6. */
  7.  
  8. #include <windows.h>
  9. #include "palette.h"
  10.  
  11. #define PALSIZE 256
  12.  
  13. /* useful global things */
  14.  
  15. HANDLE hInst;                    /* global instance handle */
  16. char szAppName[10];                /* app name */
  17. HWND hMainWnd;                    /* handle of main window */
  18. int iXcells, iYcells;           /* number of cells on each axis */
  19. int iWidth, iHeight;
  20. NPLOGPALETTE pLogPal;
  21. HPALETTE hPal;
  22. BOOL bCaptured = FALSE;         /* mouse capture flag */
  23.  
  24. /***************** local function predecs ***************************/
  25.  
  26. int get_xcells(int xsize, int ysize);
  27. void show_pixel(LONG lParam);
  28.  
  29. /***************** Main entry point routine *************************/
  30.  
  31. int PASCAL WinMain(hInstance,hPrevInstance,lpszCmdLine,cmdShow)
  32. HANDLE hInstance,hPrevInstance;
  33. LPSTR lpszCmdLine;
  34. int cmdShow;
  35. {
  36.     MSG msg;
  37.  
  38.     hInst = hInstance;               /* save our instance handle */
  39.  
  40.     LoadString(hInstance, IDS_APPNAME, szAppName, sizeof(szAppName));
  41.     if (!hPrevInstance) {
  42.         if (! InitFirstInstance(hInstance)) {
  43.             return 1;
  44.         }
  45.     }
  46.  
  47.     hMainWnd = CreateWindow(szAppName,                /* class name */
  48.                         szAppName,                /* caption text */
  49.                         WS_OVERLAPPEDWINDOW,    /* window style */
  50.                         CW_USEDEFAULT,
  51.                         0,
  52.                         GetSystemMetrics(SM_CXSCREEN) / 2,
  53.                         GetSystemMetrics(SM_CYSCREEN) / 4,
  54.                         (HWND)NULL,                /* handle of parent window */
  55.                         (HMENU)NULL,            /* menu handle (default class) */
  56.                         hInstance,                 /* handle to window instance */
  57.                         (LPSTR)NULL                /* no params to pass on */
  58.                         );
  59.  
  60.     if (!hMainWnd) {
  61.         return 1;
  62.     }
  63.  
  64.     ShowWindow(hMainWnd, cmdShow); /* display window as open or icon */
  65.     UpdateWindow(hMainWnd);        /* paint it */
  66.  
  67.     /* check for messages from Windows and process them */
  68.     /* if no messages, perform some idle function */
  69.  
  70.     do {
  71.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  72.             /* got a message to process */
  73.             if (msg.message == WM_QUIT) break;
  74.             TranslateMessage(&msg);
  75.             DispatchMessage(&msg);
  76.         } else {
  77.             /* perform some idle routine */
  78.         }
  79.     } while (1);
  80.  
  81. #ifdef PROFILE
  82.     ProfFinish();
  83. #endif
  84.     return (msg.wParam);
  85. }
  86.  
  87. /************* main window message handler ******************************/
  88.  
  89. long EXPORT MainWndProc(hWnd, message, wParam, lParam)
  90. HWND hWnd;
  91. UINT message;
  92. WPARAM wParam;
  93. LPARAM lParam;
  94. {
  95.     HANDLE hPalMem;
  96.     HDC hDC;
  97.     PAINTSTRUCT ps;                /* paint structure */
  98.     int i;
  99.  
  100.     /* process any messages we want */
  101.  
  102.     switch(message) {
  103.     case WM_CREATE:
  104.         /*
  105.             We don't bother to check for palette support as
  106.             we want the app to run even if the display doesn't
  107.             have a palette
  108.         */
  109.  
  110.         /* create a logical palette to play with */
  111.         hPalMem = LocalAlloc(LMEM_FIXED,
  112.                           sizeof(LOGPALETTE)
  113.                           + PALSIZE * sizeof(PALETTEENTRY));
  114.         if (!hPalMem) {
  115.             Error("No memory for palette");
  116.             return -1;
  117.         }
  118.  
  119.         pLogPal = (NPLOGPALETTE) LocalLock(hPalMem);
  120.         pLogPal->palVersion = 0x300; /* bloody mysterious */
  121.         pLogPal->palNumEntries = PALSIZE;
  122.         for (i=0; i<PALSIZE; i++) {
  123.             pLogPal->palPalEntry[i].peRed = (BYTE)i;
  124.             pLogPal->palPalEntry[i].peGreen = 0;
  125.             pLogPal->palPalEntry[i].peBlue = 0;
  126.             pLogPal->palPalEntry[i].peFlags = PC_EXPLICIT;
  127.         }
  128.         hPal = CreatePalette((LPLOGPALETTE)pLogPal);
  129.         if (!hPal) {
  130.             Error("CreatePalette() failed");
  131.             return -1;
  132.         }
  133.  
  134.         break;
  135.  
  136.     case WM_SIZE:
  137.  
  138.         /* remember the window size for repaint later */
  139.         iWidth = LOWORD(lParam);
  140.         iHeight = HIWORD(lParam);
  141.  
  142.         /* compute the number of cells on each axis to keep aspect good */
  143.         iXcells = get_xcells(iWidth, iHeight);
  144.         iYcells = PALSIZE / iXcells;
  145.  
  146.         break;
  147.  
  148.     case WM_PAINT:
  149.         BeginPaint(hWnd, &ps);
  150.         Paint(hWnd, ps.hdc);
  151.         EndPaint(hWnd, &ps);
  152.          break;
  153.  
  154.     case WM_PALETTECHANGED:
  155.         if (wParam != hWnd) {
  156.             hDC = GetDC(hWnd);
  157.             SelectPalette(hDC, hPal, 0);
  158.             if (RealizePalette(hDC)) {
  159.                 /* some colors changed */
  160.                 InvalidateRect(hWnd, NULL, TRUE);
  161.                 MessageBeep(0);
  162.             }
  163.             ReleaseDC(hWnd, hDC);
  164.         }
  165.         break;
  166.  
  167.     case WM_LBUTTONDOWN:
  168.         SetCapture(hWnd);       /* grab the mouse input */
  169.         bCaptured = TRUE;
  170.         show_pixel(lParam);     /* show pixel value here */
  171.         break;
  172.  
  173.     case WM_MOUSEMOVE:
  174.         if (bCaptured) {
  175.             show_pixel(lParam); /* show pixel value here */
  176.         }
  177.         break;
  178.  
  179.     case WM_LBUTTONUP:
  180.         if (bCaptured) {
  181.             ReleaseCapture();
  182.             bCaptured = FALSE;
  183.             SetWindowText(hWnd, szAppName);
  184.         }
  185.         break;
  186.  
  187.     case WM_DESTROY:
  188.         if (hPal) {
  189.             DeleteObject(hPal);
  190.         }
  191.  
  192.         PostQuitMessage(0);
  193.         break;
  194.  
  195.     default:
  196.         return DefWindowProc(hWnd, message, wParam, lParam);
  197.         break;
  198.     }
  199.     return NULL;
  200. }
  201.  
  202. void Paint(HWND hWnd, HDC hDC)
  203. {
  204.     HBRUSH hBrush, hOldBrush;
  205.     HPEN hPen, hOldPen;
  206.  
  207.     int i, j, top, left, bottom, right;
  208.  
  209. #ifdef PROFILE
  210.     ProfStart();
  211. #endif
  212.  
  213.     SelectPalette(hDC, hPal, 0);
  214.     RealizePalette(hDC);
  215.     hPen = GetStockObject(NULL_PEN);
  216.     hOldPen = SelectObject(hDC, hPen);
  217.  
  218.     /*
  219.         note: the use of (long) casts here is to avoid overrange
  220.         values for large windows
  221.     */
  222.  
  223.     for (j=0, top=0; j<iYcells; j++, top=bottom) {
  224.         bottom = (int)((long)(j+1)*(long)iHeight/(long)iYcells) + 1;
  225.         for (i=0, left=0; i<iXcells; i++, left=right) {
  226.             right = (int)((long)(i+1)*(long)iWidth/(long)iXcells) + 1;
  227.             hBrush = CreateSolidBrush(PALETTEINDEX(j * iXcells + i));
  228.             hOldBrush = SelectObject(hDC, hBrush);
  229.             Rectangle(hDC, left, top, right, bottom);
  230.             SelectObject(hDC, hOldBrush);
  231.             DeleteObject(hBrush);
  232.         }
  233.     }
  234.  
  235.     SelectObject(hDC, hOldPen);
  236.  
  237. #ifdef PROFILE
  238.     ProfStop();
  239. #endif
  240. }
  241.  
  242. /**********************************************************************
  243.  
  244.     Get the number of cells on the X axis which gives the closes
  245.     aspect ratio to 1
  246.  
  247. **********************************************************************/
  248.  
  249. int get_xcells(int xsize, int ysize)
  250. {
  251.     int i, j;
  252.  
  253.     for (i=1; i<PALSIZE; i*=2) {
  254.         j = PALSIZE / i;
  255.         if (i * ysize / j >= xsize) break;
  256.     }
  257.     return i;
  258. }
  259.  
  260.  
  261. /**********************************************************************
  262.  
  263.     Show the rgb value of a given pixel at mouse position
  264.  
  265. **********************************************************************/
  266.  
  267. void show_pixel(LONG lParam)
  268. {
  269.     HDC hDC;
  270.     char buf[80];
  271.     DWORD rgb;
  272.     POINT pt;
  273.  
  274.     /* get a hdc to the whole screen */
  275.  
  276.     hDC = GetDC(NULL);
  277.  
  278.     /* convert mouse coords to screen coords */
  279.  
  280.     pt = MAKEPOINT(lParam);
  281.     ClientToScreen(hMainWnd, &pt);
  282.  
  283.     /* get rgb at mouse position */
  284.  
  285.     rgb = GetPixel(hDC, pt.x, pt.y);
  286.  
  287.     ReleaseDC(NULL, hDC);
  288.  
  289.     wsprintf(buf,
  290.              "%s - RGB(%d,%d,%d)",
  291.              (LPSTR)szAppName,
  292.              GetRValue(rgb),
  293.              GetGValue(rgb),
  294.              GetBValue(rgb));
  295.     SetWindowText(hMainWnd, buf);
  296. }
  297.