home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / windxy.zip / WINDXF.C < prev    next >
Text File  |  1990-11-25  |  11KB  |  343 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: windxf.c
  4.  
  5.     PURPOSE: windxf template for Windows applications
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     windxfInit() - initializes window data and registers window
  11.     windxfWndProc() - processes messages
  12.     About() - processes messages for "About" dialog box
  13.  
  14. ****************************************************************************/
  15.  
  16. #include "windows.h"            /* required for all Windows applications */
  17. #include "windxf.h"            /* specific to this program             */
  18.  
  19. HANDLE hInst;                /* current instance                 */
  20. HANDLE hMenu;             /* Handle to my new menu                        */
  21. char   dbpath[256];
  22. HWND   GBL_hwnd;
  23. HDC    GBL_hdc;
  24. BOOL   GBL_draw_flag=FALSE;
  25. int    GBL_draw_mode=0;
  26. HANDLE hmf=(HANDLE)NULL;
  27. HDC    mfhdc;
  28. char   GBL_mfname[256];
  29. extern double wx1, wy1, wx2, wy2;
  30. extern int logextx, logexty;
  31. extern char GBL_mfpath[256];
  32. /****************************************************************************
  33.  
  34.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  35.  
  36.     PURPOSE: calls initialization function, processes message loop
  37.  
  38.     COMMENTS:
  39.  
  40.     This will initialize the window class if it is the first time this
  41.     application is run.  It then creates the window, and processes the
  42.     message loop until a PostQuitMessage is received.  It exits the
  43.     application by returning the value passed by the PostQuitMessage.
  44.  
  45. ****************************************************************************/
  46.  
  47. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  48. HANDLE hInstance;                 /* current instance         */
  49. HANDLE hPrevInstance;                 /* previous instance         */
  50. LPSTR lpCmdLine;                 /* command line             */
  51. int nCmdShow;                     /* show-window type (open/icon) */
  52. {
  53.     HWND hWnd;                     /* window handle             */
  54.     MSG msg;                     /* message                 */
  55.  
  56.  
  57.    if (!hPrevInstance)            /* Has application been initialized? */
  58.     if (!windxfInit(hInstance))
  59.         return (NULL);           /* Exits if unable to initialize     */
  60.  
  61.    hInst = hInstance;            /* Saves the current instance         */
  62.  
  63.    hMenu = LoadMenu(hInstance, "windxfMenu");
  64.    hWnd = CreateWindow("windxf",  /* window class         */
  65.      "Windows DXF Viewer",         /* window name         */
  66.     WS_OVERLAPPEDWINDOW,                /* window style         */
  67.     CW_USEDEFAULT,                  /* x position             */
  68.     CW_USEDEFAULT,                  /* y position             */
  69.     CW_USEDEFAULT,                  /* width             */
  70.     CW_USEDEFAULT,                  /* height             */
  71.     NULL,                      /* parent handle         */
  72.     hMenu,                      /* menu or child ID         */
  73.     hInstance,                  /* instance             */
  74.     NULL);                      /* additional info         */
  75.  
  76.     if (!hWnd)                      /* Was the window created? */
  77.     return (NULL);
  78.  
  79.     ShowWindow(hWnd, nCmdShow);              /* Shows the window         */
  80.     UpdateWindow(hWnd);                  /* Sends WM_PAINT message  */
  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: windxfInit(HANDLE)
  97.  
  98.     PURPOSE: Initializes window data and registers window class
  99.  
  100. ****************************************************************************/
  101.  
  102. BOOL windxfInit(hInstance)
  103. HANDLE hInstance;                   /* current instance         */
  104. {
  105.     HANDLE hMemory;                   /* handle to allocated memory */
  106.     PWNDCLASS pWndClass;               /* structure pointer         */
  107.     BOOL bSuccess;                   /* RegisterClass() result     */
  108.  
  109.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  110.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  111.  
  112.     pWndClass->style = NULL;
  113.     pWndClass->lpfnWndProc = windxfWndProc;
  114.     pWndClass->hInstance = hInstance;
  115.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  116.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  117.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  118.     pWndClass->lpszMenuName = "windxfMenu";
  119.     pWndClass->lpszClassName = (LPSTR) "windxf";
  120.  
  121.     bSuccess = RegisterClass(pWndClass);
  122.  
  123.     LocalUnlock(hMemory);                /* Unlocks the memory    */
  124.     LocalFree(hMemory);                    /* Returns it to Windows */
  125.  
  126.     return (bSuccess);         /* Returns result of registering the window */
  127. }
  128.  
  129. /****************************************************************************
  130.  
  131.     FUNCTION: windxfWndProc(HWND, unsigned, WORD, LONG)
  132.  
  133.     PURPOSE:  Processes messages
  134.  
  135.     MESSAGES:
  136.  
  137.     WM_CREATE    - create window
  138.     WM_DESTROY    - destroy window
  139.     WM_COMMAND    - menu selections and others
  140.  
  141.     COMMENTS:
  142.  
  143.  
  144. ****************************************************************************/
  145.  
  146. long FAR PASCAL windxfWndProc(hWnd, message, wParam, lParam)
  147. HWND hWnd;                  /* window handle             */
  148. unsigned message;              /* type of message             */
  149. WORD wParam;                  /* additional information         */
  150. LONG lParam;                  /* additional information         */
  151. {
  152.  
  153.    static FARPROC  lpfnFILEDIALOGPROC;
  154.    static FARPROC  lpfnMETADIALOGPROC;
  155.    static FARPROC  lpfnMFPROC;
  156.    static FARPROC lpProcAbout;          /* pointer to the "About" function */
  157.    PAINTSTRUCT     ps;
  158.    static LOGPEN   lpBlack = {PS_SOLID, 1, 1, RGB(0,0,0)};
  159.    HPEN            hPenBlack;
  160.    RECT            rect;
  161.    BOOL            result;
  162.    GLOBALHANDLE    hGmem;
  163.    LPMETAFILEPICT  lpMFP;
  164.  
  165.    GBL_hwnd = hWnd;
  166.     switch (message) {
  167.  
  168.     case WM_CREATE:                /* message: window being created */
  169.         break;
  170.  
  171.     case WM_DESTROY:          /* message: window being destroyed */
  172.       if (hmf != (HANDLE)NULL)
  173.           DeleteMetaFile(hmf);
  174.         PostQuitMessage(0);
  175.         break;
  176.  
  177.     case WM_COMMAND:
  178.         switch (wParam) {
  179.  
  180.         case IDM_OPEN:
  181.          lpfnFILEDIALOGPROC = MakeProcInstance((FARPROC)FILEDIALOGPROC, hInst);
  182.          DialogBox(hInst, "FILEDIALOG", hWnd, lpfnFILEDIALOGPROC);
  183.          FreeProcInstance(lpfnFILEDIALOGPROC);
  184.          InvalidateRect(hWnd, NULL, TRUE);
  185.          GBL_draw_flag = TRUE;
  186.          GBL_draw_mode = 0;
  187.          break;
  188.  
  189.       case IDM_MFOPEN:
  190.          lpfnMFPROC = MakeProcInstance((FARPROC)MFPROC, hInst);
  191.          DialogBox(hInst, "MFDIALOG", hWnd, lpfnMFPROC);
  192.          FreeProcInstance(lpfnMFPROC);
  193.          InvalidateRect(hWnd, NULL, TRUE);
  194.          GBL_draw_flag = TRUE;
  195.          GBL_draw_mode = 4;
  196.          break;
  197.  
  198.       case IDM_DRAW:
  199.          InvalidateRect(hWnd, NULL, TRUE);
  200.          GBL_draw_mode = 1;
  201.          break;
  202.  
  203.       case IDM_DRMETA:
  204.          lpfnMETADIALOGPROC = MakeProcInstance((FARPROC)MFNAMEPROC, hInst);
  205.          result = DialogBox(hInst, "MFNAME", hWnd, lpfnMETADIALOGPROC);
  206.          FreeProcInstance(lpfnMETADIALOGPROC);
  207.          if (result)
  208.             {
  209.              GBL_draw_mode = 2;
  210.              InvalidateRect(hWnd, NULL, TRUE);
  211.             }
  212.          break;
  213.  
  214.       case IDM_CLIP:
  215.  
  216.             /* Copy the current metafile to the clipboard */
  217.             hGmem = GlobalAlloc(GHND, (DWORD)sizeof(METAFILEPICT) );
  218.             lpMFP = (LPMETAFILEPICT) GlobalLock(hGmem);
  219.             GetClientRect( hWnd, &rect);
  220.             lpMFP->mm = MM_ISOTROPIC;
  221.             lpMFP->xExt = rect.right;
  222.             lpMFP->yExt = rect.bottom;
  223.             lpMFP->hMF = hmf;
  224.             GlobalUnlock(hGmem);
  225.             OpenClipboard(hWnd);
  226.             EmptyClipboard();
  227.             SetClipboardData(CF_METAFILEPICT, hGmem);
  228.             CloseClipboard();
  229.  
  230.             /* Redraw from the memory metafile */
  231.             GBL_draw_mode = 3;
  232.             InvalidateRect(hWnd, NULL, TRUE);
  233.             break;
  234.  
  235.         case IDM_ABOUT:
  236.            lpProcAbout = MakeProcInstance(About, hInst);
  237.            DialogBox(hInst, "AboutBox",  hWnd,  lpProcAbout);
  238.            FreeProcInstance(lpProcAbout);
  239.            break;
  240.  
  241.         case IDM_EXIT:
  242.             DestroyWindow(hWnd);
  243.             break;
  244.  
  245.         }
  246.         break;
  247.  
  248.    case WM_PAINT:
  249.       if (GBL_draw_flag)
  250.         {
  251.          switch(GBL_draw_mode)
  252.          {
  253.          case 0: /* Draw to Screen from dxf */
  254.             GetDbPath(dbpath);
  255.             mfhdc = CreateMetaFile(NULL);
  256.             GBL_hdc = BeginPaint(hWnd, &ps);
  257.             SetMapMode(GBL_hdc, MM_ISOTROPIC);
  258.             GetClientRect( hWnd, &rect);
  259.             SetViewportExt(GBL_hdc, rect.right, rect.bottom);
  260.  
  261.             SetWindowExt(GBL_hdc, 1000, 1000);
  262.             hPenBlack = CreatePenIndirect(&lpBlack);
  263.             SelectObject(GBL_hdc, hPenBlack);
  264.  
  265.             ReadDXF(dbpath);
  266.  
  267.             EndPaint(hWnd, &ps);
  268.             hmf = CloseMetaFile(mfhdc);
  269.             DeleteObject(hPenBlack);
  270.             break;
  271.          case 1: /* Redraw Screen from memory metafile */
  272.             GBL_hdc = BeginPaint(hWnd, &ps);
  273.             SetMapMode(GBL_hdc, MM_ISOTROPIC);
  274.             GetClientRect( hWnd, &rect);
  275.             SetViewportExt(GBL_hdc, rect.right, rect.bottom);
  276.             SetWindowExt(GBL_hdc, 1000, 1000);
  277.             PlayMetaFile(GBL_hdc, hmf);
  278.             EndPaint(hWnd, &ps);
  279.             break;
  280.  
  281.          case 2: /* Draw to Metafile from screen */
  282.             CopyMetaFile(hmf,GBL_mfname);
  283.             GBL_draw_mode = 1;
  284.             break;
  285.  
  286.          case 3: /* Copy contents of screen to clipboard */
  287.             GBL_hdc = BeginPaint(hWnd, &ps);
  288.             SetMapMode(GBL_hdc, MM_ISOTROPIC);
  289.             GetClientRect( hWnd, &rect);
  290.             SetViewportExt(GBL_hdc, rect.right, rect.bottom);
  291.             SetWindowExt(GBL_hdc, 1000, 1000);
  292.             PlayMetaFile(GBL_hdc, hmf);
  293.             EndPaint(hWnd, &ps);
  294.             break;
  295.  
  296.          case 4: /* Read from metafile */
  297.             GBL_hdc = BeginPaint(hWnd, &ps);
  298.             hmf = GetMetaFile(GBL_mfpath);
  299.             SetMapMode(GBL_hdc, MM_ISOTROPIC);
  300.             GetClientRect( hWnd, &rect);
  301.             SetViewportExt(GBL_hdc, rect.right, rect.bottom);
  302.             SetWindowExt(GBL_hdc, 1000, 1000);
  303.             PlayMetaFile(GBL_hdc, hmf);
  304.             EndPaint(hWnd, &ps);
  305.             break;
  306.        }
  307.        GBL_draw_mode = 1;
  308.        }
  309.        else
  310.         {
  311.          GBL_hdc = BeginPaint(hWnd, &ps);
  312.          EndPaint(hWnd, &ps);
  313.         }
  314.          break;
  315.  
  316.     default:              /* Passes it on if unproccessed    */
  317.         return (DefWindowProc(hWnd, message, wParam, lParam));
  318.     }
  319.     return (NULL);
  320. }
  321.  
  322. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  323. HWND hDlg;                                /* window handle of the dialog box */
  324. unsigned message;                         /* type of message                 */
  325. WORD wParam;                              /* message-specific information    */
  326. LONG lParam;
  327. {
  328.     switch (message) {
  329.     case WM_INITDIALOG:           /* message: initialize dialog box */
  330.         return (TRUE);
  331.  
  332.     case WM_COMMAND:              /* message: received a command */
  333.         if (wParam == IDOK                /* "OK" box selected?         */
  334.                 || wParam == IDCANCEL) {      /* System menu close command? */
  335.         EndDialog(hDlg, TRUE);          /* Exits the dialog box         */
  336.         return (TRUE);
  337.         }
  338.         break;
  339.     }
  340.     return (FALSE);                  /* Didn't process a message    */
  341. }
  342.  
  343.