home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / gdi / gdidemo / dialog.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  9KB  |  276 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /*---------------------------------------------------------------------------*\
  13. | DIALOG MODULE
  14. |   This module contains the dialogbox routines for this application.
  15. \*---------------------------------------------------------------------------*/
  16.  
  17. #include <windows.h>
  18. #include "gdidemo.h"
  19.  
  20. /*---------------------------------------------------------------------------*\
  21. | DISPLAY DIALOG BOX
  22. |   This is a routine to display a generic modal-dialog box.
  23. |
  24. \*---------------------------------------------------------------------------*/
  25. int FAR DisplayDialogBox(HWND hWnd, LPSTR lpszTemplate, WNDPROC lpfFunction, LONG lExtra)
  26. {
  27.     HANDLE  hInstance;
  28.     WNDPROC lpfDlg;
  29.     int     nRet;
  30.  
  31.  
  32.     nRet = -1;
  33.     if(hInstance = GETINSTANCE(hWnd))
  34.     {
  35.         if(lpfDlg = MakeProcInstance(lpfFunction,hInstance))
  36.         {
  37.             nRet = DialogBoxParam(hInstance,lpszTemplate,hWnd,(DLGPROC)lpfDlg,lExtra);
  38.             FreeProcInstance(lpfDlg);
  39.         }
  40.     }
  41.     return(nRet);
  42. }
  43.  
  44.  
  45. /*---------------------------------------------------------------------------*\
  46. | ABOUT DIALOG PROCEDURE
  47. |   This is the main dialog box routine for the HELPABOUT template.
  48. |
  49. \*---------------------------------------------------------------------------*/
  50. BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT wMsg, WPARAM wParam, LONG lParam)
  51. {
  52.     lParam = lParam;
  53.  
  54.     switch(wMsg)
  55.     {
  56.         /*
  57.         ** Set the focus to the OK button.
  58.         */
  59.         case WM_INITDIALOG:
  60.             SetFocus(GetDlgItem(hDlg,IDOK));
  61.             break;
  62.  
  63.  
  64.         /*
  65.         ** Look for an ESC or RETURN event.
  66.         */
  67.         case WM_COMMAND:
  68.             switch(wParam)
  69.             {
  70.                 case IDOK:
  71.                 case IDCANCEL:
  72.                     EndDialog(hDlg,TRUE);
  73.                     break;
  74.  
  75.                 default:
  76.                     return(FALSE);
  77.             }
  78.             break;
  79.  
  80.  
  81.         /*
  82.         ** Wash the background of the aboutbox to give it a nice blue-scaling
  83.         ** effect.  Invalidate the OK button to force it to the top.  This
  84.         ** seems to be necessary since the OK button gets overwritten during
  85.         ** the washing.
  86.         */
  87.         case WM_PAINT:
  88.             PaintWindow(hDlg,COLOR_SCALE_BLUE);
  89.             InvalidateRect(GetDlgItem(hDlg,IDOK),NULL,TRUE);
  90.  
  91.  
  92.         /*
  93.         ** Default handler.
  94.         */
  95.         default:
  96.             return(FALSE);
  97.     }
  98.     return(TRUE);
  99. }
  100.  
  101.  
  102. /*---------------------------------------------------------------------------*\
  103. | PAINT WND BACKGROUND
  104. |   This routine is used to wash the background of a window.
  105. |
  106. \*---------------------------------------------------------------------------*/
  107. VOID PaintWindow(HWND hWnd, int nColor)
  108. {
  109.     HDC         hDC;
  110.     int         nMapMode,idx,nSize,nReserved,nLoop;
  111.     RECT        rect;
  112.     HBRUSH      hBrush;
  113.     PAINTSTRUCT ps;
  114.     HPALETTE    hPal;
  115.  
  116.  
  117.     if(hDC = BeginPaint(hWnd,&ps))
  118.     {
  119.         GetClientRect(hWnd,&rect);
  120.         nMapMode = SetMapMode(hDC,MM_ANISOTROPIC);
  121.  
  122.  
  123.         if(GetDeviceCaps(hDC,RASTERCAPS) & RC_PALETTE)
  124.         {
  125.             nReserved = GetDeviceCaps(hDC,NUMRESERVED);
  126.             nSize     = GetDeviceCaps(hDC,SIZEPALETTE) - nReserved;
  127.  
  128.             if(hPal = CreateColorScalePalette(hDC,nColor))
  129.             {
  130.                 hPal = SelectPalette(hDC,hPal,FALSE);
  131.                 RealizePalette(hDC);
  132.  
  133.  
  134. #ifdef WIN16
  135.                 SetWindowExtEx(hDC,nSize,nSize);
  136.                 SetViewportExtEx(hDC,rect.right,-rect.bottom);
  137.                 SetViewportOrgEx(hDC,0,rect.bottom);
  138. #else
  139.  
  140.                 SetWindowExtEx(hDC,nSize,nSize,NULL);
  141.                 SetViewportExtEx(hDC,rect.right,-rect.bottom,NULL);
  142.                 SetViewportOrgEx(hDC,0,rect.bottom,NULL);
  143.  
  144. #endif
  145.  
  146.                 nLoop = nSize >> 1;
  147.                 for(idx=0; idx < nLoop; idx++)
  148.                 {
  149.                     hBrush = CreateSolidBrush(PALETTEINDEX(idx+nLoop));
  150.                     SetRect(&rect,idx,idx,nSize-idx,nSize-idx);
  151.                     FillRect(hDC,&rect,hBrush);
  152.                     DeleteObject(hBrush);
  153.                 }
  154.                 DeleteObject(SelectPalette(hDC,hPal,FALSE));
  155.                 RealizePalette(hDC);
  156.             }
  157.         }
  158.         else
  159.         {
  160.  
  161. #ifdef WIN16
  162.  
  163.             SetWindowExtEx(hDC,512,512);
  164.             SetViewportExtEx(hDC,rect.right,-rect.bottom);
  165.             SetViewportOrgEx(hDC,0,rect.bottom);
  166.  
  167. #else
  168.  
  169.             SetWindowExtEx(hDC,512,512,NULL);
  170.             SetViewportExtEx(hDC,rect.right,-rect.bottom,NULL);
  171.             SetViewportOrgEx(hDC,0,rect.bottom,NULL);
  172.  
  173. #endif
  174.  
  175.             for(idx=0; idx < 256; idx++)
  176.             {
  177.                 hBrush = CreateSolidBrush(RGB(0,0,idx));
  178.                 SetRect(&rect,idx,idx,512-idx,512-idx);
  179.                 FillRect(hDC,&rect,hBrush);
  180.                 DeleteObject(hBrush);
  181.             }
  182.         }
  183.  
  184.         SetMapMode(hDC,nMapMode);
  185.  
  186.         EndPaint(hWnd,&ps);
  187.     }
  188.     return;
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. /*---------------------------------------------------------------------------*\
  197. | CREATE COLOR SCALE PALETTE
  198. |   This routine creates a palette representing the scale values of a
  199. |   particular RGB color.  A gray-scale palette can also be created.
  200. |
  201. \*---------------------------------------------------------------------------*/
  202. HPALETTE CreateColorScalePalette(HWND hDC, int nColor)
  203. {
  204.     HPALETTE     hPalette;
  205.     GLOBALHANDLE hMem;
  206.     LPLOGPALETTE lpMem;
  207.     int          idx,nReserved,nSize;
  208.  
  209.  
  210.     hPalette = NULL;
  211.     if(GetDeviceCaps(hDC,RASTERCAPS) & RC_PALETTE)
  212.     {
  213.         nReserved = GetDeviceCaps(hDC,NUMRESERVED);
  214.         nSize     = GetDeviceCaps(hDC,SIZEPALETTE) - nReserved;
  215.  
  216.         if(hMem = GlobalAlloc(GHND,(DWORD)sizeof(LOGPALETTE)+(sizeof(PALETTEENTRY)*nSize)))
  217.         {
  218.             if(lpMem = (LPLOGPALETTE)GlobalLock(hMem))
  219.             {
  220.                 lpMem->palNumEntries = (WORD)nSize;
  221.                 lpMem->palVersion    = (WORD)0x0300;
  222.  
  223.                 switch(nColor)
  224.                 {
  225.                     case COLOR_SCALE_RED:
  226.                         for(idx=0; idx < nSize; idx++)
  227.                         {
  228.                             lpMem->palPalEntry[idx].peRed   = (BYTE)idx;
  229.                             lpMem->palPalEntry[idx].peGreen = 0;
  230.                             lpMem->palPalEntry[idx].peBlue  = 0;
  231.                             lpMem->palPalEntry[idx].peFlags = PC_RESERVED;
  232.                         }
  233.                         break;
  234.  
  235.                     case COLOR_SCALE_GREEN:
  236.                         for(idx=0; idx < nSize; idx++)
  237.                         {
  238.                             lpMem->palPalEntry[idx].peRed   = 0;
  239.                             lpMem->palPalEntry[idx].peGreen = (BYTE)idx;
  240.                             lpMem->palPalEntry[idx].peBlue  = 0;
  241.                             lpMem->palPalEntry[idx].peFlags = PC_RESERVED;
  242.                         }
  243.                         break;
  244.  
  245.                     case COLOR_SCALE_BLUE:
  246.                         for(idx=0; idx < nSize; idx++)
  247.                         {
  248.                             lpMem->palPalEntry[idx].peRed   = 0;
  249.                             lpMem->palPalEntry[idx].peGreen = 0;
  250.                             lpMem->palPalEntry[idx].peBlue  = (BYTE)idx;
  251.                             lpMem->palPalEntry[idx].peFlags = PC_RESERVED;
  252.                         }
  253.                         break;
  254.  
  255.                     default:
  256.                     case COLOR_SCALE_GRAY:
  257.                         for(idx=0; idx < nSize; idx++)
  258.                         {
  259.                             lpMem->palPalEntry[idx].peRed   = (BYTE)idx;
  260.                             lpMem->palPalEntry[idx].peGreen = (BYTE)idx;
  261.                             lpMem->palPalEntry[idx].peBlue  = (BYTE)idx;
  262.                             lpMem->palPalEntry[idx].peFlags = PC_RESERVED;
  263.                         }
  264.                         break;
  265.                 }
  266.  
  267.                 hPalette = CreatePalette(lpMem);
  268.  
  269.                 GlobalUnlock(hMem);
  270.             }
  271.             GlobalFree(hMem);
  272.         }
  273.     }
  274.     return(hPalette);
  275. }
  276.