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:\lead\examples\container). Make sure to select Win32->Win32 Project as the project type. Give the project the name "tutorial" and press Enter, and then click on Finish.
4. Change the path of the output exe file path to the Leadtools Bin directory,
  a. Right click on the project in the Solution Explorer window, and choose Properties.
  b. From the left-side window, choose Configuration Properties -> Linker.
  c. In the right-side window, change the value of the Output File to:

(Please note to change the path to the location of the LEADTOOLS toolkit on your machine, and if you are working on 64 platform, change the Win32 to x64 in the path.)

C:\LEADTOOLS 19\Bin\CDLLVC10\Win32\$(ProjectName).exe

  d. And then press Ok.
5. Double click on the tutorial.rc file from the solution explorer window, and then expand the Menu item, and then double click on 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 solution explorer window, and open stdafx.h file.

8.

Add the following lines at 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:/Program Files/LEAD Technologies/LEADTOOLS 19/Include/L_Bitmap.h" 
#include "C:/Program Files/LEAD Technologies/LEADTOOLS 19/Include/Ltdlg.h" 
#include "C:/Program Files/LEAD Technologies/LEADTOOLS 19/Include/Ltfil.h" 
#include "C:/Program Files/LEAD Technologies/LEADTOOLS 19/Include/ltcon.h" 

9. Right click on 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,
  a. In Solution Explorer, right click on Source Files, select Add, and then select New Item.
  b. In the Add New Item dialog, in the Categories window, choose Visual C++ -> Code, then in the Templates window, choose C++ File (cpp).
  c. In the Name field, enter the name of the file as Imports.cpp, then press Enter.
  d. Now open the Imports.cpp file (if it isnt already open) by double clicking on it in the Solution Explorer window, and copy and paste the following code into it:

#include "StdAfx.h" 
#if defined(FOR_WIN64)  
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\x64\\Ltkrn_x.lib") 
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\x64\\Ltdis_x.lib") 
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\x64\\Ltfil_x.lib") 
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\x64\\Ltdlgkrn_x.lib") 
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\x64\\Ltdlgfile_x.lib") 
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\x64\\Ltcon_x.lib") 
#elif defined(FOR_WIN32)  
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\Win32\\Ltkrn_u.lib") 
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\Win32\\Ltdis_u.lib") 
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\Win32\\Ltfil_u.lib") 
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\Win32\\Ltdlgkrn_u.lib") 
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\Win32\\Ltdlgfile_u.lib") 
   #pragma comment(lib, "C:\\LEADTOOLS 19\\Lib\\CDLL\\Win32\\Ltcon_u.lib") 
#endif // #if defined(FOR_WIN64) 

12.

Now open tutorial.cpp file by double clicking on it in the Solution Explorer.

13.

Search for the body of the WndProc function, then delete the whole function, and then 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 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Container and Automation C API Help