Implementing an Automated Annotation Program:
Take the following steps to create and run a program that implements automated annotations. Remember, the purpose of the annotation tutorials is to provide you a quick and easy way to generate an annotation program. For more in depth annotation programming, refer to the Annotate demo.
1. |
Create a new directory in the LEADTOOLS 16\Examples\CDLL directory called AnnAuto. |
2. |
Copy everything in the LoadSave directory into the AnnAuto directory. |
3. |
Compile the project as is and run LoadSave.exe to familiarize yourself with the basic program. |
4. |
Open the imports.cpp file and make the additions shown in green : |
#if defined(WIN64)
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\x64\\Ltkrn_x.lib")
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\x64\\Ltdis_x.lib")
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\x64\\Ltfil_x.lib")
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\x64\\Ltdlgkrn_x.lib")
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\x64\\Ltdlgfile_x.lib")
#pragma
comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\x64\\Ltann_x.lib")
#else
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Ltkrn_u.lib")
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Ltdis_u.lib")
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Ltfil_u.lib")
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Ltdlgkrn_u.lib")
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Ltdlgfile_u.lib")
#pragma
comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Ltann_u.lib")
#endif // #if defined(WIN64)
5. |
Define the following global variables in Loadsave.cpp : |
HANNOBJECT hContainer = 0; // Container annotation object
HANNOBJECT hAutomation = 0; // Automation object
HWND hWndToolbar = 0; // Window for the annotation toolbar
6. |
In Window_OnCreate in LoadSave.cpp, add the following code before “return (TRUE);” : |
// Create the annotation toolbar and position its top left corner at 0,0
POINT ToolbarPt = {0,0};
L_AnnCreateToolBar(hWnd, &ToolbarPt, ANNTOOLALIGN_LEFT | ANNTOOLALIGN_TOP, TRUE, &hWndToolbar, 0, NULL);
7. |
After the Window_OnCreate function, add the following function: |
L_VOID SetupAnnotations(HWND hWnd)
{
// Delete any existing annotation container, automation
objects
if(hAutomation)
{
L_AnnSetActiveState(hAutomation,
ANNACTIVE_DISABLED);
L_AnnDestroy(hAutomation,
ANNFLAG_NOINVALIDATE);
hAutomation = 0;
}
if(hContainer)
{
L_AnnDestroy(hContainer,
ANNFLAG_NOINVALIDATE);
hContainer = 0;
}
// Create the automation object
L_AnnCreate(ANNOBJECT_AUTOMATION,
&hAutomation);
// Set the dots per inch for interpreting physical measurements
L_AnnSetDpiX(hAutomation,
600, 0);
L_AnnSetDpiY(hAutomation,
600, 0);
// Set the number of possible undo actions
L_AnnSetUndoDepth(hAutomation,
3);
// Create the annotation container, which we will use
as the root container
ANNRECT ContainerRect;
ContainerRect.left = 0;
ContainerRect.top = 0;
ContainerRect.right = BITMAPWIDTH(&Data.BitmapHandle)
-1;
ContainerRect.bottom = BITMAPHEIGHT(&Data.BitmapHandle)
- 1;
L_AnnCreateContainer(hWnd,
&ContainerRect, TRUE, &hContainer);
// Set the scalars and offsets
L_AnnSetScalarX(hContainer,
1, 0);
L_AnnSetScalarY(hContainer,
1, 0);
L_AnnSetOffsetX(hContainer,
(L_DOUBLE) 0, 0);
L_AnnSetOffsetY(hContainer,
(L_DOUBLE) 0, 0);
// Assign the automation object to the container
L_AnnSetAutoContainer(hAutomation, hContainer);
// Enable the automation object
L_AnnSetActiveState(hAutomation,
ANNACTIVE_ENABLED);
// Set design mode, which allows creation of annotations
L_AnnSetUserMode(hContainer,
ANNUSER_DESIGN);
}
8. |
In the Window_OnCommand, add a call to SetupAnnotations() (shown in green ) |
case IDM_OPEN:
nRet = Get_OpenFile (hWnd);
if (nRet == 2)
; /* No file was
selected for opening, so do nothing. */
else if (nRet == SUCCESS)
{
SetupAnnotations(hWnd);
FORWARD_WM_QUERYNEWPALETTE
(hWnd, SendMessage);
}
else
{
wsprintf (buf, TEXT("Error
%d Loading %s"), nRet,
(LPTSTR)
Data.szFilename);
MessageBox (hWnd,
buf, TEXT("Error"), MB_OK);
}
break;
9. |
In the Window_OnPaint function, call L_AnnDraw after the call to L_PaintDC |
L_AnnDraw(hdc, &ps.rcPaint, hContainer);
10. |
In the Window_OnDestroy function, add the following immediately before “/* Post WM_QUIT to end the application */” : |
// Cleanup for annotations
L_AnnDestroy(hContainer, ANNFLAG_NOINVALIDATE);
L_AnnDestroy(hAutomation, ANNFLAG_NOINVALIDATE);
11. |
Add the following case to the switch statement in the MainWndProc procedure: |
LRESULT CALLBACK MainWndProc (HWND hWnd, L_UINT Message,
WPARAM wParam, LPARAM lParam)
{
switch (Message)
{
HANDLE_MSG (hWnd, WM_CREATE, Window_OnCreate);
HANDLE_MSG (hWnd, WM_COMMAND, Window_OnCommand);
HANDLE_MSG (hWnd, WM_PALETTECHANGED,
Window_OnPaletteChanged);
HANDLE_MSG (hWnd, WM_QUERYNEWPALETTE,
Window_OnQueryNewPalette);
HANDLE_MSG (hWnd, WM_PAINT, Window_OnPaint);
HANDLE_MSG (hWnd, WM_DESTROY, Window_OnDestroy);
HANDLE_MSG (hWnd, WM_ACTIVATE, Window_OnActivate);
HANDLE_MSG (hWnd, WM_PALETTEISCHANGING,
Window_OnPaletteChanging);
HANDLE_MSG (hWnd, WM_SYSCOLORCHANGE,
Window_SysColorChange);
HANDLE_MSG
(hWnd, WM_LTANNEVENT, Window_AnnEvent);
}
return DefWindowProc (hWnd, Message,
wParam, lParam);
}
12. |
Add the code below for Window_AnnEvent immediately before the existing code for Window_OnPaint . |
LRESULT Window_AnnEvent(HWND hWnd, WPARAM wParam, LPARAM
lParam)
{
UNREFERENCED_PARAMETER(hWnd);
switch(wParam)
{
case LTANNEVENT_TOOLCHECKED:
L_AnnSetTool(hAutomation,
(L_UINT)lParam);
break;
case LTANNEVENT_AUTOENDSET:
// Set tool back to ANNTOOL_SELECT
after finished creating annotation
L_AnnSetToolBarChecked(hWndToolbar,
ANNTOOL_SELECT);
break;
}
return 0;
}
13. |
Add a forward declaration at the very end of Loadsave.h |
LRESULT Window_AnnEvent(HWND hWnd, WPARAM wParam, LPARAM lParam);
14. |
On the Build menu, select Build LoadSave.exe. |
15. |
On the Debug menu, select Start Without Debugging. |
16. |
Save this project to use for testing other annotation code samples and for implementing the Using Automated Annotations in Run Mode tutorial. |