The general usage of the interface in AO)

Source: Internet
Author: User

The general usage of interfaces in AO (conversion)
We use instances to learn how to use interfaces in AO. The instance we want to learn is a COM created with ATL in VC 6.0.
The main steps for creating COM are as follows:
1. Implement the esriSystemUI ICommand interface so that you can add this build model to ArcScene or other toolbar and perform related operations when you click this button.
2. Create a connection point to implement the ISceneGraphEvents interface event and rewrite the functions related to the fire event. I added some OpenGL functions in Fire_BeforeDraw and Fire_AfterDraw to implement atomization.
3. Let's look back at the implementation of the ICommand interface. ISceneHookHelper is used to hook IScene in the OnCreate function of ICommand. In OnClick, we use ISceneHookHelper to further obtain ISceneViewer for Refresh scenarios. Fire_BeforeDraw and Fire_AfterDraw are called before and after the refresh scenario to draw the atomization effect. In this way, sinks in ArcScene can respond to the fire event function.
The main process is as follows: user --> ArcScene --> ICommand interface --> Custom interface --> connection point --> fire event --> OpenGL --> ArcScene.
Project name: Walkthrough1Cpp; custom Interface Class: CZoomIn; proxy interface event class CProxyISceneGraphEvents
Note: When using ATL to create an Interface, You must select Custom instead of Dual in the attribute panel Interface. The former inherits IUnkown, and the latter inherits IDispatch. You must also select the supported connection points.
// Stdafx. h: include file for standard system include files,
// Or project specific include files that are used frequently,
// But are changed infrequently
# If! Defined (afx_stdafx_h0000ff23b542_c904_4f39_8a97_c4de3c45a0e60000included _)
# Define afx_stdafx_h0000ff23b542_c904_4f39_8a97_c4de3c45a0e600000000ded _
# If _ MSC_VER> 1000
# Pragma once
# Endif // _ MSC_VER> 1000
# Define STRICT
# Ifndef _ WIN32_WINNT
# Define _ WIN32_WINNT 0x0400
# Endif
# Define _ ATL_APARTMENT_THREADED
# Include <atlbase. h>
// You may derive a class from CComModule and use it if you want to override
// Something, but do not change the name of _ Module
Extern CComModule _ Module;
# Include <atlcom. h>
# Pragma warning (push)
# Pragma warning (disable: 4146)
# Pragma warning (disable: 4192)
# Import \ "e :\\ Program Files \ ArcGIS \ com \ esriSystem. olb \" raw_interfaces_only raw_native_types no_namespace
Named_guids exclude (\ "OLE_COLOR \", \ "OLE_HANDLE \", \ "VARTYPE \") [Page]
# Import \ "e :\\ Program Files \ ArcGIS \ com \ esriSystemUI. olb \" raw_interfaces_only raw_native_tyno_namespace
Named_guids
# Import \ "e :\\ Program Files \ ArcGIS \ com \ esriGeometry. olb \" raw_interfaces_only raw_native_types no_namespace
Named_guids
# Import \ "e :\\ Program Files \ ArcGIS \ com \ esriDisplay. olb \" raw_interfaces_only raw_native_types no_namespace
Named_guids
# Import \ "e :\\ Program Files \ ArcGIS \ com \ esriGeoDatabase. olb \" raw_interfaces_only raw_native_types
No_namespace named_guids
# Import \ "e :\\ Program Files \ ArcGIS \ com \ esriCarto. olb \" raw_interfaces_only raw_native_types no_namespace
Named_guids
# Import \ "e :\\ Program Files \ ArcGIS \ com \ esriControlCommands. olb \" raw_interfaces_only raw_native_types
No_namespace named_guids
# Import \ "e :\\ Program Files \ ArcGIS \ com \ esriFramework. olb \" raw_interfaces_only raw_native_types no_namespace
Named_guids
# Import \ "e :\\ Program Files \ ArcGIS \ com \ esri3DAnalyst. olb \" raw_interfaces_only raw_native_tyno_namespace
Named_guids
# Pragma warning (pop)
# Include \ "gl \ gl. h \"
# Include \ "gl \ glu. h \"
# Include \ "gl \ glaux. h \"
# Include <GL/glut. h>
// {AFX_INSERT_LOCATION }}
// Microsoft Visual C ++ will insert additional declarations immediately before the previous line.
# Endif //! Defined (afx_stdafx_h0000ff23b542_c904_4f39_8a97_c4de3c45a0e600000000ded)

// ZoomIn. h: Declaration of the CZoomIn
# Ifndef _ ZOOMIN_H _
# Define _ ZOOMIN_H _
# Include \ "resource. h \" // main symbols
# Include \ "esri3DAnalystCP. h \"
// # Import \ "D :\\ Program Files \ ArcGIS \ com \ esriSystemUI. olb \" raw_interfaces_only, raw_native_types,
No_namespace, named_guids
//////////////////////////////////////// /////////////////////////////////////
// CZoomIn
Class ATL_NO_VTABLE CZoomIn:

Public CComObjectRootEx <CComSingleThreadModel>,
Public CComCoClass <CZoomIn, & CLSID_ZoomIn>,
Public IZoomIn,
Public ICommand,
Public CProxyISceneGraphEvents <CZoomIn>,
Public IConnectionPointContainerImpl <CZoomIn> [Page]
{
Public:
CZoomIn ()
{
M_hBitmap =: LoadBitmap (_ Module. GetResourceInstance (), MAKEINTRESOURCE (IDB_ZOOMIN ));
}
~ CZoomIn ()
{
DeleteObject (m_hBitmap );
}
IHookHelperPtr m_ipHookHelper;
ISceneHookHelperPtr m_ipSceneHookHelper;
DECLARE_REGISTRY_RESOURCEID (IDR_ZOOMIN)
DECLARE_PROTECT_FINAL_CONSTRUCT ()
BEGIN_COM_MAP (CZoomIn)
COM_INTERFACE_ENTRY (IZoomIn)
COM_INTERFACE_ENTRY (ICommand)
COM_INTERFACE_ENTRY_IMPL (IConnectionPointContainer)
END_COM_MAP ()
// IZoomIn private:
HBITMAP m_hBitmap;
Public:
// ICommand
STDMETHOD (get_Enabled) (VARIANT_BOOL * Enabled );
STDMETHOD (get_Checked) (VARIANT_BOOL * Checked );
STDMETHOD (get_Name) (BSTR * Name );
STDMETHOD (get_Caption) (BSTR * Caption );
STDMETHOD (get_Tooltip) (BSTR * Tooltip );
STDMETHOD (get_Message) (BSTR * Message );
STDMETHOD (get_HelpFile) (BSTR * HelpFile );
STDMETHOD (get_HelpContextID) (LONG * helpID );
STDMETHOD (get_Bitmap) (OLE_HANDLE * Bitmap );
STDMETHOD (get_Category) (BSTR * categoryName );
STDMETHOD (OnCreate) (IDispatch * hook );
STDMETHOD (OnClick )();
Public:
BEGIN_CONNECTION_POINT_MAP (CZoomIn)
CONNECTION_POINT_ENTRY (IID_ISceneGraphEvents)
END_CONNECTION_POINT_MAP ()
};
# Endif/_ ZOOMIN_H _

// ZoomIn. cpp: Implementation of CZoomIn
# Include \ "stdafx. h \"
# Include \ "through1cpp. h \"
# Include \ "ZoomIn. h \"
//////////////////////////////////////// /////////////////////////////////////
// CZoomIn
STDMETHODIMP CZoomIn: get_Enabled (VARIANT_BOOL * Enabled)
{
If (Enabled = NULL)
Return E_POINTER;

* Enabled = VARIANT_TRUE; // Enable the tool always
Return S_ OK;
}
STDMETHODIMP CZoomIn: get_Checked (VARIANT_BOOL * Checked)
{
If (Checked = NULL)
Return E_POINTER;

Return E_NOTIMPL;
}
STDMETHODIMP CZoomIn: get_Name (BSTR * Name)
{
If (Name = NULL) [Page]
Return E_POINTER;
* Name =: SysAllocString (L \ "ZoomIn x 0.5 Cpp \");
Return S_ OK;
}
STDMETHODIMP CZoomIn: get_Caption (BSTR * Caption)
{
If (Caption = NULL)
Return E_POINTER;
* Caption =: SysAllocString (L \ "ZoomIn x 0.5 Cpp \");
Return S_ OK;
}
STDMETHODIMP CZoomIn: get_Tooltip (BSTR * Tooltip)
{
If (Tooltip = NULL)
Return E_POINTER;

Return E_NOTIMPL;
}
STDMETHODIMP CZoomIn: get_Message (BSTR * Message)
{
If (Message = NULL)
Return E_POINTER;

Return E_NOTIMPL;
}
STDMETHODIMP CZoomIn: get_HelpFile (BSTR * HelpFile)
{
If (HelpFile = NULL)
Return E_POINTER;

Return E_NOTIMPL;
}
STDMETHODIMP CZoomIn: get_HelpContextID (LONG * helpID)
{
If (helpID = NULL)
Return E_POINTER;

Return E_NOTIMPL;
}
STDMETHODIMP CZoomIn: get_Bitmap (OLE_HANDLE * Bitmap)
{
If (Bitmap = NULL)
Return E_POINTER;
* Bitmap = (OLE_HANDLE) m_hBitmap;
Return S_ OK;
}
STDMETHODIMP CZoomIn: get_Category (BSTR * categoryName)
{
If (categoryName = NULL)
Return E_POINTER;

* CategoryName =: SysAllocString (L \ "Developer Samples \");
Return S_ OK;
}
STDMETHODIMP CZoomIn: OnCreate (IDispatch * hook)
{
M_ipSceneHookHelper.CreateInstance (CLSID_SceneHookHelper );
HRESULT hr = m_ipSceneHookHelper-> putref_Hook (hook );
Return hr;
}
STDMETHODIMP CZoomIn: OnClick ()
{
// HRESULT checking omitted for clarity
IScenePtr ipScene;
M_ipSceneHookHelper-> get_Scene (& ipScene );
ISceneGraphPtr ipSceneGraph;
IpScene-> get_SceneGraph (& ipSceneGraph );

ISceneViewer * pViewer;
IpSceneGraph-> get_ActiveViewer (& pViewer); [Page]
Fire_BeforeDraw (pViewer, NULL );
IpSceneGraph-> RefreshViewers ();
Fire_AfterDraw (pViewer );
Return S_ OK;
} // Define through1cpp. idl: IDL source for define through1cpp. dll
//
// This file will be processed by the MIDL tool
// Produce the type library (Walkthrough1Cpp. tlb) and define alling code.
Import \ "oaidl. idl \";
Import \ "ocidl. idl \";
[
Object,
Uuid (5CA669DE-0DFE-4C49-8546-B8AE24A629D5 ),

Helpstring (\ "IZoomIn Interface \"),
Pointer_default (unique)
]
Interface IZoomIn: IUnknown
{
};
[
Uuid (5C0841D7-80E7-4CDB-AFCD-5D77B23B879E ),
Version (1.0 ),
Helpstring (\ "Walkthrough1Cpp 1.0 Type Library \")
]
Library export through1cpplib
{
Importlib (\ "stdole32.tlb \");
Importlib (\ "stdole2.tlb \");
Importlib (\ "e :\\ Program Files \ ArcGIS \ com \ esriSystemUI. olb \");
[
Uuid (C9265E95-7317-4CB2-9ACC-E3EA5C9CA550 ),
Helpstring (\ "ZoomIn Class \")
]
Coclass ZoomIn
{
[Default] interface IUnknown;
Interface IZoomIn;
Interface ICommand;
// [Default] interface IZoomIn;
};
};

// Esri3DAnalystCP. h
# Ifndef _ ESRI3DANALYSTCP_H _
# Define _ ESRI3DANALYSTCP_H _
# Import \ "E :\\ Program Files \ ArcGIS \ com \ esri3DAnalyst. olb \" raw_interfaces_only, raw_native_types,
No_namespace, named_guids // \ "Import typelib \"
Template <class T>
Class CProxyISceneGraphEvents: public IConnectionPointImpl <T, & IID_ISceneGraphEvents,
CComDynamicUnkArray>
{
// Warning this class may be recreated by the wizard.
Public:
HRESULT Fire_ActiveViewerChanged (ISceneViewer * pViewer)
{
HRESULT ret;
T * pT = static_cast <T *> (this );
Int nConnectionIndex;
Int nConnections = m_vec.GetSize ();

For (nConnectionIndex = 0; nConnectionIndex <nConnections; nConnectionIndex ++)
{
PT-> Lock ();
CComPtr <IUnknown> sp = m_vec.GetAt (nConnectionIndex); [Page]
PT-> Unlock ();
ISceneGraphEvents * pISceneGraphEvents = reinterpret_cast <ISceneGraphEvents *>
(Sp. p );
If (pISceneGraphEvents! = NULL)
Ret = pISceneGraphEvents-> ActiveViewerChanged (pViewer );
} Return ret;

}
HRESULT Fire_ViewerAdded (ISceneViewer * pViewer)
{
HRESULT ret;
T * pT = static_cast <T *> (this );
Int nConnectionIndex;
Int nConnections = m_vec.GetSize ();

For (nConnectionIndex = 0; nConnectionIndex <nConnections; nConnectionIndex ++)
{
PT-> Lock ();
CComPtr <IUnknown> sp = m_vec.GetAt (nConnectionIndex );
PT-> Unlock ();
ISceneGraphEvents * pISceneGraphEvents = reinterpret_cast <ISceneGraphEvents *>
(Sp. p );
If (pISceneGraphEvents! = NULL)
Ret = pISceneGraphEvents-> ViewerAdded (pViewer );
} Return ret;

} HRESULT Fire_ViewerRemoved (ISceneViewer * pViewer)
{
HRESULT ret;
T * pT = static_cast <T *> (this );
Int nConnectionIndex;
Int nConnections = m_vec.GetSize ();

For (nConnectionIndex = 0; nConnectionIndex <nConnections; nConnectionIndex ++)
{
PT-> Lock ();
CComPtr <IUnknown> sp = m_vec.GetAt (nConnectionIndex );
PT-> Unlock ();
ISceneGraphEvents * pISceneGraphEvents = reinterpret_cast <ISceneGraphEvents *>
(Sp. p );
If (pISceneGraphEvents! = NULL)
Ret = pISceneGraphEvents-> ViewerRemoved (pViewer );
} Return ret;

}
HRESULT Fire_InteractionStopped ()
{
HRESULT ret;
T * pT = static_cast <T *> (this );
Int nConnectionIndex;
Int nConnections = m_vec.GetSize ();

For (nConnectionIndex = 0; nConnectionIndex <nConnections; nConnectionIndex ++) [Page]
{
PT-> Lock ();
CComPtr <IUnknown> sp = m_vec.GetAt (nConnectionIndex );
PT-> Unlock ();
ISceneGraphEvents * pISceneGraphEvents = reinterpret_cast <ISceneGraphEvents *>
(Sp. p );
If (pISceneGraphEvents! = NULL)
Ret = pISceneGraphEvents-> InteractionStopped ();
} Return ret;

}
HRESULT Fire_RecordingStarted (BSTR Name)
{
HRESULT ret;
T * pT = static_cast <T *> (this );
Int nConnectionIndex;
Int nConnections = m_vec.GetSize ();

For (nConnectionIndex = 0; nConnectionIndex <nConnections; nConnectionIndex ++)
{
PT-> Lock ();
CComPtr <IUnknown> sp = m_vec.GetAt (nConnectionIndex );
PT-> Unlock ();
ISceneGraphEvents * pISceneGraphEvents = reinterpret_cast <ISceneGraphEvents *>
(Sp. p );
If (pISceneGraphEvents! = NULL)
Ret = pISceneGraphEvents-> RecordingStarted (Name );
} Return ret;

}

HRESULT Fire_RecordingStopped ()
{
HRESULT ret;
T * pT = static_cast <T *> (this );
Int nConnectionIndex;
Int nConnections = m_vec.GetSize ();

For (nConnectionIndex = 0; nConnectionIndex <nConnections; nConnectionIndex ++)
{
PT-> Lock ();
CComPtr <IUnknown> sp = m_vec.GetAt (nConnectionIndex );
PT-> Unlock ();
ISceneGraphEvents * pISceneGraphEvents = reinterpret_cast <ISceneGraphEvents *>
(Sp. p );
If (pISceneGraphEvents! = NULL)
Ret = pISceneGraphEvents-> RecordingStopped ();
} Return ret;

}
HRESULT Fire_BeforeDraw (ISceneViewer * pViewer, VARIANT_BOOL * pbHandled)
{
ISceneGraphPtr ipSceneGraph;
PViewer-> get_SceneGraph (& ipSceneGraph); [Page]
IEnvelopePtr pExtent;
IpSceneGraph-> get_Extent (& pExtent );

ICameraPtr pCamera;
PViewer-> get_Camera (& pCamera );
Double Near;
Double Far;
Double Angle;
Double m_aspect;
PCamera-> QueryViewingPlatformMBB (pExtent, & Near, & Far, & Angle, & m_aspect );
Glable (GL_FOG );
GlFogf (GL_FOG_MODE, GL_LINEAR );
GlFogf (GL_FOG_START, Near );
GlFogf (GL_FOG_END, Far );
Float red= 0.8;
Float green = 0.4;
Float blue = 0.4;

GLfloat FogColor [4] = {red, green, blue, 1 };

GlFogfv (GL_FOG_COLOR, FogColor );

HRESULT ret;
T * pT = static_cast <T *> (this );
Int nConnectionIndex;
Int nConnections = m_vec.GetSize ();

For (nConnectionIndex = 0; nConnectionIndex <nConnections; nConnectionIndex ++)
{
PT-> Lock ();
CComPtr <IUnknown> sp = m_vec.GetAt (nConnectionIndex );
PT-> Unlock ();
ISceneGraphEvents * pISceneGraphEvents = reinterpret_cast <ISceneGraphEvents *>
(Sp. p );
If (pISceneGraphEvents! = NULL)
Ret = pISceneGraphEvents-> BeforeDraw (pViewer, pbHandled );
} Return ret;

}
HRESULT Fire_AfterDraw (ISceneViewer * pViewer)
{

GlDisable (GL_FOG );
HRESULT ret;
T * pT = static_cast <T *> (this );
Int nConnectionIndex;
Int nConnections = m_vec.GetSize ();

For (nConnectionIndex = 0; nConnectionIndex <nConnections; nConnectionIndex ++)
{
PT-> Lock ();
CComPtr <IUnknown> sp = m_vec.GetAt (nConnectionIndex );
PT-> Unlock ();
ISceneGraphEvents * pISceneGraphEvents = reinterpret_cast <ISceneGraphEvents *> [Page]
(Sp. p );
If (pISceneGraphEvents! = NULL)
Ret = pISceneGraphEvents-> AfterDraw (pViewer );
} Return ret;

}
};
# Endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.