home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / graphics / gnearclr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  8.2 KB  |  224 lines

  1. /*
  2.  * This example demonstrates the use of GetNearestColor(). This example
  3.  * begins with the main menu selection where the user enters RGB values
  4.  * into a dialog box. These values are used to compute the nearest
  5.  * color and displayed in a message box. This loop continues until the
  6.  * user chooses the "Done" selection.
  7.  *
  8.  */
  9.  
  10. #include "windows.h"
  11. #include <stdio.h>
  12. #include "gnearclr.h"
  13.  
  14. /* Global Variables */
  15. static HANDLE hInst;
  16. FARPROC lpprocColor;
  17.  
  18. /* Forward References */
  19. long FAR PASCAL NearestWndProc(HWND, unsigned, WORD, LONG);
  20. BOOL FAR PASCAL ColorDlg(HWND, unsigned, WORD, LONG);
  21.  
  22. /* Color Dialog Function */
  23. BOOL FAR PASCAL ColorDlg(hDlg, message, wParam, lParam)
  24. HWND hDlg;
  25. unsigned message;
  26. WORD wParam;
  27. LONG lParam;
  28. {
  29.    char szOutBuf[80];      /* Output Buffer for Message Box    */
  30.    unsigned uiRValue;      /* Edit Input for Red Value         */
  31.    unsigned uiGValue;      /* Edit Input for Green Value       */
  32.    unsigned uiBValue;      /* Edit Input for Blue Value        */
  33.    BOOL bSuccess;          /* Flag if All Edit Values are Good */
  34.    DWORD dwrgbValue;       /* RGB Value of Nearest Color       */
  35.    HDC hDC;                /* Display Context of Dialog Box    */
  36.    BOOL bReceived;         /* Flag if Edit Received Set Value  */
  37.  
  38.    switch (message) {
  39.       case WM_COMMAND:
  40.          switch (wParam) {
  41.             case ID_DONE:  /* If all done, quit the dialog box */
  42.                EndDialog(hDlg, TRUE);
  43.                return TRUE;
  44.  
  45.             case ID_MYOK:  /* If ready to accept dialog values */
  46.                /* Innocent until proven guilty */
  47.                bSuccess = TRUE;
  48.                /* Get all of the edit values and check them for range */
  49.                uiRValue = GetDlgItemInt(hDlg, ID_REDIT,
  50.                                         (BOOL FAR *)&bReceived, NULL);
  51.                if ((bReceived == NULL) || (uiRValue > 255) ||
  52.                    (uiRValue < 0))
  53.                   {
  54.                   MessageBox(hDlg, (LPSTR)"Invalid Red Value",
  55.                              (LPSTR)"Range Error (0-255)!", MB_OK);
  56.                   bSuccess = FALSE;
  57.                   }
  58.                uiGValue = GetDlgItemInt(hDlg, ID_GEDIT,
  59.                                         (BOOL FAR *)&bReceived, NULL);
  60.                if ((bReceived == NULL) || (uiGValue > 255) ||
  61.                    (uiGValue < 0))
  62.                   {
  63.                   MessageBox(hDlg, (LPSTR)"Invalid Green Value",
  64.                              (LPSTR)"Range Error (0-255)!", MB_OK);
  65.                   bSuccess = FALSE;
  66.                   }
  67.                uiBValue = GetDlgItemInt(hDlg, ID_BEDIT,
  68.                                         (BOOL FAR *)&bReceived, NULL);
  69.                if ((bReceived == NULL) || (uiBValue > 255) ||
  70.                    (uiBValue < 0))
  71.                   {
  72.                   MessageBox(hDlg, (LPSTR)"Invalid Blue Value",
  73.                              (LPSTR)"Range Error (0-255)!", MB_OK);
  74.                   bSuccess = FALSE;
  75.                   }
  76.                if (bSuccess) { /* If all are OK, continue */
  77.                   /* Get the DC and get the nearest color of the user's
  78.                      input. */
  79.                   hDC = GetDC(hDlg);
  80.                   dwrgbValue = GetNearestColor(hDC, RGB((BYTE)uiRValue,
  81.                                                         (BYTE)uiGValue,
  82.                                                         (BYTE)uiBValue)
  83.                                               );
  84.                   ReleaseDC(hDlg, hDC);
  85.                   sprintf(szOutBuf, "%s%d%s%d%s%d%s",
  86.                           "The Nearest Color is: (",
  87.                           GetRValue(dwrgbValue),
  88.                           " , ", GetGValue(dwrgbValue),
  89.                           " , ", GetBValue(dwrgbValue), ").");
  90.                   /* Print a message box and set the focus back so that
  91.                      the user can enter values at will in a loop. */
  92.                   MessageBox(hDlg, (LPSTR)szOutBuf,
  93.                              (LPSTR)"GetNearestColor()", MB_OK);
  94.                   }
  95.                SetFocus(GetDlgItem(hDlg, ID_REDIT));
  96.                SendMessage(GetDlgItem(hDlg, ID_REDIT), EM_SETSEL, NULL,
  97.                            MAKELONG((WORD)0, (WORD)32767));
  98.                return TRUE;
  99.             
  100.             default:
  101.                return FALSE;
  102.             };
  103.          break;
  104.  
  105.       case WM_INITDIALOG:
  106.          /* When the box first comes up, limit the text for each
  107.             numeric value to three digits so that the user is
  108.             less likely to enter an erroneus value. */
  109.          SendMessage(GetDlgItem(hDlg, ID_REDIT), EM_LIMITTEXT,
  110.                      (WORD)3, (LONG)NULL);
  111.          SendMessage(GetDlgItem(hDlg, ID_GEDIT), EM_LIMITTEXT,
  112.                      (WORD)3, (LONG)NULL);
  113.          SendMessage(GetDlgItem(hDlg, ID_BEDIT), EM_LIMITTEXT,
  114.                      (WORD)3, (LONG)NULL);
  115.          /* Intialize all of the edit controls to zero. */
  116.          SetDlgItemInt(hDlg, ID_REDIT, (unsigned)0, NULL);
  117.          SetDlgItemInt(hDlg, ID_GEDIT, (unsigned)0, NULL);
  118.          SetDlgItemInt(hDlg, ID_BEDIT, (unsigned)0, NULL);
  119.          SetFocus(GetDlgItem(hDlg, ID_REDIT));
  120.          SendMessage(GetDlgItem(hDlg, ID_REDIT), EM_SETSEL, NULL,
  121.                      MAKELONG((WORD)0, (WORD)32767));
  122.          return FALSE;
  123.  
  124.       default:
  125.          return FALSE;
  126.       }
  127. }
  128.  
  129.  
  130. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  131. HANDLE hInstance, hPrevInstance;
  132. LPSTR lpszCmdLine;
  133. int cmdShow;
  134. {
  135.     MSG       msg;
  136.     HWND      hWnd;
  137.     PWNDCLASS pNearClass;
  138.  
  139.     if (!hPrevInstance) {
  140.        pNearClass = (PWNDCLASS)LocalAlloc(LPTR, sizeof(WNDCLASS));
  141.  
  142.        pNearClass->hCursor        = LoadCursor(NULL, IDC_ARROW);
  143.        pNearClass->hIcon          = NULL;
  144.        pNearClass->lpszMenuName   = (LPSTR)"NearMenu";
  145.        pNearClass->lpszClassName  = (LPSTR)"gnearclr";
  146.        pNearClass->hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  147.        pNearClass->hInstance      = hInstance;
  148.        pNearClass->style          = CS_HREDRAW | CS_VREDRAW;
  149.        pNearClass->lpfnWndProc    = NearestWndProc;
  150.  
  151.        if (!RegisterClass((LPWNDCLASS)pNearClass))
  152.            /* Initialization failed.
  153.             * Windows will automatically deallocate all allocated memory.
  154.             */
  155.            return FALSE;
  156.  
  157.        LocalFree((HANDLE)pNearClass);
  158.         }
  159.  
  160.     hWnd = CreateWindow((LPSTR)"gnearclr",
  161.                         (LPSTR)"GetNearestColor() Demo",
  162.                         WS_TILEDWINDOW,
  163.                         CW_USEDEFAULT,
  164.                         CW_USEDEFAULT,
  165.                         CW_USEDEFAULT,
  166.                         CW_USEDEFAULT,
  167.                         (HWND)NULL,        /* no parent */
  168.                         (HMENU)NULL,       /* use class menu */
  169.                         (HANDLE)hInstance, /* handle to window instance */
  170.                         (LPSTR)NULL);      /* no params to pass on */
  171.  
  172.     /* Save instance handle for DialogBox */
  173.     hInst = hInstance;
  174.  
  175.     /* Bind callback function with module instance */
  176.     lpprocColor = MakeProcInstance((FARPROC)ColorDlg, hInstance);
  177.  
  178.     /* Make window visible according to the way the app is activated */
  179.     ShowWindow(hWnd, cmdShow);
  180.     UpdateWindow(hWnd);
  181.  
  182.     /* Polling messages from event queue */
  183.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  184.         TranslateMessage((LPMSG)&msg);
  185.         DispatchMessage((LPMSG)&msg);
  186.         }
  187.  
  188.     return (int)msg.wParam;
  189. }
  190.  
  191. /* Procedures which make up the window class. */
  192. long FAR PASCAL NearestWndProc(hWnd, message, wParam, lParam)
  193. HWND hWnd;
  194. unsigned message;
  195. WORD wParam;
  196. LONG lParam;
  197. {
  198.     PAINTSTRUCT ps;
  199.  
  200.     switch (message)
  201.     {
  202.     case WM_COMMAND:
  203.         switch (wParam)
  204.         {
  205.         case ID_NEAR:
  206.            /* Let 'er rip. */
  207.             DialogBox(hInst, MAKEINTRESOURCE(NEARBOX), hWnd, lpprocColor);
  208.             break;
  209.         default:
  210.             return DefWindowProc(hWnd, message, wParam, lParam);
  211.         }
  212.         break;
  213.  
  214.     case WM_DESTROY:
  215.         PostQuitMessage(0);
  216.         break;
  217.  
  218.     default:
  219.         return DefWindowProc(hWnd, message, wParam, lParam);
  220.         break;
  221.     }
  222.     return(0L);
  223. }
  224.