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

  1. /*
  2.  
  3. Function(s) demonstrated in this program: GetDC
  4. Windows version:  2.03
  5. Windows SDK version:  2.00
  6. Compiler version:  C 5.10
  7. Description:  This function returns a handle to the display context.
  8.  
  9. */
  10.  
  11. #define NOMINMAX
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include "GethDC.h"
  16.  
  17.  
  18. HWND     hWndParent1, hWndParent2, hWndChild1, hWndChild2;
  19. HANDLE   hInstMain;
  20.  
  21. char     szOutputBuffer1 [70];
  22. char     szOutputBuffer2 [500];
  23.  
  24.  
  25. /****************************************************************************/
  26. /************************    Message Structure      *************************/
  27. /****************************************************************************/
  28.  
  29. struct { char *szMessage; }
  30.        Messages [] = {
  31. "About\0",
  32. "     This is a sample application to demonstrate the\n\
  33. use of the GetDC Windows function.",
  34.  
  35. "Help Message",
  36. "     This program uses the GetDC Windows function\n\
  37. to get a handle to the current display context this\n\
  38. handle is then displayed in a message box.  Use\n\
  39. the menu to invoke this function.",
  40.  
  41. };    
  42.  
  43. /****************************************************************************/
  44.  
  45. void ProcessMessage (HWND, int); 
  46.  
  47. void ProcessMessage (hWnd, MessageNumber) 
  48.      HWND     hWnd;
  49.      int      MessageNumber;
  50. {
  51.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  52.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  53.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  54. }       
  55.  
  56. /****************************************************************************/
  57.  
  58. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  59.      HANDLE      hInstance, hPrevInstance ;
  60.      LPSTR       lpszCmdLine ;
  61.      int         nCmdShow ;
  62.      {
  63.      static char szAppName [] = "GethDC" ;
  64.      HWND        hWnd ;
  65.      WNDCLASS    wndclass ;
  66.      MSG msg;
  67.      short       xScreen, yScreen ;
  68.  
  69.      if (!hPrevInstance) 
  70.           {
  71.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  72.           wndclass.lpfnWndProc   = WndProc ;
  73.           wndclass.cbClsExtra    = 0 ;
  74.           wndclass.cbWndExtra    = 0 ;
  75.           wndclass.hInstance     = hInstance ;
  76.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  77.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  78.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  79.           wndclass.lpszMenuName  = szAppName ;
  80.           wndclass.lpszClassName = szAppName ;
  81.  
  82.           if (!RegisterClass (&wndclass))
  83.                return FALSE ;
  84.           }
  85.  
  86.      xScreen = GetSystemMetrics (SM_CXSCREEN) ;
  87.      yScreen = GetSystemMetrics (SM_CYSCREEN) ;
  88.  
  89.      hWndParent1 = CreateWindow (szAppName,     /* window class name       */
  90.                     "GetDC",                    /* window caption          */
  91.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  92.                     CW_USEDEFAULT,              /* initial x position      */
  93.                     0,                          /* initial y position      */
  94.                     CW_USEDEFAULT,              /* initial x size          */
  95.                     0,                          /* initial y size          */
  96.                     NULL,                       /* parent window handle    */
  97.                     NULL,                       /* window menu handle      */
  98.                     hInstance,                  /* program instance handle */
  99.                     NULL) ;                     /* create parameters       */
  100.  
  101.      ShowWindow (hWndParent1, nCmdShow) ;
  102.      UpdateWindow (hWndParent1) ;
  103.  
  104.      hInstMain = hInstance;
  105.  
  106.      while (GetMessage(&msg, NULL, 0, 0))
  107.      {
  108.       TranslateMessage(&msg);
  109.       DispatchMessage(&msg);
  110.      } 
  111.      return (msg.wParam) ;     
  112.      }
  113.  
  114. /****************************************************************************/
  115.  
  116. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  117. HWND     hWnd ;
  118. unsigned iMessage ;
  119. WORD     wParam ;
  120. LONG     lParam ;
  121. {
  122.  HMENU       hMenu;
  123.  HDC         hDC;
  124.  PAINTSTRUCT ps;
  125.  static int  xClient, yClient;
  126.  switch(iMessage)
  127.  {
  128.   case WM_CREATE:
  129.        hMenu = GetSystemMenu (hWnd, FALSE);
  130.  
  131.        ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  132.                    MF_APPEND | MF_STRING);
  133.        break;
  134.  
  135.   case WM_SYSCOMMAND:
  136.        switch (wParam) {
  137.           case IDM_ABOUT:
  138.                ProcessMessage (hWnd, 0);
  139.                break;
  140.           default:
  141.                return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  142.        }
  143.        break;
  144.  
  145.   case WM_COMMAND:
  146.        switch (wParam) {
  147.           case IDM_GETHDC:
  148.                hDC = GetDC (hWnd);
  149.                sprintf (szOutputBuffer1, 
  150.                         "The Handle to the display context is %x", hDC);
  151.                MessageBox (hWnd, szOutputBuffer1, "GetDC", MB_OK);
  152.                ReleaseDC(hWnd, hDC);
  153.                break;
  154.  
  155.           case IDM_HELP:
  156.                ProcessMessage (hWnd, 2);
  157.                break;
  158.        }
  159.        break;
  160.  
  161.   case WM_PAINT:
  162.        BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  163.        EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  164.        break;
  165.  
  166.   case WM_DESTROY:
  167.        PostQuitMessage(0);
  168.        break;
  169.  
  170.   default:
  171.   {
  172.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  173.   }
  174.  }
  175.  return (0L); 
  176. }
  177.  
  178.  
  179.  
  180. 
  181.