AccuSoft Resource Center

ImageGear MFC Tutorial

ImageGear is the industry standard for imaging application development. Building your application with our award-winning toolkits provides you with a powerful set of imaging libraries and image manipulation features and functions. In this tutorial, you will create a Visual C++ MFC application using the ImageGear ActiveX Custom Control. This tutorial uses Microsoft® Visual Studio® 6.0, and assumes that you are using Microsoft Windows 98SE, Me, 2000, or XP. The application you create will load, display and zoom images. The instructions for this tutorial are specific to Microsoft Visual Studio 6.0 and ImageGear v14.

Preparing the Compiler

  1. Start Visual Studio 6.0. The Visual Studio Development Environment appears (see below).
     
  2. Create a new AppWizard MFC (exe) project.



    Make it a Dialog-based application. Turn off all features except 3D and ActiveX controls.
     
  3. Once the project is created, add the ImageGear ActiveX controls to the project.  Do this by going to the Project menu a selecting Add to Project, Components and Controls to display the Components and Controls Gallery dialog box.  Enter the Registered ActiveX Controls directory to see a list of available controls.  We are going to add each of the following controls to the project:
  • IGCoreCtl Class
  • IGDisplayCtl Class
  • IGDlgsCtl Class
  • IGFormatsCtl Class
  • IGPageViewCtl Class

Note:  If you do not see a required control in the list, you may need to install the ImageGear ActiveX product. If you selected to install this product on first use, simply run one of the ImageGear ActiveX samples.  To do this, click Start, Programs, AccuSoft ImageGear v14, Version, ActiveX, Samples (where Version is the edition of ImageGear [Professional, Enterprise or MD] being used) to locate the samples. Launching an ImageGear sample program will automatically cause the ActiveX product to be downloaded and installed on your machine.



Once you click Insert, the Visual Studio wizard will offer to build wrapper classes for all of the COM objects defined for each ActiveX control. It will display the Confirm Classes dialog (shown below).



Note:  We have found that there is a better way to obtain MFC wrapper classes for ActiveX controls (described below). We suggest that you uncheck all but the base-class in the Confirm Classes dialog before clicking OK (the wizard forces you to select at least the base-class).

  1. Following the process shown above, insert each of the five ImageGear ActiveX controls into your application.  After doing this, you will see the icons for each control appear in the Resource Editor Controls toolbar (below).

     

Setting up the Form

  1. Open the dialog created by the Application Wizard in the Resource Editor.  Right-click on the dialog box itself, select Properties and give it a Resizing border style.  Remove the OK and Cancel buttons as well as the To Do static text.  Instead, place three flat buttons at the top of the form. Give them control IDs IDC_OPEN, IDC_ZOOMIN and IDC_ZOOMOUT, with the captions Open, Zoom In and Zoom Out respectively:



     
  2. In turn, add each ImageGear 14 control to the form by clicking the appropriate icon in the toolbox and drawing the control on the form.  Stretch the PageView control (IGPageViewCtl) to fill most of the form (it should appear as a white rectangle). The remaining controls (IGCoreCtl, IGDisplayCtl, IGFormatsCtl, and IGDlgsCtl) should show up as small icons on the form. For this tutorial, we will use the default names (for example, IGCoreCtl1) for each control. Your form should now look something like this:


     

Add the Code

  1. As described above, we have found that the wizard-generated wrapper classes are not the best way to use ActiveX controls in an MFC application. However, because the wizard forces you to generate at least five such wrapper classes, you will first need to remove those files from your project. If you select File View mode in Visual Studio, you will see the various files created by the wizards. Remove the following files from the project:
  igcorectl.cpp igcorectl.h
igdisplayctl.cpp igdisplayctl.h
igdlgsctl.cpp igdlgsctl.h
igformatsctl.cpp igformatsctl.h
igpageviewctl.cpp igpageviewctl.h

With this done, open the header file created for your application’s dialog (if you named your project Tutorial, then the dialog header file will be TutorialDlg.h) and add five import statements near the top, as follows:

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#import "..\..\..\Bin\igcore14a.ocx"
#import "..\..\..\Bin\igformats14a.ocx"
#import "..\..\..\Bin\igdisplay14a.ocx"
#import "..\..\..\Bin\igdlgs14a.ocx"
#import "..\..\..\Bin\igview14a.ocx"


///////////////////////////////////////////////////////////////////////
// CTutorialDlg dialog

This will cause .tlh and .thi files to be generated when your application is compiled. We have found that the wrapper classes defined in .tlh files are easier to use.

  1. Add private member variables for the various ImageGear ActiveX controls and COM objects to your Dialog class:

