LoadInfo... example for C++ 5.0 and later
Take the following steps to exercise the LoadInfo event and LoadInfo... properties. The example creates a raw FAX file, then loads the file it just created. (Getting format information from an unfamiliar file is beyond the scope of this example.)
1. |
Start with the project that you created in Loading and Displaying an Image. |
2. |
Edit TUTORDLG.H and insert the following lines in the definition of CTutorDlg after DECLARE_MESSAGE_MAP(): |
public:
// Declare variables for demonstrating raw FAX.
int MyInfoBits;
long MyInfoFlags;
int MyInfoFormat;
float MyInfoHeight;
long MyInfoOffset;
float MyInfoWidth;
int MyInfoXRes;
int MyInfoYRes;
3. |
Add a CommandButton control and code its click procedure as follows: |
void CTutorDlg::OnButton1()
{
ILEADRasterProcess *pRasterProc=NULL;
CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL,
IID_ILEADRasterProcess, (void**)&pRasterProc);
m_LEADRasterView1.SetScaleMode (3); // Pixels
// Make the image look OK as a 1-bit image.
pRasterProc->Size(m_LEADRasterView1.GetRaster(),
m_LEADRasterView1.GetScaleWidth(),
m_LEADRasterView1.GetScaleHeight(),
RESIZE_RESAMPLE);
pRasterProc->Halftone(m_LEADRasterView1.GetRaster(),
HALFTONE_VIEW, 45);
// Set the form-level variables to the values we will use when loading.
MyInfoBits = 1;
MyInfoFlags = LOADINFO_TOPLEFT;
MyInfoFormat = FILE_FAX_G3_2D;
MyInfoHeight = m_LEADRasterView1.GetRaster().GetBitmapHeight();
MyInfoOffset = 0;
MyInfoWidth = m_LEADRasterView1.GetRaster().GetBitmapWidth();
MyInfoXRes = m_LEADRasterView1.GetRaster().GetBitmapXRes();
MyInfoYRes = m_LEADRasterView1.GetRaster().GetBitmapYRes();
// Save the image as raw FAX; then load the saved file.
// Format information will be supplied in the LoadInfo event.
m_pRasterIO->Save(m_LEADRasterView1.GetRaster(),
"d:\\temp\\tmp.tif", FILE_FAX_G3_2D,
1, (QFactorConstants)1, SAVE_OVERWRITE);
m_pRasterIO->Load(m_LEADRasterView1.GetRaster(),
"d:\\temp\\tmp.tif", 0, 0, 1);
// Repaint the newly loaded image.
m_LEADRasterView1.ForceRepaint();
pRasterProc->Release();
}
4. |
Edit the TUTORDLG.H file and change the definition of CTutorDlg : CDialog by inserting the following lines after DECLARE_MESSAGE_MAP: |
public:
ILEADRasterIO *m_pRasterIO;
CRasterIOSink *m_pRasterIOSink;
IConnectionPoint *m_pIOCP;
DWORD m_dwIOCookie;
5. |
Edit the TUTORDLG.CPP file and add the following code to the end of the OnInitDialog function: |
//Instantiate the sink class and hold a pointer to it.
m_pRasterIOSink = new CRasterIOSink;
m_pRasterIOSink->m_pDlg = this;
//Create the RasterIO object
CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL,
IID_ILEADRasterIO, (void**)&m_pRasterIO);
//Establish a connection between source and sink.
LPUNKNOWN pIOUnkSink = m_pRasterIOSink->GetIDispatch(FALSE);
AfxConnectionAdvise(m_pRasterIO, DIID__LEADRasterIOEvents, pIOUnkSink, FALSE, &m_dwIOCookie);
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 CRasterIOSink 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 RasterIOSink.h file, move the destructor so that it is public: |
// Implementation
virtual ~CRasterIOSink();
protected:
In the RasterIOSink.h
file, add the following to the top of the file:
class CTutorDlg;
In the RasterIOSink.h file, add the following to the CRasterIOSink class in the //Attributes public section:
// Attributes
public:
CTutorDlg *m_pDlg;
In the RasterIOSink.cpp file, add the following to the top of the file (after the #include "RasterIOSink.h")
#include "tutorDlg.h"
7. |
Add #include statements so you can access the new class: |
|
|
a. |
In the Project Workspace, click the FileView tab. |
|
b. |
Double-click the tutor 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 "RasterIOSink.h"
8. |
Edit the header for the Sink class: |
|
|
a. |
In the Project Workspace, click the FileView tab. |
|
b. |
Double-click the tutor files folder to open it. |
|
c. |
Double-click the Header Files folder to open it. |
|
d. |
Double-click the RasterIOSink.h file to edit it. |
|
e. |
Add the following just before //}}AFX_MSG: |
afx_msg void OnLoadInfo(void);
9. |
Edit the source for the Sink class: |
|
|
a. |
In the Project Workspace, click the FileView tab. |
|
b. |
Double-click the tutor files folder to open it. |
|
c. |
Double-click the Source Files folder. |
|
d. |
Double-click the RasterIOSink.cpp file to edit it. |
|
e. |
Add the following to the DISPATCH_MAP: |
DISP_FUNCTION_ID(CRasterIOSink,"LoadInfo",2,OnLoadInfo,VT_EMPTY,VTS_NONE)
|
f. |
Inside the BEGIN_INTERFACE_MAP section, change the INTERFACE_PART tothe following: |
INTERFACE_PART(CRasterIOSink, DIID__LEADRasterIOEvents, Dispatch)
|
g. |
Add the following to the end of the file: |
void CRasterIOSink::OnLoadInfo()
{
// Use the form-level variables to supply format information.
m_pDlg->m_pRasterIO->PutLoadInfoBits(m_pDlg->MyInfoBits);
m_pDlg->m_pRasterIO->PutLoadInfoFlags((LoadInfoFlagConstants)m_pDlg->MyInfoFlags);
m_pDlg->m_pRasterIO->PutLoadInfoFormat((RasterFileConstants)m_pDlg->MyInfoFormat);
m_pDlg->m_pRasterIO->PutLoadInfoHeight(m_pDlg->MyInfoHeight);
m_pDlg->m_pRasterIO->PutLoadInfoOffset(m_pDlg->MyInfoOffset);
m_pDlg->m_pRasterIO->PutLoadInfoWidth(m_pDlg->MyInfoWidth);
m_pDlg->m_pRasterIO->PutLoadInfoXRes(m_pDlg->MyInfoXRes);
m_pDlg->m_pRasterIO->PutLoadInfoYRes(m_pDlg->MyInfoYRes);
}
10. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: |
|
|
a. |
In the Class name combo box, select CTutorDlg. |
|
b. |
In the Object IDs list box, select CTutorDlg. |
|
c. |
In the Messages list box, select WM_DESTROY. |
|
d. |
Click the Add function button. Choose OK for the default function name (OnDestroy). |
|
e. |
Click the Edit Code button to start entering the code. |
11. |
Enter new code as follows: |
void CTutorDlg::OnDestroy()
{
//Terminate a connection between source and sink.
LPUNKNOWN pIOUnkSink = m_pRasterIOSink->GetIDispatch(FALSE);
AfxConnectionUnadvise(m_pRasterIO, DIID__LEADRasterIOEvents,
pIOUnkSink, FALSE, m_dwIOCookie);
delete m_pRasterIOSink;
m_pRasterIO->Release();
CDialog::OnDestroy();
}
12. |
Run your program to test it. |