Capturing Multiple Images (Visual C++ 5 and later)

Take the following steps to add some code that will let you capture multiple images.

1.

Start with the project you created in Capturing an Icon from an EXE.

2.

Add two command buttons and set the ID and Caption properties as follows:

 

Name

Caption

 

IDC_CAPMULTI

Capture Multi

 

IDC_DISPLAY

Display Multi

 

3.

A sink interface must be added to receive CaptureImage events from the LEADRasterScr COM object. 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 CRasterScrSink 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 RasterScrSink.h file, move the destructor so that it is public:

// Implementation
   virtual ~CRasterScrSink();
protected:

4.

Add code to handle the CaptureImage Event.

 

a.

Add the following code to RasterScrSink.cpp immediately before the END_DISPATCH_MAP() macro. The VT_EMPTY argument indicates that the CaptureImage Event has no return value. The VTS_I4 argument indicates the type of the CaptureImage Event argument:

   DISP_FUNCTION_ID(CRasterScrSink, "CaptureImage",  1, OnCaptureImage, VT_EMPTY, VTS_I4)

 

b.

In RasterScrSink.cpp, modify the second parameter of the INTERFACE_PART macro to become as follows:

 

c.

INTERFACE_PART(CRasterScrSink, DIID__LTSCREvents, Dispatch)

 

d.

Click on the Class View tab in the project workspace.

 

e.

Right-click the "CRasterScrSink" branch, and choose Add Member Function.

 

f.

For Function Type enter void.

 

g.

For Function Declaration enter the following and click the OK button:

OnCaptureImage(long CaptureNumber)

 

h.

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

   #include "ScrCapDlg.h"
   #include "leadraster.h"

 

i.

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

class CScrCapDlg;

 

j.

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

CScrCapDlg *m_pDlg;

 

k.

Code the OnCaptureImage() method as follows:

void CRasterScrSink::OnCaptureImage (long CaptureNumber)
{
   m_pDlg->m_LEADRasterView1.GetRaster().SetBitmap(m_pDlg->m_pLEADRasterScr->GetBitmap ());
   ILEADRasterIO *pRasterIO=NULL;
   CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);
   pRasterIO-> Save(m_pDlg->m_LEADRasterView1.GetRaster(), "D:\\Temp\\Multi.tif",FILE_TIF, 0, (QFactorConstants)2, SAVE_APPEND);
   pRasterIO->Release();
}

5.

Add the following lines near the top of ScrCapDlg.cpp (just after #include "ScrCapDlg.h")

#include <afxctl.h>
#include "RasterScrSink.h"

6.

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 CScrCapDlg.

 

c.

In the Object IDs list box, select IDC_CAPMULTI.

 

d.

In the Messages list box, select BN_CLICKED.

 

e.

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

 

f.

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

void CScrCapDlg::OnCapmulti() 
{
   CRasterScrSink *pRasterScrSink;
   DWORD dwCookie;

   //Instantiate the sink class and hold a pointer to it.
   pRasterScrSink = new CRasterScrSink;
   pRasterScrSink->m_pDlg = this;
   //Establish a connection between source and sink.
   LPUNKNOWN pUnkSink = pRasterScrSink->GetIDispatch(FALSE);
   AfxConnectionAdvise(m_pLEADRasterScr, DIID__LTSCREvents, pUnkSink, FALSE, &dwCookie);

   //set the cancel key
   m_pLEADRasterScr->PutCaptureCancelKey (VK_ESCAPE);
   //set the delay, interval, and number of captures to perform
   m_pLEADRasterScr->PutCaptureDelay (2000);
   m_pLEADRasterScr->PutCaptureInterval(2000);
   m_pLEADRasterScr->PutCaptureCount (3);

   //capture
   m_pLEADRasterScr->CaptureActiveClient ();

   //Terminate connection between source and sink.
   pUnkSink = pRasterScrSink->GetIDispatch(FALSE);
   AfxConnectionUnadvise(m_pLEADRasterScr, DIID__LTSCREvents, pUnkSink, FALSE, dwCookie);
   delete pRasterScrSink;
}

7.

Back in MFC Class Wizard, do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select CScrCapDlg.

 

c.

In the Object IDs list box, select IDC_DISPLAY.

 

d.

In the Messages list box, select BN_CLICKED.

 

e.

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

 

f.

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

void CScrCapDlg::OnDisplay() 
{
   ILEADRasterIO *pRasterIO=NULL;
   CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);
   pRasterIO-> Load(m_LEADRasterView1.GetRaster(), "D:\\Temp\\Multi.tif", 0, 1, 1);
   AfxMessageBox(TEXT("Loaded page 1"));
   pRasterIO->Load(m_LEADRasterView1.GetRaster(), "D:\\Temp\\Multi.tif", 0, 2, 1);
   AfxMessageBox(TEXT("Loaded page 2"));
   pRasterIO->Load(m_LEADRasterView1.GetRaster(), "D:\\Temp\\Multi.tif", 0, 3, 1);
   AfxMessageBox(TEXT("Loaded page 3"));
   pRasterIO->Release();

   DeleteFile (TEXT("D:\\Temp\\Multi.tif"));
}

8.

On the main menu, select Build> Build ScrCap.exe to build the project.

9.

On the main menu, select Build> Execute ScrCap.exe to run the project. Click on Capture Multi, press F11, and then select the window(s) you wish to capture, at the proper times. You may want to decrease the capture interval in the code. To view the images you captured, click on the Display command button after capturing has ended.