home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 18.ddi / SAMPLES / WMFDCODE / WMFDCODE.C_ / WMFDCODE.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  16.2 KB  |  515 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: wmfdcode.c
  4.  
  5.     PURPOSE: view and decode Windows Metafiles
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     WaitCursor() - loads hourglass cursor/restores original cursor
  14.  
  15.     HISTORY: 1/16/91 - wrote it - drc
  16.  
  17. ****************************************************************************/
  18.  
  19. #define MAIN
  20.  
  21. #include "windows.h"                /* required for all Windows applications */
  22. #include "wmfdcode.h"            /* specific to this program          */
  23.  
  24.  
  25. /***********************************************************************
  26.  
  27.   FUNCTION   : WinMain
  28.  
  29.   PARAMETERS : HANDLE
  30.            HANDLE
  31.            LPSTR
  32.            int
  33.  
  34.   PURPOSE    : calls initialization function, processes message loop
  35.  
  36.   CALLS      : WINDOWS
  37.          GetMessage
  38.          TranslateMessage
  39.          DispatchMessage
  40.  
  41.            APP
  42.          InitApplication
  43.  
  44.   RETURNS    : int
  45.  
  46.   COMMENTS   : Windows recognizes this function by name as the initial entry
  47.            point for the program.  This function calls the application
  48.            initialization routine, if no other instance of the program is
  49.            running, and always calls the instance initialization routine.
  50.            It then executes a message retrieval and dispatch loop that is
  51.            the top-level control structure for the remainder of execution.
  52.            The loop is terminated when a WM_QUIT message is received, at
  53.            which time this function exits the application instance by
  54.            returning the value passed by PostQuitMessage().
  55.  
  56.            If this function must abort before entering the message loop,
  57.            it returns the conventional value NULL.
  58.  
  59.   HISTORY    : 1/16/91 - created - modified from SDK sample GENERIC
  60.  
  61. ************************************************************************/
  62.  
  63. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  64. HANDLE hInstance;                            /* current instance             */
  65. HANDLE hPrevInstance;                        /* previous instance            */
  66. LPSTR lpCmdLine;                             /* command line                 */
  67. int nCmdShow;                                /* show-window type (open/icon) */
  68. {
  69.     MSG msg;                                 /* message                      */
  70.  
  71.     if (!hPrevInstance)                  /* Other instances of app running? */
  72.     if (!InitApplication(hInstance)) /* Initialize shared things */
  73.         return (FALSE);              /* Exits if unable to initialize     */
  74.  
  75.     /* Perform initializations that apply to a specific instance */
  76.  
  77.     if (!InitInstance(hInstance, nCmdShow))
  78.     return (FALSE);
  79.  
  80.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  81.  
  82.     while (GetMessage(&msg,        /* message structure                      */
  83.         NULL,                  /* handle of window receiving the message */
  84.         NULL,                  /* lowest message to examine              */
  85.         NULL))                 /* highest message to examine             */
  86.     {
  87.     TranslateMessage(&msg);    /* Translates virtual key codes           */
  88.     DispatchMessage(&msg);     /* Dispatches message to window           */
  89.     }
  90.     return (msg.wParam);           /* Returns the value from PostQuitMessage */
  91. }
  92.  
  93.  
  94. /***********************************************************************
  95.  
  96.   FUNCTION   : InitApplication
  97.  
  98.   PARAMETERS : HANDLE hInstance
  99.  
  100.   PURPOSE    : Initializes window data and registers window class
  101.  
  102.   CALLS      : WINDOWS
  103.          RegisterClass
  104.  
  105.   MESSAGES   : none
  106.  
  107.   RETURNS    : BOOL
  108.  
  109.   COMMENTS   : This function is called at initialization time only if no
  110.            other instances of the application are running.  This function
  111.            performs initialization tasks that can be done once for any
  112.            number of running instances.
  113.  
  114.            In this case, we initialize a window class by filling out a
  115.            data structure of type WNDCLASS and calling the Windows
  116.            RegisterClass() function.  Since all instances of this
  117.            application use the same window class, we only need to do this
  118.            when the first instance is initialized.
  119.  
  120.   HISTORY    : 1/16/91 - created - modified from SDK sample app GENERIC
  121.  
  122. ************************************************************************/
  123.  
  124. BOOL InitApplication(hInstance)
  125. HANDLE hInstance;                       /* current instance */
  126. {
  127.     WNDCLASS  wc;
  128.  
  129.     bInPaint = FALSE;
  130.  
  131.     /* Fill in window class structure with parameters that describe the
  132.        main window. */
  133.  
  134.     wc.style = NULL;                    /* Class style(s) */
  135.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for */
  136.                     /* windows of this class */
  137.     wc.cbClsExtra = 0;                  /* No per-class extra data */
  138.     wc.cbWndExtra = 0;                  /* No per-window extra data */
  139.     wc.hInstance = hInstance;           /* Application that owns the class */
  140.     wc.hIcon = LoadIcon(hInstance, "WMFICON");
  141.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  142.     wc.hbrBackground = COLOR_WINDOW + 1;
  143.     wc.lpszMenuName =  "MetaMenu";      /* Name of menu resource in .RC file */
  144.     wc.lpszClassName = "MetaWndClass";  /* Name used in call to CreateWindow */
  145.  
  146.     /* Register the window class and return success/failure code */
  147.  
  148.     return (RegisterClass(&wc));
  149.  
  150. }
  151.  
  152. /***********************************************************************
  153.  
  154.   FUNCTION   : InitInstance
  155.  
  156.   PARAMETERS : HANDLE  hInstance - Current instance identifier
  157.            int     nCmdShow  - Param for first ShowWindow() call
  158.  
  159.   PURPOSE    : Saves instance handle and creates main window
  160.  
  161.   CALLS      : WINDOWS
  162.          CreateWindow
  163.          ShowWindow
  164.          UpdateWindow
  165.  
  166.   MESSAGES   : none
  167.  
  168.   RETURNS    : BOOL
  169.  
  170.   COMMENTS   : This function is called at initialization time for every
  171.            instance of this application.  This function performs
  172.            initialization tasks that cannot be shared by multiple
  173.            instances.
  174.  
  175.            In this case, we save the instance handle in a static variable
  176.            and create and display the main program window.
  177.  
  178.   HISTORY    :
  179.  
  180. ************************************************************************/
  181.  
  182. BOOL InitInstance(hInstance, nCmdShow)
  183. HANDLE     hInstance;          /* Current instance identifier.       */
  184. int        nCmdShow;           /* Param for first ShowWindow() call. */
  185. {
  186.     HWND   hWnd;               /* Main window handle.                */
  187.  
  188.     /* Save the instance handle in static variable, which will be used in  */
  189.     /* many subsequence calls from this application to Windows.            */
  190.  
  191.     hInst = hInstance;
  192.  
  193.     /* Create a main window for this application instance.  */
  194.  
  195.     hWnd = CreateWindow(
  196.     "MetaWndClass",                 /* See RegisterClass() call.          */
  197.     APPNAME,                        /* Text for window title bar.         */
  198.     WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  199.     CW_USEDEFAULT,                  /* Default horizontal position.       */
  200.     CW_USEDEFAULT,                  /* Default vertical position.         */
  201.     CW_USEDEFAULT,                  /* Default width.                     */
  202.     CW_USEDEFAULT,                  /* Default height.                    */
  203.     NULL,                           /* Overlapped windows have no parent. */
  204.     NULL,                           /* Use the window class menu.         */
  205.     hInstance,                      /* This instance owns this window.    */
  206.     NULL                            /* Pointer not needed.                */
  207.     );
  208.  
  209.     /* If window could not be created, return "failure" */
  210.  
  211.     if (!hWnd)
  212.     return (FALSE);
  213.  
  214.     hWndMain = hWnd;
  215.  
  216.     /* Make the window visible; update its client area; and return "success" */
  217.  
  218.     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  219.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  220.     return (TRUE);               /* Returns the value from PostQuitMessage */
  221.  
  222. }
  223.  
  224. /***********************************************************************
  225.  
  226.   FUNCTION   : MainWndProc
  227.  
  228.   PARAMETERS : HWND hWnd        -  window handle
  229.            unsigned message -  type of message
  230.            WORD wParam      -  additional information
  231.            LONG lParam      -  additional information
  232.  
  233.   PURPOSE    : Processes messages
  234.  
  235.   CALLS      :
  236.  
  237.   MESSAGES   : WM_CREATE
  238.  
  239.            WM_COMMAND
  240.  
  241.          wParams
  242.          - IDM_EXIT
  243.          - IDM_ABOUT
  244.          - IDM_OPEN
  245.          - IDM_PRINT
  246.          - IDM_LIST
  247.          - IDM_CLEAR
  248.          - IDM_ENUM
  249.          - IDM_ENUMRANGE
  250.          - IDM_ALLREC
  251.          - IDM_DESTDISPLAY
  252.          - IDM_DESTMETA
  253.          - IDM_HEADER
  254.          - IDM_CLIPHDR
  255.          - IDM_ALDUSHDR
  256.  
  257.            WM_DESTROY
  258.  
  259.   RETURNS    : long
  260.  
  261.   COMMENTS   :
  262.  
  263.   HISTORY    : 1/16/91 - created - drc
  264.  
  265. ************************************************************************/
  266.  
  267. long FAR PASCAL __export MainWndProc(hWnd, message, wParam, lParam)
  268. HWND hWnd;                                /* window handle                   */
  269. UINT message;                  /* type of message         */
  270. WPARAM wParam;                    /* additional information           */
  271. LPARAM lParam;                    /* additional information           */
  272. {
  273.     RECT        rect;
  274.     int         iFOpenRet;
  275.     char        TempOpenName[128];
  276.     int         iDlgRet;
  277.  
  278.     switch (message) {
  279.     case WM_CREATE:
  280.  
  281.         /* init the state of the menu items */
  282.         EnableMenuItem(GetMenu(hWnd), IDM_VIEW, MF_DISABLED|MF_GRAYED|MF_BYPOSITION);
  283.         EnableMenuItem(GetMenu(hWnd), IDM_PLAY, MF_DISABLED|MF_GRAYED|MF_BYPOSITION);
  284.         EnableMenuItem(GetMenu(hWnd), IDM_PRINT, MF_DISABLED|MF_GRAYED);
  285.         CheckMenuItem(GetMenu(hWnd), IDM_DESTDISPLAY, MF_CHECKED);
  286.         break;
  287.  
  288.     case WM_COMMAND:
  289.         /* message: command from application menu */
  290.         switch (wParam) {
  291.  
  292.         case IDM_EXIT: /* file exit menu option */
  293.  
  294.            PostQuitMessage(0);
  295.            break;
  296.  
  297.         case IDM_ABOUT: /* about box */
  298.  
  299.            lpProcAbout = MakeProcInstance(About, hInst);
  300.            DialogBox(hInst,                      /* current instance         */
  301.                 "AboutBox",                  /* resource to use          */
  302.                  hWnd,                       /* parent handle            */
  303.                  lpProcAbout);               /* About() instance address */
  304.            FreeProcInstance(lpProcAbout);
  305.            break;
  306.  
  307.         case IDM_OPEN: /* select a metafile to open */
  308.  
  309.             /* save the name of previously opened file */
  310.             if (lstrlen((LPSTR)OpenName) != 0)
  311.             lstrcpy((LPSTR)TempOpenName, (LPSTR)OpenName);
  312.  
  313.             /* initialize file info flags */
  314.             if (!bMetaFileOpen) {
  315.               bBadFile = FALSE;
  316.               bValidFile = FALSE;
  317.             }
  318.  
  319.             /* clear the client area */
  320.             GetClientRect(hWnd, (LPRECT)&rect);
  321.             InvalidateRect(hWnd, (LPRECT)&rect, TRUE);
  322.  
  323.             /* call file open dlg */
  324.               iFOpenRet = OpenFileDialog((LPSTR)OpenName);
  325.  
  326.             /* if a file was selected */
  327.             if (iFOpenRet)  {
  328.  
  329.               /* if file contains a valid metafile and it was rendered */
  330.               if (!ProcessFile(hWnd, (LPSTR)OpenName))
  331.             lstrcpy((LPSTR)OpenName, (LPSTR)TempOpenName);
  332.             }
  333.             else
  334.               lstrcpy((LPSTR)OpenName, (LPSTR)TempOpenName);
  335.  
  336.             break;
  337.  
  338.         case IDM_PRINT: /* play the metafile to a printer DC */
  339.  
  340.             /* if the metafile hasn't already been rendered as a placeable
  341.                or clipboard metafile */
  342.             if (!bMetaInRam)
  343.               hMF = GetMetaFile( (LPSTR)OpenName);
  344.  
  345.             /* print it */
  346.             PrintWMF();
  347.             break;
  348.  
  349.         case IDM_LIST: /* list box containing all records of metafile */
  350.  
  351.             WaitCursor(TRUE);
  352.             lpListDlgProc = MakeProcInstance(ListDlgProc, hInst);
  353.             DialogBox(hInst,             /* current instance         */
  354.                  "LISTRECS",                         /* resource to use          */
  355.                   hWnd,                      /* parent handle            */
  356.                   lpListDlgProc);            /* About() instance address */
  357.             FreeProcInstance(lpListDlgProc);
  358.             WaitCursor(FALSE);
  359.             break;
  360.  
  361.         case IDM_CLEAR: /* clear the client area */
  362.  
  363.             GetClientRect(hWnd, (LPRECT)&rect);
  364.             InvalidateRect(hWnd, (LPRECT)&rect, TRUE);
  365.             break;
  366.  
  367.         case IDM_ENUM: /* play - step - all menu option */
  368.  
  369.             /* set flags appropriately before playing to destination */
  370.             bEnumRange = FALSE;
  371.             bPlayItAll = FALSE;
  372.             PlayMetaFileToDest(hWnd, iDestDC);
  373.             break;
  374.  
  375.         case IDM_ENUMRANGE: /* play - step - range menu option */
  376.  
  377.             /* odd logic here...this just forces evaluation of the
  378.                enumeration range in MetaEnumProc. We are not "playing
  379.                it all" */
  380.  
  381.             bPlayItAll = TRUE;
  382.  
  383.             lpEnumRangeDlg = MakeProcInstance(EnumRangeDlgProc, hInst);
  384.             iDlgRet = DialogBox(hInst,
  385.                        "ENUMRANGE",
  386.                        hWnd,
  387.                        lpEnumRangeDlg);
  388.             FreeProcInstance(lpEnumRangeDlg);
  389.  
  390.             /* if cancel button not pressed, play to destination */
  391.             if (iDlgRet != IDCANCEL)
  392.               PlayMetaFileToDest(hWnd, iDestDC);
  393.             break;
  394.  
  395.  
  396.         case IDM_ALLREC: /* play - all menu option */
  397.  
  398.             /* set flag appropriately and play to destination */
  399.             bEnumRange = FALSE;
  400.             bPlayItAll = TRUE;
  401.             bPlayRec = TRUE;
  402.             PlayMetaFileToDest(hWnd, iDestDC);
  403.             break;
  404.  
  405.         case IDM_DESTDISPLAY: /* play - destination - display menu option */
  406.  
  407.             CheckMenuItem(GetMenu(hWnd), IDM_DESTDISPLAY, MF_CHECKED);
  408.             CheckMenuItem(GetMenu(hWnd), IDM_DESTMETA, MF_UNCHECKED);
  409.  
  410.             /* set destination flag to display */
  411.             iDestDC = DESTDISPLAY;
  412.             break;
  413.  
  414.         case IDM_DESTMETA: /* play - destination - metafile menu option */
  415.  
  416.             CheckMenuItem(GetMenu(hWnd), IDM_DESTDISPLAY, MF_UNCHECKED);
  417.             CheckMenuItem(GetMenu(hWnd), IDM_DESTMETA, MF_CHECKED);
  418.  
  419.             /* set destination flag to metafile */
  420.             iDestDC = DESTMETA;
  421.             break;
  422.  
  423.  
  424.         case IDM_HEADER: /* display the common metafile header */
  425.  
  426.            if (bValidFile) {
  427.              lpHeaderDlg = MakeProcInstance(HeaderDlgProc, hInst);
  428.              DialogBox(hInst,
  429.                   "HEADER",
  430.                    hWnd,
  431.                    lpHeaderDlg);
  432.              FreeProcInstance(lpHeaderDlg);
  433.            }
  434.            break;
  435.  
  436.         case IDM_CLIPHDR: /* display the metafilepict of a clipboard file */
  437.  
  438.            if (bValidFile) {
  439.              lpClpHeaderDlg = MakeProcInstance(ClpHeaderDlgProc, hInst);
  440.              DialogBox(hInst,            /* current instance         */
  441.                   "CLIPHDR",                         /* resource to use          */
  442.                    hWnd,                     /* parent handle            */
  443.                    lpClpHeaderDlg);          /* About() instance address */
  444.              FreeProcInstance(lpClpHeaderDlg);
  445.            }
  446.            break;
  447.  
  448.         case IDM_ALDUSHDR: /* display the placeable metafile header */
  449.  
  450.            if (bValidFile) {
  451.              lpAldusHeaderDlg = MakeProcInstance(AldusHeaderDlgProc, hInst);
  452.              DialogBox(hInst,            /* current instance         */
  453.                   "ALDUSHDR",                        /* resource to use          */
  454.                    hWnd,                     /* parent handle            */
  455.                    lpAldusHeaderDlg);                /* About() instance address */
  456.              FreeProcInstance(lpAldusHeaderDlg);
  457.            }
  458.            break;
  459.  
  460.         default:  /* let Windows process it */
  461.             return (DefWindowProc(hWnd, message, wParam, lParam));
  462.         }
  463.         break;
  464.  
  465.     case WM_DESTROY: /* message: window being destroyed */
  466.         PostQuitMessage(0);
  467.         break;
  468.  
  469.  
  470.     default:  /* passes it on if unproccessed */
  471.         return (DefWindowProc(hWnd, message, wParam, lParam));
  472.     }
  473.     return (NULL);
  474. }
  475.  
  476. /***********************************************************************
  477.  
  478.   FUNCTION   : WaitCursor
  479.  
  480.   PARAMETERS : BOOL bWait - TRUE for the hour glass cursor
  481.                 FALSE to return to the previous cursor
  482.  
  483.   PURPOSE    : toggle the mouse cursor to the hourglass and back
  484.  
  485.   CALLS      : WINDOWS
  486.          LoadCursor
  487.          SetCursor
  488.  
  489.   MESSAGES   : none
  490.  
  491.   RETURNS    : void
  492.  
  493.   COMMENTS   :
  494.  
  495.   HISTORY    : 1/16/91 - created - drc
  496.  
  497. ************************************************************************/
  498.  
  499. void WaitCursor(bWait)
  500. BOOL bWait;
  501. {
  502.   HCURSOR hCursor;
  503.   static HCURSOR hOldCursor;
  504.  
  505.   /* if hourglass cursor is to be used */
  506.   if (bWait) {
  507.  
  508.     hCursor = LoadCursor(NULL, IDC_WAIT);
  509.     hOldCursor = SetCursor(hCursor);
  510.   }
  511.   else {
  512.     SetCursor(hOldCursor);
  513.   }
  514. }
  515.