Implementing Containers - Initializing, Creating and Freeing

Take the following steps to start a project and add some code that will create a new container handle:

  1. Start Visual C++, version 2005.

  2. Select the File->New->Project menu option.

  3. In the New Project dialog box, enter the location (C:\LEADTOOLS22\Examples\Container). Make sure to select the Win32->Win32 Project as the project type. Give the project the name "tutorial" and press Enter, and then click Finish.

  4. Change the path of the output exe file path to the LEADTOOLS Bin directory as follows:

    1. Right-click the project in the Solution Explorer window, and choose Properties.
    2. From the left-side window, choose Configuration Properties > Linker.
    3. In the right-side window, change the value of the Output File to the location of the LEADTOOLS toolkit on your machine, and if you are working on an x64 platform, change the Win32 in the path to x64. The default location is
      C:\\LEADTOOLS22\\Bin\\CDLL\\Win32\\$(ProjectName).exe
    4. Press OK.
  5. Double-click the tutorial.rc file from the Solution Explorer window, and then expand the Menu item, and then double-click the IDC_TUTORIAL menu.

  6. Modify the File Menu to match the following structure:

    IDR_TUTORIAL 
       &File 
          &Open  with ID = IDM_FILE_OPEN 
          E&xit   with ID = IDM_EXIT 

  7. Now go back to the Solution Explorer window, and open the stdafx.h file.

  8. Append the following lines to the end of the file: (Please note to change the path to the location of the LEADTOOLS toolkit on your machine)

    #include <CommDlg.h> 
    // LEAD Header Files 
    #include "C:/LEADTOOLS22/Include/L_Bitmap.h" 
    #include "C:/LEADTOOLS22/Include/Ltdlg.h" 
    #include "C:/LEADTOOLS22/Include/Ltfil.h" 
    #include "C:/LEADTOOLS22/Include/ltcon.h" 

  9. Right-click Tutorial.rc, and select View Code.

  10. Search for the IDS_APP_TITLE id, and change its value from "tutorial" to "Container Tutorial".

  11. Create a new file called Imports.cpp as follows:

    1. In the Solution Explorer, right-click Source Files, select Add, and then select New Item.
    2. In the Add New Item dialog, in the Categories window, choose Visual C++ -> Code. Then in the Templates window, choose C++ File (cpp).
    3. In the Name field, enter the name of the file as Imports.cpp, then press Enter.
    4. Now open the Imports.cpp file (if it is not already open) by double-clicking it in the Solution Explorer window. Copy and paste the following code into it:
      #include "StdAfx.h" 
      #if defined(FOR_WIN64)  
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\x64\\Ltkrn_x.lib") 
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\x64\\Ltdis_x.lib") 
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\x64\\Ltfil_x.lib") 
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\x64\\Ltdlgkrn_x.lib") 
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\x64\\Ltdlgfile_x.lib") 
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\x64\\Ltcon_x.lib") 
      #elif defined(FOR_WIN32)  
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\Win32\\Ltkrn_u.lib") 
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\Win32\\Ltdis_u.lib") 
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\Win32\\Ltfil_u.lib") 
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\Win32\\Ltdlgkrn_u.lib") 
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\Win32\\Ltdlgfile_u.lib") 
         #pragma comment(lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\Win32\\Ltcon_u.lib") 
      #endif // #if defined(FOR_WIN64) 
  12. Now open the tutorial.cpp file by double-clicking it in the Solution Explorer.

  13. Search for the body of the WndProc function, and then delete the entire function. Copy the following code and insert it in its place.
    static L_VOID OnSize 
    ( 
    HWND hWnd, 
    pBITMAPHANDLE pBitmap, 
    LPRECT prcView, 
    L_INT cx, 
    L_INT cy, 
    L_INT nZoom, 
    L_INT *pnHScroll, 
    L_INT *pnVScroll 
    ) 
    { 
       SCROLLINFO si ; 
       L_INT      nXOffset, nYOffset ; 
        
        
       if ( NULL != pBitmap && pBitmap->Flags.Allocated ) 
       { 
          si.cbSize = sizeof ( SCROLLINFO ) ; 
          si.fMask  = SIF_ALL ; 
           
          // vertical scroll. 
          GetScrollInfo ( hWnd, SB_VERT, &si ) ; 
          si.nMin  = 0 ; 
          si.nMax  = BITMAPHEIGHT ( pBitmap ) ; 
          si.nPage = MulDiv ( cy, 100, nZoom ) ; 
           
          SetScrollInfo ( hWnd, SB_VERT, &si, TRUE ) ; 
           
          GetScrollInfo ( hWnd, SB_VERT, &si ) ; 
           
          *pnVScroll = si.nPos ; 
           
          // horizontal scroll 
          GetScrollInfo ( hWnd, SB_HORZ, &si ) ; 
          si.nMin  = 0 ; 
          si.nMax  = BITMAPWIDTH ( pBitmap ) ; 
          si.nPage = MulDiv ( cx, 100, nZoom ) ; 
           
          SetScrollInfo ( hWnd, SB_HORZ, &si, TRUE ) ; 
           
          GetScrollInfo ( hWnd, SB_HORZ, &si ) ; 
           
          *pnHScroll = si.nPos ; 
       } 
       else 
       { 
          si.cbSize = sizeof ( SCROLLINFO ) ; 
          si.fMask  = SIF_RANGE ; 
          si.nMin   = 0 ; 
          si.nMax   = 0 ; 
           
          SetScrollInfo ( hWnd, SB_VERT, &si, TRUE ) ; 
          SetScrollInfo ( hWnd, SB_HORZ, &si, TRUE ) ; 
           
          *pnHScroll = 0 ; 
          *pnVScroll = 0 ; 
       } 
        
       // set the painting rectangle. 
       SetRect ( prcView, 0, 0, BITMAPWIDTH ( pBitmap ), BITMAPHEIGHT ( pBitmap ) ) ; 
        
       if ( nZoom < 100 ) 
       { 
          nXOffset = MulDiv ( *pnHScroll, 100, nZoom ) ; 
          nYOffset = MulDiv ( *pnVScroll, 100, nZoom ) ; 
       } 
       else 
       { 
          nXOffset = *pnHScroll ; 
          nYOffset = *pnVScroll ; 
       } 
        
       OffsetRect ( prcView, - nXOffset, - nYOffset ) ; 
        
       prcView->left   = MulDiv ( prcView->left,   nZoom, 100 ) ; 
       prcView->top    = MulDiv ( prcView->top,    nZoom, 100 ) ; 
       prcView->right  = MulDiv ( prcView->right,  nZoom, 100 ) ; 
       prcView->bottom = MulDiv ( prcView->bottom, nZoom, 100 ) ; 
    } 
    L_VOID OnHScroll 
    ( 
    HWND hWnd, 
    L_UINT code, 
    L_INT nZoom, 
    LPRECT prcView, 
    L_INT *pnHScroll, 
    L_INT *pnVScroll 
    ) 
    { 
       SCROLLINFO si ; 
        
        
       si.cbSize = sizeof (SCROLLINFO) ; 
       si.fMask  = SIF_ALL ; 
       GetScrollInfo (hWnd, SB_HORZ, &si) ; 
        
       *pnHScroll = si.nPos ; 
        
       switch ( code ) 
       { 
          case SB_LINELEFT: 
          si.nPos -= 1 ; 
          break ; 
           
          case SB_LINERIGHT: 
          si.nPos += 1 ; 
          break ; 
           
          case SB_PAGELEFT: 
          si.nPos -= si.nPage ; 
          break ; 
           
          case SB_PAGERIGHT: 
          si.nPos += si.nPage ; 
          break ; 
           
          case SB_THUMBTRACK: 
          si.nPos = si.nTrackPos ; 
          break ; 
           
    default: 
          break ; 
       } 
        
       si.fMask = SIF_POS ; 
       SetScrollInfo (hWnd, SB_HORZ, &si, TRUE) ; 
       GetScrollInfo (hWnd, SB_HORZ, &si) ; 
        
       if ( si.nPos != *pnHScroll ) 
       { 
          L_INT dxUpdate ; 
           
           
          // update screen. 
          if ( nZoom >= 100 ) 
          { 
             dxUpdate = MulDiv ( ( *pnHScroll - si.nPos ), nZoom, 100 ) ; 
          } 
          else 
          { 
             dxUpdate = *pnHScroll - si.nPos ; 
          } 
           
          // update screen. 
          OffsetRect ( prcView, dxUpdate, 0 ) ; 
           
          ScrollWindow ( hWnd, dxUpdate, 0, NULL, NULL ) ; 
           
          *pnHScroll = si.nPos ; 
           
          UpdateWindow ( hWnd ) ; 
       } 
    } 
    L_VOID OnVScroll 
    ( 
    HWND hWnd, 
    L_UINT code, 
    L_INT nZoom, 
    LPRECT prcView, 
    L_INT *pnHScroll, 
    L_INT *pnVScroll 
    ) 
    { 
       SCROLLINFO si ; 
        
        
       si.cbSize = sizeof ( SCROLLINFO ) ; 
       si.fMask  = SIF_ALL ; 
       GetScrollInfo (hWnd, SB_VERT, &si) ; 
        
       *pnVScroll = si.nPos ; 
        
       switch ( code ) 
       { 
          case SB_LINEUP: 
          si.nPos -= 1 ; 
          break ; 
           
          case SB_LINEDOWN: 
          si.nPos += 1 ; 
          break ; 
           
          case SB_PAGEUP: 
          si.nPos -= si.nPage ; 
          break ; 
           
          case SB_PAGEDOWN: 
          si.nPos += si.nPage ; 
          break ; 
           
          case SB_THUMBTRACK: 
          si.nPos = si.nTrackPos ; 
          break ; 
           
    default: 
          break ; 
       } 
        
       si.fMask = SIF_POS ; 
       SetScrollInfo ( hWnd, SB_VERT, &si, TRUE ) ; 
       GetScrollInfo ( hWnd, SB_VERT, &si ) ; 
        
       if ( si.nPos != *pnVScroll ) 
       { 
          L_INT dyUpdate ; 
           
           
          // update screen. 
          if ( nZoom >= 100 ) 
          { 
             dyUpdate = MulDiv ( ( *pnVScroll - si.nPos ), nZoom, 100 ) ; 
          } 
          else 
          { 
             dyUpdate = *pnVScroll - si.nPos ; 
          } 
           
          // update screen. 
          OffsetRect ( prcView, 0, dyUpdate ) ; 
           
          ScrollWindow ( hWnd, 0, dyUpdate, NULL, NULL ) ; 
           
          *pnVScroll = si.nPos ; 
           
          UpdateWindow ( hWnd ) ; 
       } 
    } 
    L_VOID OnPaint 
    ( 
    HWND hWnd, 
    pBITMAPHANDLE pBitmap, 
    LPRECT prcView, 
    HPALETTE hPalette 
    ) 
    { 
       HDC hDC ; 
       PAINTSTRUCT ps ; 
       HPALETTE hOldPal = NULL ; 
        
        
       hDC = BeginPaint (hWnd, &ps) ; 
        
       if ( pBitmap->Flags.Allocated ) 
       { 
          if ( hPalette ) 
          { 
             hOldPal = SelectPalette ( hDC, hPalette, TRUE ) ; 
             RealizePalette ( hDC ) ; 
          } 
           
          L_PaintDC  ( hDC, pBitmap, NULL, NULL, prcView, &ps.rcPaint, SRCCOPY ) ; 
           
          if ( NULL != hOldPal ) 
          { 
             SelectPalette ( hDC, hOldPal, TRUE ) ; 
          } 
       } 
        
       EndPaint (hWnd, &ps) ; 
    } 
    static L_BOOL OnOpen ( HWND hWnd, pBITMAPHANDLE pBitmap ) 
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
    { 
       int wmId, wmEvent; 
        
       static BITMAPHANDLE hBitmap ; 
       static RECT rcView ; 
       static HPALETTE hPalette = NULL ; 
       static L_INT nVScroll = 0 ; 
       static L_INT nHScroll = 0 ; 
       static L_INT nZoomFactor = 100 ; 
       switch (message) 
       { 
          case WM_CREATE: 
          L_DlgInit (DLG_INIT_COLOR) ; 
          break; 
          case WM_COMMAND: 
          wmId    = LOWORD(wParam); 
          wmEvent = HIWORD(wParam); 
          // Parse the menu selections: 
          switch (wmId) 
          { 
             case IDM_ABOUT: 
             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 
             break; 
             case IDM_EXIT: 
             DestroyWindow(hWnd); 
             return 0; 
             case IDM_FILE_OPEN: 
             { 
                BITMAPHANDLE hTempBitmap ; 
                HDC hDC ; 
                if ( OnOpen ( hWnd, &hTempBitmap ) ) 
                { 
                   if ( hBitmap.Flags.Allocated ) 
                   { 
                      L_FreeBitmap  ( &hBitmap ) ; 
                   } 
                    
                   if ( hPalette ) 
                   { 
                      DeleteObject ( hPalette ) ; 
                   } 
                    
                   L_CopyBitmapHandle  ( &hBitmap, &hTempBitmap, sizeof(BITMAPHANDLE) ) ; 
                    
                   hDC = GetDC ( hWnd ) ; 
                    
                   hPalette = L_CreatePaintPalette  ( hDC, &hBitmap ) ; 
                    
                   ReleaseDC ( hWnd, hDC ) ; 
                    
                   {//ADJUST WINDOW SIZE 
                       
                      RECT  rcWindow ; 
                      L_INT nWidth, nHeight ; 
                       
                       
                      SystemParametersInfo ( SPI_GETWORKAREA, 0, &rcWindow, 0 ) ; 
                       
                      nWidth = min ( MulDiv ( BITMAPWIDTH ( &hBitmap ), nZoomFactor, 100 ), 
                      ( rcWindow.right  - rcWindow.left ) ) ; 
                       
                      nHeight = min ( MulDiv ( BITMAPHEIGHT ( &hBitmap ), nZoomFactor, 100 ), 
                      ( rcWindow.bottom - rcWindow.top  ) ) ; 
                       
                      MoveWindow ( hWnd, 
                      0, 
                      0, 
                      nWidth, 
                      nHeight, 
                      TRUE ) ; 
                       
                   }//ADJUST WINDOW SIZE 
                    
                   InvalidateRect ( hWnd, NULL, TRUE ) ; 
                } 
             } 
          } 
          return 0; 
          case WM_SIZE: 
          OnSize ( hWnd, 
          &hBitmap, 
          &rcView, 
          LOWORD ( lParam ), 
          HIWORD ( lParam ), 
          nZoomFactor, 
          &nHScroll, 
          &nVScroll ) ; 
          break ; 
           
          case WM_HSCROLL: 
          OnHScroll ( hWnd, 
          LOWORD ( wParam ), 
          nZoomFactor, 
          &rcView, 
          &nHScroll, 
          &nVScroll ) ; 
          break ; 
           
          case WM_VSCROLL: 
          OnVScroll ( hWnd, 
          LOWORD ( wParam ), 
          nZoomFactor, 
          &rcView, 
          &nHScroll, 
          &nVScroll ) ; 
           
          break ; 
          case WM_PAINT: 
          OnPaint ( hWnd, &hBitmap, &rcView, hPalette ) ; 
          break; 
          case WM_DESTROY: 
          if ( hBitmap.Flags.Allocated ) 
          { 
             L_FreeBitmap  ( &hBitmap ) ; 
          } 
          if (hPalette) 
          { 
             DeleteObject ( hPalette ) ; 
          } 
          PostQuitMessage(0); 
          return 0; 
       } 
       return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
  14. Add the following declaration to the WndProc function:
    static pCONTAINERHANDLE pContainer ;  
  15. Replace the line "break;" statement at the end of "case WM_CREATE:" in the WndProc function with the following code:
    if ( SUCCESS == L_ContainerInit ( &pContainer ) ) 
    { 
       L_ContainerCreate ( pContainer, hWnd ) ; 
       return 0L ; 
    } 
    else 
    { 
       return -1L ; 
    } 
  16. Add the following lines to the WndProc function, before the statement "PostQuitMessage" in the "case WM_DESTROY:"
    if ( SUCCESS == L_ContainerIsValid ( pContainer ) ) 
    { 
       L_ContainerFree ( pContainer ) ; 
    } 
  17. Add the following code to the WndProc function, before the InvalidateRect function call in the "case IDM_FILE_OPEN:" statement:
    L_ContainerReset ( pContainer ) ; 
  18. Compile and run the project by selecting Build->Rebuild Solution from the menu, and then Debug->Start Without Debugging.
Help Version 22.0.2021.8.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Container and Automation C API Help
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.