protected:
HICON m_hIcon;

GearCORELib::IIGPagePtr m_pPage;
GearDISPLAYLib::IIGPageDisplayPtr m_pPageDisplay;
GearDISPLAYLib::IIGDisplayCtlPtr m_pDisplayCtl;
GearVIEWLib::IIGPageViewCtlPtr m_pPageViewCtl;
GearFORMATSLib::IIGFormatsCtlPtr m_pFormatsCtl;
GearCORELib::IIGCoreCtlPtr m_pCoreCtl;
GearDIALOGSLib::IIGDlgsCtlPtr m_pDlgsCtl;
GearDIALOGSLib::IIGFileDlgPtr m_pFileDialog;


// Generated message map functions
//{{AFX_MSG(CTutorialDlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()

  1. Add initializers for these member variables to the constructor for your Dialog class:

CTutorialDlg::CTutorialDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTutorialDlg::IDD, pParent)
, m_pPage(NULL)
, m_pPageDisplay(NULL)
, m_pDisplayCtl(NULL)
, m_pPageViewCtl(NULL)
, m_pFormatsCtl(NULL)
, m_pCoreCtl(NULL)
, m_pDlgsCtl(NULL)
, m_pFileDialog(NULL)
{
//{{AFX_DATA_INIT(CTutorialDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

  1. Add the following code to the Init_Dialog() method for your Dialog class:

BOOL CTutorialDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

CWnd *coreWnd, *formatsWnd, *displayWnd, *pageviewWnd, *dlgsWnd;
coreWnd = GetDlgItem(IDC_IGCORECTL1);
formatsWnd = GetDlgItem(IDC_IGFORMATSCTL1);
dlgsWnd = GetDlgItem(IDC_IGDLGSCTL1);
displayWnd = GetDlgItem(IDC_IGDISPLAYCTL1);
pageviewWnd = GetDlgItem(IDC_IGPAGEVIEWCTL1);

//Create the different controls
m_pCoreCtl = (GearCORELib::IIGCoreCtlPtr)
coreWnd->GetControlUnknown();
m_pFormatsCtl = (GearFORMATSLib::IIGFormatsCtlPtr)
formatsWnd->GetControlUnknown();
m_pDlgsCtl = (GearDIALOGSLib::IIGDlgsCtlPtr)
dlgsWnd->GetControlUnknown();
m_pDisplayCtl = (GearDISPLAYLib::IIGDisplayCtlPtr)
displayWnd->GetControlUnknown();
m_pPageViewCtl = (GearVIEWLib::IIGPageViewCtlPtr)
pageviewWnd->GetControlUnknown();

m_pCoreCtl->GetLicense()->SetSolutionName(L"AccuSoft 1-100-14");
m_pCoreCtl->GetResult()->NotificationFlags =
GearCORELib::IG_ERR_OLE_ERROR;
try
{
m_pCoreCtl->AssociateComponent(
m_pFormatsCtl->ComponentInterface );
m_pCoreCtl->AssociateComponent(
m_pDisplayCtl->ComponentInterface );

m_pDlgsCtl->GearFormats = m_pFormatsCtl->ComponentInterface;
m_pDlgsCtl->GearCore = m_pCoreCtl->ComponentInterface;
m_pDlgsCtl->GearDisplay = m_pDisplayCtl->ComponentInterface;
m_pFileDialog = m_pDlgsCtl->CreateFileDlg();

m_pPage = m_pCoreCtl->CreatePage();
m_pPageDisplay = m_pDisplayCtl->CreatePageDisplay(
m_pPage);

m_pPageViewCtl->PageDisplay = m_pPageDisplay;
}
catch( _com_error err )
{
AfxMessageBox(err.Description());

}

return TRUE; // return TRUE unless you set the focus to a control
}

The code above includes the following ImageGear methods and properties:

  IGCoreCtl License.SetSolutionName This method of the IGLicense object sets the solution name for the ImageGear license.
  Result.NotificationFlag This method of the IGResult object tells ImageGear to report errors using the VB On Error mechanism.
  AssociateComponent This method associates ImageGear components with the Core control.
  CreatePage This method creates and returns a new IGPage object.
  Result.isOk This property of the IGResult object tells if there are any errors on the error stack.
IGDlgsCtl GearFormats This property sets the current Formats control used by the Dialog control.
  GearCore This property sets the current Core control used by the Dialog control.
