Vector Load and Save

Start with the project you created in Creating an Empty Vector Handle and Attaching it to a Window.

Take the following steps to add vector load and save functionality:

1.

Select File->New menu option, and in the dialog box, select Resource Script and press Enter.

2.

Right click the script you just created. Select Insert and then Menu from the dialog box that appears and click OK.

3.

Create this Menu structure:

IDR_MENU1

     &File
     &Open… with ID = IDM_FILE_OPEN
     &Save… with ID = IDM_FILE_SAVE
     E&xit with ID = IDM_FILE_EXIT

4.

Select File->Save from the menu. Enter Tutorial.rc in the Save As box and click Save.

5.

Select Insert->Files into Project. Browse to the Tutorial.rc file that was just created and click Add.

6.

Add the following line right under #include "c:\lead\include\lvkrn.h":

#include "resource.h"

7.

Change the line:

WndClass.lpszMenuName = NULL;

in the WinMain function to

WndClass.lpszMenuName = MAKEINTRESOURCE( IDR_MENU1 );

8.

Add these local variables under HDC hDC; in the WndProc function:

static OPENFILENAME OpenFileName;
static L_TCHAR szFileName[ _MAX_PATH ];
static L_TCHAR szFileTitle[ _MAX_PATH ];
static L_TCHAR *szFilter[] = { TEXT("All Files\0*.*\0") };
L_INT nRet;

9.

Add these lines right after ResetView( hWnd, &Vector ); in WM_CREATE handle:

// reset the OPENFILENAME structure
memset( &OpenFileName, 0, sizeof( OPENFILENAME ) );
OpenFileName.lStructSize = sizeof( OPENFILENAME );
OpenFileName.hwndOwner = hWnd;
OpenFileName.lpstrFilter = szFilter[ 0 ];
OpenFileName.lpstrFile = szFileName;
OpenFileName.nMaxFile = _MAX_PATH;
OpenFileName.lpstrFileTitle = szFileTitle;
OpenFileName.nMaxFileTitle = _MAX_PATH;

10.

Add these lines after the return 0L; statement of case WM_PAINT:

case WM_COMMAND:
   switch( LOWORD( wParam ) )
   {
      case IDM_FILE_OPEN:
         OpenFileName.Flags = OFN_HIDEREADONLY;
         if( GetOpenFileName( &OpenFileName ) )
         {
            // open the vector file
            nRet = L_VecLoadFile ( szFileName, &Vector, NULL, NULL );
            if( nRet != SUCCESS )
               MessageBox( hWnd, TEXT("Cannot open file!"), TEXT("Error"), MB_ICONEXCLAMATION );

            GetClientRect( hWnd, &Rect );
            L_VecSetViewport ( &Vector, &Rect );

            ResetView( hWnd, &Vector );
         }
         return 0L;

      case IDM_FILE_SAVE:
         OpenFileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
         if( GetSaveFileName( &OpenFileName ) )
         {
            // save the vector file
            nRet = L_VecSaveFile ( szFileName, &Vector, FILE_DXF, NULL );
            if( nRet != SUCCESS )
               MessageBox( hWnd, TEXT("Cannot save file!"), TEXT("Error"), MB_ICONEXCLAMATION );
         }
         return 0L;

      case IDM_FILE_EXIT:
         PostMessage( hWnd, WM_CLOSE, 0L, 0L );

         return 0L;
   }
   break;

11.

Select Insert->Files into Project from the menu. In the dialog box that appears, browse to c:\lead\lib\ltfil_n.lib, select it and click Add.

12.

Compile and run the project. You will be able to view, load and save vector files as you browse your local hard disk.