Working with Zones(Visual C++ 5.0 and later)

Take the following steps to add code to the existing project that will let you recognize pages:

1.

Start with the program you created in Working with Pages.

2.

Add the following command buttons to the main window and set the ID and Caption properties as follows:

 

ID

Caption

 

IDC_FINDZONES

Find Zones

 

IDC_ZONESCOUNT

Zones Count

 

IDC_VERIFYZONE

Verify Zone

3.

Edit the OCRTutorDlg.h file and change the definition of COCRTutorDlg : CDialog by inserting the following line after DECLARE_MESSAGE_MAP:

   ILTZoneData* m_pZoneData; 
   CSampleZoneSink m_pZoneSink;
   IConnectionPoint *m_pZoneCP; 
   DWORD m_dwZoneCookie;

4.

Edit the OCRTutorDlg.h file and add the following to the OnShutdown procrdure:

   if (m_pZoneData)
   {
      if (m_pZoneSink)
      {
         LPUNKNOWN pUnkSink = m_pZoneSink->GetIDispatch(FALSE);
         if (m_pZoneData)
            AfxConnectionUnadvise(m_pZoneData, DIID__LTZoneDataEvents, pUnkSink, FALSE, m_dwZoneCookie);

         delete m_pZoneSink;
         m_pZoneSink = NULL;
      }

      m_pZoneData->Release();
      m_pZoneData = NULL;
   }

5.

Edit the OCRTutorDlg.cpp file and add the following to the OnInitDialog procedure as follows:

m_pZoneData = NULL; 

6.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Add Class button.

 

b.

Click New....

 

c.

Type CSampleZoneSink for the name of the class.

 

d.

Select CCmdTarget for the base class of the new class.

 

e.

Under Automation, click the Automation radio button.

 

f.

Click OK to create the class.

 

g.

In the SampleZoneSink.h file, move the destructor so that it is public:

// Implementation
   virtual ~ CSampleZoneSink();
protected:

 

h.

In the SampleZoneSink.h file, add the following to the top of the file:

class COCRTutorDlg;

 

i.

In the SampleZoneSink.h file, add the following to the CSampleZoneSink class in the //Attributes public section:


// Attributes
public:
   COCRTutorDlg *m_pDlg;

 

j.

In the SampleZoneSink.cpp file, add the following to the top of the file (after the #include "SampleZoneSink.h")

#include "OCRTutorDlg.h"

7.

Edit the header for the Sink class:

 

a.

In the Project Workspace, click the FileView tab.

 

b.

Double-click the OCRTutor Files folder to open it.

 

c.

Double-click the Header Files folder to open it.

 

d.

Double-click the SampleZoneSink.h file to edit it.

 

e.

Add the following just before //}}AFX_MSG:

afx_msg void OnVerification (short iZoneIndex, BSTR bstrWord);

 

f.

Double-click the Source Files folder to open it.

 

g.

Double-click the SampleZoneSink.cpp file to edit it.

 

h.

Inside the BEGIN_INTERFACE_MAP section, change the INTERFACE_PART to the following:

INTERFACE_PART(CSamplerZoneSink, DIID__LTZoneDataEvents, Dispatch)

8.

Edit the source for the Sink class:

 

a.

In the Project Workspace, click the FileView tab.

 

b.

Double-click the Source Files folder.

 

c.

Double-click the SampleZoneSink.cpp file to edit it.

 

d.

Add the following to the DISPATCH_MAP:

DISP_FUNCTION_ID(CSampleZoneSink,"Verification",1, OnVerification,VT_NONE,VTS_I4,VTS_BSTR)

 

e.

Add the following to the end of the file:

void CSampleZoneSink::OnVerification (short iZoneIndex, BSTR bstrWord)
{
   m_pDlg->m_pZoneData->VerifyCode VF_ACCEPT;
   m_pDlg->m_pZoneData->StopVerificationEvent FALSE:
}

9.

Add #include statements so you can access the new class:

 

a.

In the Project Workspace, click the FileView tab.

 

b.

Double-click the OCRTutor files folder to open it.

 

c.

Double-click the Header files folder to open it.

 

d.

Double-click the StdAfx.h file to edit it.

 

e.

Add the following lines to the end of the file:

#include <AFXCTL.H>
#include "SampleZoneSink.h"

 

10.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select COCRTutorDlg.

 

c.

In the Object IDs list box, select IDC_FINDZONES.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnFindzones).

 

f.

Click the Edit Code button and code the OnFindzones procedure as follows:

   CString csBuffer;
   m_pRasterDoc->GetAutoZoneOptions ();
   m_pRasterDoc->EnableZoneForceSingleColumn = TRUE; 
   m_pRasterDoc->ShowZoneGridLines = TRUE; 
   m_pRasterDoc->SetAutoZoneOptions ();
   int nRet = m_pRasterDoc->FindZones (0, TRUE); 
   if (nRet == 0) 
      AfxMessageBox(TEXT("Automatic zones method found all available zones in the specified pages successfully."));
   else
   {
      csBuffer.Format (TEXT("Error %d in finding available zone in the specified page"), nRet); 
      AfxMessageBox (csBuffer); 
   }

11.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select COCRTutorDlg.

 

c.

In the Object IDs list box, select IDC_ZONESCOUNT.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnZonescount).

 

f.

Click the Edit Code button and code the OnZonescount procedure as follows:

   CString csBuffer; 
   int nZoneCount; 
   nZoneCount = m_pRasterDoc->ZoneCount [0]
   csBuffer.Format (TEXT("Total Zones count = %d\n"), nZoneCount); 
   AfxMessageBox(csBuffer);

 

12.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select COCRTutorDlg.

 

c.

In the Object IDs list box, select IDC_VERIFYZONE.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnVerifyzone).

 

f.

Click the Edit Code button and code the OnVerifyzone procedure as follows:

   CString csBuffer; 
   int nZoneCount; 
   int nRet; 

   HRESULT hr = CoCreateInstance(CLSID_LTZoneData, NULL, CLSCTX_ALL, IID_ILTZoneData, (void**)&m_pZoneData); 
   if (FAILED(hr)) 
   {
      AfxMessageBox(TEXT("Can't instantiate the Zone Data Object...")); 
      return; 
   }

   m_pZoneSink = new CSampleZoneSink; 
   m_pZoneSink->m_pView = this; 

   LPUNKNOWN pUnkSink = m_pZoneSink->GetIDispatch(FALSE); 
   AfxConnectionAdvise(m_pZoneData, DIID__LTZoneDataEvents, pUnkSink, FALSE, &m_dwZoneCookie); 

   nZoneCount = m_pRasterDoc->ZoneCount [0]; 
   for(int i=0; i<nZoneCount; i++)
   {
      nRet = m_pRasterDoc->GetZoneInfo (0, i, m_pZoneData); 
      if (nRet  ==  0) 
      {

         m_pZoneData->EnableVerificationEvent = TRUE; 
         nRet = m_pRasterDoc->UpdateZone (0, i, m_pZoneData); 
         if (nRet != 0) 
         {
            csBuffer.Format (TEXT("Error %d in updating zone # %d"), nRet, i); 
            AfxMessageBox (csBuffer); 
         }
      }
   }

13.

On the Build menu, select Build OCRTutor.exe.

14.

On the Build menu, select Execute OCRTutor.exe.

15.

Save this project to use for testing other code samples.