ImagXpress 13 for ActiveX - User Guide > Getting Started > Import ImagXpress as a COM Object |
The #import directive line can be added to the stdafx.h file to provide the appropriate functionality to all sources in the project.
C++ Example |
Copy Code
|
---|---|
// This code demonstrates the #import directive used in the stdafx.h file #define AFX_STDAFX_H__B0A9FE69_3B8B_11D3_9CFE_00400543FF49__INCLUDED_ #if _MSC_VER= 1000 #pragma once #endif // _MSC_VER >= 1000 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC extensions #include <afxdisp.h> // MFC OLE automation classes #ifndef _AFX_NO_AFXCMN_SUPPORT #include <afxcmn.h> // MFC support for Windows Common Controls #endif // _AFX_NO_AFXCMN_SUPPORT #import <Accusoft.ImagXpress13.ActiveX.dll> no_function_mapping //{{AFX_INSERT_LOCATION}} //Microsoft Developer Studio will insert additional declarations immediately before the previous line. #endif // !defined(AFX_STDAFX_H__B0A9FE69_3B8B_11D3_9CFE_00400543FF49__INCLUDED_) |
The #import directive simply points to the ImagXpress® control. When you #import the ImagXpress control, the compiler produces 2 files – a .tli and a .tlh file – that together create the necessary COM wrapper for the control’s properties and methods. Since the directive is in your stdafx.h file, the wrappers are immediately available to your other modules. The wrapper code defines COM smart pointers to your .IDL-defined interfaces. To use this COM object, you simply create an instance of the interface you want and then call methods directly off this instance pointer.
Because the ImagXpress COM library has over 1,000 methods, the Visual C++ compiler will inject the 'implementation_key' compiler directive into the generated .tlh and .tli files. This is done to improve performance, but can cause the side effect of introducing compiler and/or linker errors that normally wouldn’t occur. This can be avoided by using the 'no_function_mapping' compiler directive for the #import statement. Please contact Microsoft for more details. There is a known issue with the Platform Toolset (Project Properties -> Configuration Properties -> General -> Platform Toolset) of Visual Studio 2012 regarding how type library files are imported which can cause compiler errors that normally wouldn’t occur. This issue can occur in Visual Studio 2013 if the Platform Toolset is set to Visual Studio 2012 (v110). This can be worked around by selecting the Visual Studio 2010 (v100) toolset instead. Or it can be worked around by specifying the ImagXpress library to be imported using a filename instead of using a ProgID, Localization ID, and Version Number (see http://msdn.microsoft.com/en-us/library/8etzzkb6.aspx). If using the Platform Toolset of Visual Studio 2012, it’s recommend the library be imported as follows: #import <Accusoft.ImagXpress13.ActiveX.dll> no_function_mapping |
After you add the #import statement, you need to define a pointer to the ImagXpress COM object and its event handlers. We have implemented the event handling interface for you. This interface is located in ImagXpress13Events.h which installs in the ImagXpress VC++\Include directory. This file introduces the CImagXpress class which encapsulates both the COM pointer and the event handler interface, therefore, you must always include this file. In the Print project, the pointer is called pImagXpress and the encapsulated class is called ppCImagXpress. They are implemented in the PrintDlg.h file as follows:
C++ Example |
Copy Code
|
---|---|
// This code demonstrates how the pointer is declared and implemented in the PrintDlg.h file #if !defined(AFX_PRINTDLG_H__B0A9FE67_3B8B_11D3_9CFE_00400543FF49__INCLUDED_) #define AFX_PRINTDLG_H__B0A9FE67_3B8B_11D3_9CFE_00400543FF49__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 Using namespace AccusoftImagXpress13; #include "ImagXpress13Events.h" ///////////////////////////////////////////////////////////////////////////// // CPrintDlg dialog class CPrintDlg : public CDialog public: // Construction CPrintDlg(CWnd* pParent = NULL); // standard constructor // these are the pointers to our COM instance CImagXpress *ppCImagXpress; IImagXpressPtr pImagXpress; |
You need to initialize the COM library before you can call COM functions. Also, you need to close the COM library and delete the ImagXpress COM objects when your program ends. This is illustrated in the Print.cpp file as follows:
C++ Example |
Copy Code
|
---|---|
// This code demonstrates how the COM library must be initialized BOOL CPrintApp::InitInstance() HRESULT hRes = CoInitialize(NULL); AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif CPrintDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) // TODO: Place code here to handle when the dialog is // dismissed with OK else if (nResponse == IDCANCEL) // TODO: Place code here to handle when the dialog is // dismissed with Cancel // Closes the COM library on the current apartment, // unloads all DLLs loaded by apartment, // frees any other resources that the apartment maintains and // forces all RPC connections on the apartment to close. CoUninitialize(); //Since the dialog is closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; |
You need to create an instance of the ImagXpress COM object. After the object is created, you can use the object’s properties and methods to integrate ImagXpress features in your application. In the Print project, the COM object is created in the PrintDlg.cpp file as follows:
C++ Example |
Copy Code
|
---|---|
// This code demonstrates how the ImagXpress COM object is created BOOL CPrintDlg::OnInitDialog() CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here // Create an ImagXpress Object ppCImagXpress = new CImagXpress(this, 1, m_hWnd, 50, 20, 50, 50); pImagXpress = ppCImagXpress->pImagXpress; // Control Initialization if (pImagXpress) pImagXpress->BorderType = BORD_Raised; pImagXpress->FileName = "bird.jpg"; // From now on, the library is referenced by the new // ImagXpress COM instance. return TRUE; // return TRUE unless you set the focus to a control } |
C++ Example |
Copy Code
|
---|---|
// When creating a new ImagXpress object, you must pass the following parameters LPVOID classPtr // The Dialog’s this pointer. DWORD objID // A unique object ID that you assign to the object’s instance. This ID can be used in event handling to determine which instance of an object has triggered the event. long hWndParent // The parent Window of the ImagXpress object. Typically, this is the Dialog’s hWnd. long left // The x coordinate of the ImagXpress object in pixels. long top // The top coordinate of the ImagXpress object in pixels. long width // The width of the ImagXpress object in pixels. long height // The height of the ImagXpress object in pixels. |
You must delete the ImagXpress object when you are finished with it. A convenient place to do this is when the dialog receives a WM_DESTROY message.
C++ Example |
Copy Code
|
---|---|
// This code demonstrates how to delete the ImagXpress COM object void CPrintDlg::OnDestroy() CDialog::OnDestroy(); if (pImagXpress) pImagXpress = NULL; if (ppCImagXpress) delete ppCImagXpress; |