home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / metafile / smetafb.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  4KB  |  129 lines

  1. /*
  2.  * This application demonstrates the SetMetaFile() Windows function.
  3.  * The result is demonstrated on the screen.
  4.  *
  5.  */
  6. #include "windows.h"
  7.  
  8. static HANDLE hInst;
  9. HANDLE hMF;
  10. HANDLE hMem;
  11. HANDLE hMetaDC;
  12. HANDLE hSetMF;
  13.  
  14. long FAR PASCAL SetMetaWndProc(HWND, unsigned, WORD, LONG);
  15.  
  16. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  17. HANDLE hInstance, hPrevInstance;
  18. LPSTR lpszCmdLine;
  19. int cmdShow;
  20. {
  21.     MSG   msg;
  22.     HWND  hWnd;
  23.     HMENU hMenu;
  24.  
  25.     if (!hPrevInstance) {
  26.        PWNDCLASS   pSetMetaClass;
  27.  
  28.        pSetMetaClass = (PWNDCLASS)LocalAlloc(LPTR, sizeof(WNDCLASS));
  29.  
  30.        pSetMetaClass->hCursor        = LoadCursor(NULL, IDC_ARROW);
  31.        pSetMetaClass->hIcon          = NULL;
  32.        pSetMetaClass->lpszMenuName   = (LPSTR)NULL;
  33.        pSetMetaClass->lpszClassName  = (LPSTR)"SetMetaFileBits";
  34.        pSetMetaClass->hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  35.        pSetMetaClass->hInstance      = hInstance;
  36.        pSetMetaClass->style          = CS_HREDRAW | CS_VREDRAW;
  37.        pSetMetaClass->lpfnWndProc    = SetMetaWndProc;
  38.  
  39.        if (!RegisterClass((LPWNDCLASS)pSetMetaClass))
  40.            return FALSE;
  41.  
  42.        LocalFree((HANDLE)pSetMetaClass);
  43.        }
  44.  
  45.     hWnd = CreateWindow((LPSTR)"SetMetaFileBits",
  46.                         (LPSTR)"SetMetaFileBits() Demo",
  47.                         WS_TILEDWINDOW,
  48.                         CW_USEDEFAULT,
  49.                         CW_USEDEFAULT,
  50.                         CW_USEDEFAULT,
  51.                         CW_USEDEFAULT,
  52.                         (HWND)NULL,        
  53.                         (HMENU)NULL,       
  54.                         (HANDLE)hInstance, 
  55.                         (LPSTR)NULL        
  56.                         );
  57.  
  58.     hInst = hInstance;
  59.  
  60.     hMetaDC = CreateMetaFile(NULL);
  61.     if (hMetaDC == NULL)
  62.        MessageBox(GetFocus(),
  63.                   (LPSTR)"Error in Creating Memory Metafile",
  64.                   (LPSTR)"CreateMetaFile() Error!",
  65.                   MB_OK | MB_ICONEXCLAMATION);
  66.     else {
  67.        Rectangle(hMetaDC, 10, 10, 200, 200);
  68.        Rectangle(hMetaDC, 201, 10, 401, 200);
  69.        LineTo(hMetaDC, 100, 250);
  70.        Ellipse(hMetaDC, 0, 0, 100, 100);
  71.        hMF = CloseMetaFile(hMetaDC);
  72.        if (hMF == NULL)
  73.           MessageBox(GetFocus(),
  74.                      (LPSTR)"Error Closing Meta File.",
  75.                      (LPSTR)"CloseMetaFile() Error!",
  76.                      MB_OK | MB_ICONEXCLAMATION);
  77.        else {
  78.           hMem = GetMetaFileBits(hMF);
  79.           if (hMem == NULL)
  80.              MessageBox (GetFocus(),
  81.                          (LPSTR)"Error in Obtaining Metafile Bit Handle",
  82.                          (LPSTR)"GetMetaFileBits() Error!!!",
  83.                          MB_OK | MB_ICONEXCLAMATION);
  84.           else {
  85.              hSetMF = SetMetaFileBits(hMem);
  86.              if (hSetMF == NULL)
  87.                 MessageBox(GetFocus(),
  88.                            (LPSTR)"Error in Setting Meta File Bits!",
  89.                            (LPSTR)"SetMetaFileBits() Error!!!",
  90.                            MB_OK | MB_ICONEXCLAMATION);
  91.              }
  92.           }
  93.        }
  94.  
  95.     ShowWindow(hWnd, cmdShow);
  96.     UpdateWindow(hWnd);
  97.  
  98.     /* Polling messages from event queue */
  99.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  100.         TranslateMessage((LPMSG)&msg);
  101.         DispatchMessage((LPMSG)&msg);
  102.         }
  103.  
  104.     return (int)msg.wParam;
  105. }
  106.  
  107. long FAR PASCAL SetMetaWndProc(hWnd, iMessage, wParam, lParam)
  108. HWND     hWnd;
  109. unsigned iMessage;
  110. WORD     wParam;
  111. LONG     lParam;
  112. {
  113.    PAINTSTRUCT ps;
  114.    HDC         hDC;
  115.  
  116.    switch (iMessage) {
  117.       case WM_PAINT:
  118.          hDC = BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  119.          PlayMetaFile(hDC, hSetMF);
  120.          EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  121.          break;
  122.  
  123.       default:
  124.          return DefWindowProc(hWnd, iMessage, wParam, lParam);
  125.       }
  126.    return(0L);
  127. }
  128.  
  129.