原創|使用教程|編輯:郝浩|2013-07-23 13:53:11.000|閱讀 459 次
概述:ImagXpress 是世界上最先進的彩色映像和照片圖像處理工具包,有著.NET、COM、VC三種組件形式。本文主要來看看如何將ImagXpress作為一個COM對象導入 。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
本文主要來看看如何將ImagXpress作為一個COM對象導入,將對步驟進行詳細的講解以及給出示例源碼,主要步驟如下:
1、導入控件到Visual C + +中
#import指令行可以被添加到stdafx.h文件中來提供相應的功能給在項目中所有的源。
// 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.ImagXpress12.ActiveX.dll //{{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_)#import 指令簡單地指向 ImagXpress ®控件,當你#import這個 ImagXpress控件時,編譯器就會生成2個文件,分別是a .tli 和a .tlh文件,主要對控件的屬性和方法創建必要的COM wrapper,由于該指令是在你的stdafx.h文件中,對于你其他的模塊,這些wrapper也是隨時可用的,wrapper的代碼定義了COM的指針到你的.IDL-defined接口,用這個COM對象,你可以簡單的創建一個接口實例,并在需要的時候直接調用方法,關閉這個示例的指針。
2、聲明一個指向COM對象的指針
添加完#import語句后,你需要定義一個指針指向ImagXpress的COM對象和它的事件處理器。我們已經實現了事件處理接口,這個借口位于安裝在ImagXpress VC++\Include目錄的ImagXpress12Events.h中。在文件中介紹了它封裝的COM指針和事件處理程序接口的CImagXpress類,因此必須始終包含這個文件。在打印項目中,這個指針被稱為pImagXpress,封裝的類被稱為ppCImagXpress,它們在PrintDlg.h文件中實現的方式如下所示:
// 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 AccusoftImagXpress12; #include " ImagXpress12Events.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;
3、初始化COM
在調用COM功能之前需要初始化COM庫,此外,當你結束程序的時候,你也需要關閉COM庫和刪除ImagXpress COM對象,在Print.cpp文件中的說明如下所示:
// 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. CoUnintialize(); //Since the dialog is closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE;
4、創建 ImagXpress COM 對象
需要創建 ImagXpress COM 對象的實例。創建對象后,可以使用該對象的屬性和方法來集成ImagXpress的功能到你的應用程序中,在打印項目中,這個COM對象在PrintDlg.cpp文件中創建如下所示:
// 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 }
// 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.
5、刪除 ImagXpress COM 對象
當使用完這個ImagXpress對象后,你必須刪除它,當對話框收到 WM_DESTROY 的消息時,就可以在適當的位置刪除刪除 ImagXpress COM 對象。
// This code demonstrates how to delete the ImagXpress COM object void CPrintDlg::OnDestroy() CDialog::OnDestroy(); if (pImagXpress) pImagXpress = NULL; if (ppCImagXpress) delete ppCImagXpress;
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件