IGDisplayCtl CreatePageDisplay This method creates a new IGPageDisplay object for the specified IGPage.
IGPageViewCtl PageDisplay This property sets the current Page Display object displayed by the Page View control.
  UpdateView This method causes the Page View control to redraw itself.
  1. Use the Class Wizard to define a Command Handler for the WM_SIZE event for your Dialog class. Then, add the following code to the OnSize() method:

void CTutorialDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

if (m_pPageViewCtl != NULL)
{
HWND hWnd = (HWND) m_pPageViewCtl->hWnd;

::SetWindowPos(hWnd, HWND_TOP, 0, 20,
cx, cy-20, SWP_NOZORDER | SWP_NOACTIVATE);
}

}

  1. Next, create a click procedure for the Open button by double-clicking it in the Dialog Editor. Then, copy the following code into the OnOpen() method:

void CTutorialDlg::OnOpen()
{
try
{
GearDIALOGSLib::IIGFileDlgPageLoadOptionsPtr dlgLoadOptions;

dlgLoadOptions = (GearDIALOGSLib::IIGFileDlgPageLoadOptionsPtr)
m_pDlgsCtl->CreateFileDlgOptions(
GearDIALOGSLib::IG_FILEDLGOPTIONS_PAGELOADOBJ );

if(m_pFileDialog->Show(
(GearDIALOGSLib::IIGFileDlgOptionsPtr) dlgLoadOptions) )
{
m_pFormatsCtl->LoadPageFromFile(
m_pPage,
dlgLoadOptions->FileName,
dlgLoadOptions->PageNum);
}
m_pPageViewCtl->UpdateView();
}
catch( _com_error err )
{
AfxMessageBox(err.Description());
}

}

The code above includes the following methods:

  IGDlgsCtl CreateFileDlgOptions This method creates a file dialog options object that will contain for example the filename and page number.
  IGFileDlg Show This method shows the file dialog and returns True if the user clicked OK.
  IGFormatsCtl LoadPageFromFile This method loads an image into the specified IGPage from the specified filename.
  1. Finally, create click procedures for the Zoom In and Zoom Out buttons by double-clicking each of them in the Dialog Editor.  Then, copy the following code into the OnZoomin() and OnZoomout() methods:

void CTutorialDlg::OnZoomin()
{
try
{
GearDISPLAYLib::IIGDisplayZoomInfoPtr zoominfo;
zoominfo = (GearDISPLAYLib::IIGDisplayZoomInfoPtr)
m_pPageDisplay->GetZoomInfo( m_pPageViewCtl->hWnd );
zoominfo->HZoom = zoominfo->HZoom * 1.25;
zoominfo->VZoom = zoominfo->VZoom * 1.25;
zoominfo->Mode = (GearDISPLAYLib::enumZoomModes)
(GearDISPLAYLib::IG_DSPL_ZOOM_H_FIXED |
GearDISPLAYLib::IG_DSPL_ZOOM_V_FIXED);
m_pPageDisplay->UpdateZoomFrom(zoominfo);

m_pPageViewCtl->UpdateView();
}
catch( _com_error err )
{
AfxMessageBox(err.Description());
}

}

The above code zooms the image to 125 percent, or 5/4ths, of its size (zoom in by 25 percent).

void CTutorialDlg::OnZoomout()
{
try
{
GearDISPLAYLib::IIGDisplayZoomInfoPtr zoominfo;
zoominfo = (GearDISPLAYLib::IIGDisplayZoomInfoPtr)
m_pPageDisplay->GetZoomInfo( m_pPageViewCtl->hWnd );
zoominfo->HZoom = zoominfo->HZoom * 0.80;
zoominfo->VZoom = zoominfo->VZoom * 0.80;
zoominfo->Mode = (GearDISPLAYLib::enumZoomModes)
(GearDISPLAYLib::IG_DSPL_ZOOM_H_FIXED |
GearDISPLAYLib::IG_DSPL_ZOOM_V_FIXED);
m_pPageDisplay->UpdateZoomFrom(zoominfo);

m_pPageViewCtl->UpdateView();
}
catch( _com_error err )
{
AfxMessageBox(err.Description());
}

}

The above code zooms out, decreasing the image to 4/5ths of its size, thereby canceling the effect of the zoom-in above.

The code above includes:

  IGPageDisplay GetZoomInfo This method creates and returns a new IGDisplayZoomInfo object that contains zoom mode and values.
    UpdateZoomFrom This method sets zoom mode and values from the specified IGDisplayZoomInfo object.

Running the Application

That’s it! Simply compile and run your MFC project and you will have a simple, yet functional image viewer. Add the plug-ins to enable support for over 100 image formats.