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

  1. /*
  2.  *  Function Name:   CreateMetaFile
  3.  *  Program Name:    crmetafi.c
  4.  *  SDK Version:         2.03
  5.  *  Runtime Version:     2.03
  6.  *  Microsoft C Version: 5.0
  7.  *
  8.  *  Description:
  9.  *   This function is used for saving GDI images on a disk (and in memory)
  10.  *   that can latter be recalled and displayed or printed.  Here a metafile
  11.  *   is created, text is placed in it, and it is written to the disk.
  12.  *   It is then recalled and displayed on the screen.  The recall is either
  13.  *   from memory or disk depending on whether a handle is available to a
  14.  *   memory image.
  15.  */
  16.  
  17. #include "windows.h"
  18.  
  19. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  20.  
  21. /***********************************************************************/
  22.  
  23. void CALL_CreateMetaFile(hWnd, hDC)
  24. HWND hWnd;
  25. HDC hDC;
  26. {
  27.   HDC hDCMeta;
  28.   HANDLE hDiskMetaFile;
  29.  
  30.   hDCMeta = CreateMetaFile((LPSTR) "crmetafi.met");  /* Metafile created */
  31.  
  32.   if (hDCMeta == NULL)
  33.   {
  34.     MessageBox(hWnd,(LPSTR)"CreateMetaFile failed",(LPSTR)"ERROR",MB_ICONHAND);
  35.     return;
  36.   }
  37.   else
  38.     TextOut(hDCMeta, 20, 20, (LPSTR)"Metafile created", 16);
  39.  
  40.   hDiskMetaFile = CloseMetaFile(hDCMeta);
  41.   PlayMetaFile(hDC, hDiskMetaFile);        /* show metafile  */
  42.   DeleteMetaFile(hDiskMetaFile);
  43.  
  44. return;
  45. }
  46.  
  47. /**************************************************************************/
  48.  
  49. /* Procedure called when the application is loaded for the first time */
  50. BOOL WinInit( hInstance )
  51. HANDLE hInstance;
  52. {
  53.     WNDCLASS   wcClass;
  54.  
  55.     wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  56.     wcClass.lpfnWndProc    = WndProc;
  57.     wcClass.cbClsExtra     =0;
  58.     wcClass.cbWndExtra     =0;
  59.     wcClass.hInstance      = hInstance;
  60.     wcClass.hIcon          = LoadIcon( hInstance,NULL );
  61.     wcClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  62.     wcClass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  63.     wcClass.lpszMenuName   = (LPSTR)NULL;
  64.     wcClass.lpszClassName  = (LPSTR)"CreateMetaFile";
  65.  
  66.     if (!RegisterClass( (LPWNDCLASS)&wcClass ) )
  67.         /* Initialization failed.
  68.          * Windows will automatically deallocate all allocated memory.
  69.          */
  70.         return FALSE;
  71.  
  72.     return TRUE;        /* Initialization succeeded */
  73. }
  74.  
  75.  
  76. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  77. HANDLE hInstance, hPrevInstance;
  78. LPSTR lpszCmdLine;
  79. int cmdShow;
  80. {
  81.     MSG   msg;
  82.     HWND  hWnd;
  83.     HMENU hMenu;
  84.  
  85.     if (!hPrevInstance)
  86.         {
  87.         /* Call initialization procedure if this is the first instance */
  88.         if (!WinInit( hInstance ))
  89.             return FALSE;
  90.         }
  91.  
  92.     hWnd = CreateWindow((LPSTR)"CreateMetaFile",
  93.                         (LPSTR)"CreateMetaFile()",
  94.                         WS_OVERLAPPEDWINDOW,
  95.                         CW_USEDEFAULT,
  96.                         CW_USEDEFAULT,
  97.                         CW_USEDEFAULT,
  98.                         CW_USEDEFAULT,
  99.                         (HWND)NULL,        /* no parent */
  100.                         (HMENU)NULL,       /* use class menu */
  101.                         (HANDLE)hInstance, /* handle to window instance */
  102.                         (LPSTR)NULL        /* no params to pass on */
  103.                         );
  104.  
  105.     /* Make window visible according to the way the app is activated */
  106.     ShowWindow( hWnd, cmdShow );
  107.     UpdateWindow( hWnd );
  108.  
  109.     /* Polling messages from event queue */
  110.     while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  111.         {
  112.         TranslateMessage((LPMSG)&msg);
  113.         DispatchMessage((LPMSG)&msg);
  114.         }
  115.  
  116.     return (int)msg.wParam;
  117. }
  118.  
  119. /* Procedures which make up the window class. */
  120. long FAR PASCAL WndProc( hWnd, message, wParam, lParam )
  121. HWND hWnd;
  122. unsigned message;
  123. WORD wParam;
  124. LONG lParam;
  125. {
  126.     PAINTSTRUCT ps;
  127.  
  128.     switch (message)
  129.     {
  130.  
  131.     case WM_PAINT:
  132.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  133.         CALL_CreateMetaFile(hWnd, ps.hdc);
  134.         ValidateRect(hWnd, (LPRECT) NULL);
  135.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  136.         break;
  137.  
  138.     case WM_DESTROY:
  139.         PostQuitMessage( 0 );
  140.         break;
  141.  
  142.     default:
  143.         return DefWindowProc( hWnd, message, wParam, lParam );
  144.         break;
  145.     }
  146.     return(0L);
  147. }
  148.  
  149.