How to listen for arcengine COM events in VC code

Source: Internet
Author: User
Tags definition class definition function definition

In Arcengine, many components send information in event mode. For example, in Mapcontrol, there are events such as mouse press events, mouse movements and so on, Iworkspaceedit also have the beginning edit, end edit and so on. Sometimes we need to be aware of these events in order to process the necessary information when they occur. So, how do you listen to events for components? The following is an example of a listener editing event to illustrate how listening is handled. To turn on help, we'll see the following code:

struct __declspec(uuid("0b437962
-89f
9-11d4-8b
5f
-000000000000"))
IWorkspaceEditEvents : IUnknown
{
  //
  // Raw methods provided by interface
  //
  virtual HRESULT __stdcall OnStartEditing (
    VARIANT_BOOL withUndoRedo ) = 0;
  virtual HRESULT __stdcall OnStopEditing (
    VARIANT_BOOL saveEdits ) = 0;
  virtual HRESULT __stdcall OnStartEditOperation ( ) = 0;
  virtual HRESULT __stdcall OnAbortEditOperation ( ) = 0;
  virtual HRESULT __stdcall OnStopEditOperation ( ) = 0;
  virtual HRESULT __stdcall OnUndoEditOperation ( ) = 0;
  virtual HRESULT __stdcall OnRedoEditOperation ( ) = 0;
};

See the function definition of the edit event from the above code. We need to build a class that overloads the above function.

class Caeeditevents:
Public ccomobjectroot,
public iworkspaceeditevents
{
Public:
Caee Ditevents (void);
~caeeditevents (void);
Begin_com_map (caeeditevents)
Com_interface_entry (iworkspaceeditevents)
End_com_map ()
STDMETHOD ( onstartediting) (Variant_bool Withundoredo)
{
:: MessageBox (NULL, _t ("Start editing?)". "), _t (" Note!!! "), MB_OK);
return E_NOTIMPL;   
}
Stdmethod (onstopediting) (Variant_bool saveedits)
{
return E_NOTIMPL;
  
Stdmethod (onstarteditoperation) ()
{
return E_NOTIMPL;
  
Stdmethod (onaborteditoperation) ()
{
return E_NOTIMPL;
  
Stdmethod (onstopeditoperation) ()
{
return E_NOTIMPL;
  
Stdmethod (onundoeditoperation) ()
{
return E_NOTIMPL;
  
Stdmethod (onredoeditoperation) ()
{
return E_NOTIMPL;
}
};

Stdmethod's definition is not clear? Alas, is actually a macro definition, simplifying the definition of virtual function. STDMETHOD (Onredoeditoperation) () is actually: virtual HRESULT __stdcall onredoeditoperation (), very simple!! If there are a lot of do not understand, then quickly look at the knowledge of COM. See, huh? We implement the Iworkspaceeditevents interface function once.

Notice that we added a public ccomobjectroot to the class definition, why? No longer explained here, the right to be necessary. Want to know the reason, oneself reference com aspect knowledge. In this way, we have all the listening classes ready.

The class only does one thing, when you start editing this event, send out an inquiry, and the inquiry does nothing, just ask. Below, it is officially started. Define variables First: Caeeditevents *g_pevents;

DWORD M_dwcookie;

Where is this variable defined? See where you like to define. The definition is complete, should let Iworkspace know, you want to do when, want to tell me! How can I tell you? Look below:

g_pEvents = new CComObject<CAEEditEvents>;
  CComPtr<IUnknown> ptrEventUnk = g_pEvents;
  AtlAdvise(ipWorkspace, ptrEventUnk, IID_IWorkspaceEditEvents, &m_dwCookie);

Well, that's the end of it. When iworkspace have any action, nature will tell Caeeditevents. Finally, when you don't need it, don't forget the following code: AtlUnadvise (Ipworkspace, iid_iworkspaceeditevents, M_dwcookie);

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.