DIALOG.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:9k
源码类别:

Windows编程

开发平台:

Visual C++

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