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.
To Get or Set a TWAIN capability using LEADTOOLS:
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 TwainCap directory:
#include "..\..\..\include\lttwn.h" Define the following global variables in Ezfunc.h in the TwainCap directory:
HTWAINSESSION hSession;TW_CAPABILITY twCap;L_UINT uItemValue;Also, add the following defines:#define IDM_SELECT_SOURCE 200#define IDM_GET_CAPABILITY 201#define IDM_SET_CAPABILITY 202#define IDM_SET_BRIGHTNESS 203#define IDM_ACQUIRE 204
Edit EZFUNC.RC file in the TwainCap directory and add the following lines:
#include "EZFUNC.H"MAIN_MENU MENUBEGINMENUITEM "Select Source" IDM_SELECT_SOURCEMENUITEM "Get Capability" IDM_GET_CAPABILITYMENUITEM "Set Capability" IDM_SET_CAPABILITYMENUITEM "Set Brightness" IDM_SET_BRIGHTNESSMENUITEM "Acquire" IDM_ACQUIREEND
In the InitApplication function change the line:
wcWindowClass.lpszMenuName = NULL; /* No menu */to bewcWindowClass.lpszMenuName = TEXT("MAIN_MENU");
In the MainWndProc function, 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_GET_CAPABILITY:{}break;case IDM_SET_CAPABILITY:{}break;case IDM_SET_BRIGHTNESS:{}break;case IDM_ACQUIRE:{}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);if(nRet != SUCCESS)return FALSE;
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_GET_CAPABILITY menu handler add the following code:
case IDM_GET_CAPABILITY:{L_INT nItemType;twCap.Cap = ICAP_XFERMECH;twCap.ConType = TWON_DONTCARE16;twCap.hContainer = NULL;L_TwainGetCapability(hSession, &twCap, LTWAIN_CAPABILITY_GETCURRENT);L_TwainGetNumericContainerItemType(&twCap, &nItemType);if (nItemType == TWTY_UINT16)L_TwainGetNumericContainerUINTValue(&twCap, 0, &uItemValue);elseMessageBox(hWnd, TEXT("Returned bad item type"), TEXT("Get Item Type"), MB_OK);}break;
In the IDM_SET_CAPABILITY menu handle add the following code:
case IDM_SET_CAPABILITY:{if (uItemValue != TWSX_NATIVE){TW_CAPABILITY twSetCap;twSetCap.Cap = ICAP_XFERMECH;twSetCap.ConType = TWON_ONEVALUE;L_TwainCreateNumericContainerOneValue(&twSetCap, TWAINNUMERICTYPE_TW_UINT16, TWSX_NATIVE);L_TwainSetCapability(hSession, &twSetCap, LTWAIN_CAPABILITY_SET);L_TwainFreeContainer(&twSetCap);}elseMessageBox(hWnd, TEXT("The Transfer mode is Native"), TEXT("Set Capability"), MB_OK);}break;
In the IDM_SET_BRIGHTNESS menu handle add the following code:
case IDM_SET_BRIGHTNESS:{TW_CAPABILITY twBrightnessCap;TW_FIX32 twFix32;pTW_ONEVALUE ptwOneVal;twBrightnessCap.Cap = ICAP_BRIGHTNESS;twBrightnessCap.ConType = TWON_ONEVALUE;nRet = L_TwainGetCapability (hSession, &twBrightnessCap, LTWAIN_CAPABILITY_GETCURRENT);if (nRet == SUCCESS){ptwOneVal = (pTW_ONEVALUE)GlobalLock(twCap.hContainer);memcpy(&twFix32, (void *)&ptwOneVal->Item, sizeof(TW_FIX32));GlobalUnlock(twBrightnessCap.hContainer);GlobalFree(twBrightnessCap.hContainer);twBrightnessCap.hContainer = NULL;if (twFix32.Whole != 128){twFix32.Whole = 128;twFix32.Frac = 0;}twBrightnessCap.Cap = ICAP_BRIGHTNESS;twBrightnessCap.ConType = TWON_ONEVALUE;twBrightnessCap.hContainer = GlobalAlloc(GHND, sizeof(TW_ONEVALUE) + sizeof(TW_FIX32));ptwOneVal = (pTW_ONEVALUE)GlobalLock(twCap.hContainer);ptwOneVal->ItemType = TWTY_FIX32;memcpy((void *)&ptwOneVal->Item, &twFix32, sizeof(TW_FIX32));GlobalUnlock(twBrightnessCap.hContainer);L_TwainSetCapability (hSession, &twBrightnessCap, LTWAIN_CAPABILITY_SET);}}break;
In the IDM_ACQUIRE menu handler add the following code:
case IDM_ACQUIRE:{L_TwainEndCapsNeg(hSession);if (LeadBitmap.Flags.Allocated){L_FreeBitmap(&LeadBitmap);L_InitBitmap(&LeadBitmap, sizeof(BITMAPHANDLE), 0, 0, 0);L_TwainAcquire(hSession, &LeadBitmap, sizeof(BITMAPHANDLE), NULL, LTWAIN_SHOW_USER_INTERFACE, NULL, NULL);}}break;
Compile and test the program.