Note: The purpose of this 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.
In the Imports.cpp, add the following lines:
#if defined(WIN64)
#else
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Lttwn_u.lib")
#endif // #if defined(WIN64)
Add the following line in StdAfx.h in the TwainTutorial directory:
#include "..\..\..\include\lttwn.h"
Define the following global variables in Ezfunc.h in the TwainTutorial directory:
HTWAINSESSION hSession = NULL;
L_INT nTransferMode;
Also, add the following defines:
#define IDM_SELECT_SOURCE 200
#define IDM_ACQUIRE 201
#define IDM_NATIVE_TRANSFER 202
#define IDM_MEMORY_TRANSFER 203
#define IDM_FILE_TRANSFER 204
Edit EZFUNC.RC file in the TwainTutorial directory and add the following lines:
#include "EZFUNC.H"
MAIN_MENU MENU
BEGIN
MENUITEM "Select Source" IDM_SELECT_SOURCE
MENUITEM "Acquire" IDM_ACQUIRE
MENUITEM "Native Transfer" IDM_NATIVE_TRANSFER
MENUITEM "Memory Transfer" IDM_MEMORY_TRANSFER
MENUITEM "File Transfer" IDM_FILE_TRANSFER
END
In the InitApplication function change the line:
wcWindowClass.lpszMenuName = NULL; /* No menu */
to be
wcWindowClass.lpszMenuName = TEXT("MAIN_MENU");
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;
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, TEXT("TWAIN Test Application"));
nRet = L_TwainInitSession(&hSession, &AppData);
if (nRet != SUCCESS)
return FALSE;
nRet = L_TwainStartCapsNeg(hSession);
nTransferMode = TWSX_NATIVE;
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);
In the IDM_SELECT_SOURCE menu handler add the following code:
case IDM_SELECT_SOURCE:
{
nRet = L_TwainSelectSource(hSession, NULL);
}
break;
In the IDM_ACQUIRE menu handler add the following code:
case IDM_ACQUIRE:
{
L_TwainEndCapsNeg(hSession);
nRet = L_TwainAcquire(hSession, NULL, sizeof(BITMAPHANDLE), TwainBitmapCB, LTWAIN_SHOW_USER_INTERFACE, NULL, NULL);
if (nRet != SUCCESS)
return nRet;
L_TwainStartCapsNeg(hSession);
InvalidateRect (hWnd, NULL, FALSE);
}
break;
In IDM_NATIVE_TRANSFER menu handler add the following code:
LTWAINPROPERTIES twProps;
memset (&twProps, 0, LTWAINPROPERTIESSIZE);
nRet = L_TwainGetProperties(hSession, &twProps, sizeof(LTWAINPROPERTIES), 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;
nTransferMode = TWSX_NATIVE;
In the IDM_MEMORY_TRANSFER menu handler add the following code:
LTWAINPROPERTIES twProps;
memset (&twProps, 0, LTWAINPROPERTIESSIZE);
nRet = L_TwainGetProperties(hSession, &twProps, sizeof(LTWAINPROPERTIES), 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;
nTransferMode = TWSX_MEMORY;
In the IDM_FILE_TRANSFER menu handler add the following code:
LTWAINPROPERTIES twProps;
memset (&twProps, 0, LTWAINPROPERTIESSIZE);
nRet = L_TwainGetProperties(hSession, &twProps, sizeof(LTWAINPROPERTIES), LTWAIN_PROPERTIES_GETCURRENT);
if (nRet != SUCCESS)
return nRet;
twProps.DataTransfer.nTransferMode = TWSX_FILE;
lstrcpy (twProps.DataTransfer.szFileName, TEXT("c:\\twain.bmp"));
nRet = L_TwainSetProperties(hSession, &twProps, LTWAIN_PROPERTIES_SET, NULL, NULL);
if (nRet != SUCCESS)
return nRet;
nTransferMode = TWSX_FILE;
In Ezfunc.cpp file, add the following function before MainWndProc function:
L_INT EXT_CALLBACK TwainBitmapCB(HTWAINSESSION hSession, pBITMAPHANDLE pBitmap, L_VOID * pUserData)
{
UNREFERENCED_PARAMETER(hSession);
UNREFERENCED_PARAMETER(pUserData);
if (LeadBitmap.Flags.Allocated)
L_FreeBitmap(&LeadBitmap);
if (nTransferMode != TWSX_FILE)
L_CopyBitmap(&LeadBitmap, pBitmap, sizeof(BITMAPHANDLE));
L_FreeBitmap(pBitmap);
return SUCCESS;
}
Compile and test the program.