Tutorials

Take the following steps to create and run a program that implements LEADTOOLS TWAIN features. Remember, the purpose of the TWAIN tutorial is to provide you a quick and easy way to generate a TWAIN program. For more in depth TWAIN programming, refer to the TWAIN demo.

 

1.

Create a new directory in the LTWINxxx\EXAMPLES\DLL directory called EZTWN.

2.

Copy everything in the EZFUNC directory into the EZTWN directory.

3.

Rename the EZFunc.c file to be EZFunc.cpp.

4.

Remove the EXFunc.c from the project and add the EZFunc.cpp in its place.

5.

Compile the project as it is and run EZFUNC32.exe to familiarize yourself with the basic program. You can run the exe file from the MS-DOS prompt, or set the image file to load through the Project->Settings->Debug->Program Arguments menu.

6.

On the Insert menu, select Files into Project. Add the Lttwn_n.lib file to the project from the LTWINxxx\LIB directory.

7.

Define the following global variables in EZFUNC.CPP in the EZTWN directory:

 

BITMAPHANDLE LeadBitmap;   /* Bitmap handle to hold the loaded image. */
HTWAINSESSION hSession = NULL;

 

8.

In the project workspace go to the resources tab. Add a menu ("MAIN_MENU") to the application with the following menu items:

 

ID

Caption

 

IDM_SELECT_SOURCE

Select Source

 

IDM_ACQUIRE

Acquire

 

IDM_NATIVE_TRANSFER

Native Transfer

 

IDM_MEMORY_TRANSFER

Memory Transfer

 

IDM_FILE_TRANSFER

File Transfer

9.

In the InitApplication function change the line:

 

wcWindowClass.lpszMenuName = NULL;  /* No menu */
to be
wcWindowClass.lpszMenuName = TEXT("MAIN_MENU");

10.

In the EZFunc.cpp add the following includes:

 

#include "resource.h"
#include "LTWINxxx\Include\ltTwain.h"

11.

In the MainWndProc function and before the handling of the WM_PALETTECHANGED message add the following code:

 

   case WM_COMMAND: 
      {
         switch (LOWORD(wParam)) 
         {
         case IDM_SELECT_SOURCE: 
            {
            }
            break; 
         case IDM_ACQUIRE: 
            {
            }
            break; 
         case IDM_NATIVE_TRANSFER: 
            {
            }
            break; 
         case IDM_MEMORY_TRANSFER: 
            {
            }
            break; 
         case IDM_FILE_TRANSFER: 
            {
            }
            break; 
         }
      }
      break;

12.

In the WM_CREATE message handler add the following code at the end of it (after SendMessage (hWnd, WM_QUERYNEWPALETTE, 0, 0L);):

 

APPLICATIONDATA AppData; 
AppData.uStructSize = sizeof(APPLICATIONDATA);
AppData.hWnd = hWnd; 
lstrcpy (AppData.szManufacturerName, TEXT("LEAD Technologies, Inc.")); 
lstrcpy (AppData.szAppProductFamily, TEXT("LEAD Test Applications"));
lstrcpy (AppData.szVersionInfo, TEXT("Version 1.0"));
lstrcpy (AppData.szAppName, TXT("TWAIN Test Application"));
nRet = L_TwainInitSession(&hSession, &AppData); 
if (nRet != SUCCESS) 
   return FALSE; 
nRet = L_TwainStartCapsNeg(hSession); 

13.

In the WM_DESTROY message handler add the following code at the end of it (before the PostQuitMessage (0);):

 

L_TwainEndCapsNeg(hSession); 
L_TwainEndSession
(&hSession); 

14.

In the IDM_SELECT_SOURCE menu handler add the following code:

 

case IDM_SELECT_SOURCE:
   {
      nRet = L_TwainSelectSource(hSession, NULL); 
   }
   break; 

15.

In the IDM_ACQUIRE menu handler add the following code:

 

case IDM_ACQUIRE: 
   {
      if (LeadBitmap.Flags.Allocated) 
         L_FreeBitmap(&LeadBitmap); 
      L_TwainEndCapsNeg(hSession); 
      nRet = L_TwainAcquire(hSession, &LeadBitmap, sizeof(BITMAPHANDLE), NULL, LTWAIN_SHOW_USER_INTERFACE, NULL, NULL); 
      if (nRet != SUCCESS) 
         return nRet; 
      L_TwainStartCapsNeg(hSession); 
      InvalidateRect (hWnd, NULL, FALSE); 
   }
   break; 

16.

In IDM_NATIVE_TRANSFER menu handler add the following code:

 

LTWAINPROPERTIES twProps; 
memset (&twProps, 0, LTWAINPROPERTIESSIZE); 
nRet = L_TwainGetProperties(hSession, &twProps, LTWAIN_PROPERTIES_GETCURRENT); 
if (nRet != SUCCESS) 
   return nRet; 
twProps.DataTransfer.nTransferMode = TWSX_NATIVE; 
nRet = L_TwainSetProperties(hSession, &twProps, LTWAIN_PROPERTIES_SET, NULL, NULL); 
if (nRet != SUCCESS) 
   return nRet; 

17.

In the IDM_MEMORY_TRANSFER menu handler add the following code:

 

LTWAINPROPERTIES twProps; 
memset (&twProps, 0, LTWAINPROPERTIESSIZE); 
nRet = L_TwainGetProperties(hSession, &twProps, LTWAIN_PROPERTIES_GETCURRENT); 
if (nRet != SUCCESS) 
   return nRet; 
twProps.DataTransfer.nTransferMode = TWSX_MEMORY; 
twProps.DataTransfer.nBufMemCompression = TWCP_NONE; 
nRet = L_TwainSetProperties(hSession, &twProps, LTWAIN_PROPERTIES_SET, NULL, NULL); 
if (nRet != SUCCESS) 
   return nRet; 

18.

In the IDM_FILE_TRANSFER menu handler add the following code:

 

LTWAINPROPERTIES twProps; 
memset (&twProps, 0, LTWAINPROPERTIESSIZE); 
nRet = L_TwainGetProperties(hSession, &twProps, LTWAIN_PROPERTIES_GETCURRENT); 
if (nRet != SUCCESS) 
   return nRet; 
twProps.DataTransfer.nTransferMode = TWSX_NATIVE; 
lstrcpy (twProps.DataTransfer.szFileName, TEXT("c:\\twain.bmp"));
nRet = L_TwainSetProperties(hSession, &twProps, LTWAIN_PROPERTIES_SET, NULL, NULL); 
if (nRet != SUCCESS) 
   return nRet; 

19.

Compile and test the program.