请问ActiveX controls event发出的消息怎样在container里接收

wb1983126 2003-09-12 04:03:00

控件内部已经做好了, 但是event 发出的消息不知道怎么接收,
是否需要在包装类里面写代码?使用包装类的container 呢?

多谢多谢
...全文
113 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ayqlj 2003-09-15
  • 打赏
  • 举报
回复
这里好难懂呀!!!!
高手是否可以仔细讲讲
让我们学学?
wb1983126 2003-09-14
  • 打赏
  • 举报
回复
谢谢您,不过没看明白,比如:
switch (dispidMember)
{
case 0x3f6:
OnScrollEvent();//here is my event handler
break;
...
}
dispidMember是怎样传给函数Invoke的呢,而OnScrollEvent是怎么用的呢?控件里发出Event的函数是什么?是否要用消息处理来获得控件发出的消息?谢谢
wbbh 2003-09-14
  • 打赏
  • 举报
回复
Visual C++ Concepts: Adding Functionality

Using IDispEventSimpleImplSee Also
Event Handling and ATL | ATLEventHandling Sample
When using IDispEventSimpleImpl to handle events, you will need to:

Derive your class from IDispEventSimpleImpl.
Add an event sink map to your class.
Define _ATL_FUNC_INFO structures describing the events.
Add entries to the event sink map using the SINK_ENTRY_INFO macro.
Implement the methods that you're interested in handling.
Advise and unadvise the event source.
Example
The example below shows you how to handle the DocumentChange event fired by Word's Application object. This event is defined as a method on the ApplicationEvents dispinterface.

The example is from the ATLEventHandling sample.

[
uuid(000209F7-0000-0000-C000-000000000046),
hidden
]
dispinterface ApplicationEvents {
properties:
methods:
[id(0x00000001), restricted, hidden]
void Startup();
[id(0x00000002)]
void Quit();
[id(0x00000003)]
void DocumentChange();
};
The example uses #import to generate the required header files from Word's type library. If you want to use this example with other versions of Word, you must specify the correct mso dll file. For example, Office 2000 provides mso9.dll and OfficeXP provides mso.dll. This code is from stdafx.h.:

#pragma warning (disable : 4146)
#import "C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE\MSO97.DLL" raw_interfaces_only
#import "C:\PROGRAM FILES\COMMON FILES\MICROSOFT SHARED\VBA\VBEEXT1.OLB" raw_interfaces_only
#import "C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE\MSWORD8.OLB" rename("ExitWindows", "WordExitWindows") raw_interfaces_only
#pragma warning (default : 4146)
The only information from the type library actually used in this example is the CLSID of the Word Application object and the IID of the ApplicationEvents interface. This information is only used at compile time.

The following code appears in Simple.h. The relevant code is in bold:

// Declare structure for type information
extern _ATL_FUNC_INFO OnDocChangeInfo;

class ATL_NO_VTABLE CSimple :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CSimple, &CLSID_Simple>,
public IDispatchImpl<ISwitch, &IID_ISwitch, &LIBID_DISPEVENTLib>,
public IDispEventSimpleImpl</*nID =*/ 1, CSimple, &__uuidof(Word::ApplicationEvents)>
{
public:
CSimple()
{
}

DECLARE_REGISTRY_RESOURCEID(IDR_SIMPLE)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CSimple)
COM_INTERFACE_ENTRY(ISwitch)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()

CComPtr<Word::_Application> m_pApp;

// Event handler
// Note the __stdcall calling convention and
// dispinterface-style signature
void __stdcall OnDocChange()
{
// Get a pointer to the _Document interface on the active document
CComPtr<Word::_Document> pDoc;
m_pApp->get_ActiveDocument(&pDoc);

// Get the name from the active document
CComBSTR bstrName;
pDoc->get_Name(&bstrName);

// Create a display string
CComBSTR bstrDisplay(_T("New document title:\n"));
bstrDisplay += bstrName;

// Display the name to the user
USES_CONVERSION;
MessageBox(NULL, W2CT(bstrDisplay), _T("Active Document Changed"), MB_OK);
}

BEGIN_SINK_MAP(CSimple)
SINK_ENTRY_INFO(/*nID =*/ 1, __uuidof(Word::ApplicationEvents), /*dispid =*/ 3, OnDocChange, &OnDocChangeInfo)
END_SINK_MAP()

// ISwitch
public:
STDMETHOD(Start)()
{
// If you already have an object, just return
if (m_pApp)
return S_OK;

// Create an instance of Word's Application object
m_pApp.CoCreateInstance(__uuidof(Word::Application), NULL, CLSCTX_SERVER);

// Make the Word user interface visible
m_pApp->put_Visible(true);

// Forge a connection to enable you to receive events
DispEventAdvise(m_pApp);

return S_OK;
}

STDMETHOD(Stop)()
{
// Check you have an object to unadvise on
if (!m_pApp)
return S_OK;

// Break the connection with the event source
DispEventUnadvise(m_pApp);

// Release the Word application
m_pApp.Release();

return S_OK;
}
};
The following code is from Simple.cpp:

// Define type info structure
_ATL_FUNC_INFO OnDocChangeInfo = {CC_STDCALL, VT_EMPTY, 0};
See Also
Event Handling and ATL | ATLEventHandling Sample



--------------------------------------------------------------------------------

Send feedback to Microsoft

© 2001 Microsoft Corporation. All rights reserved.
masterz 2003-09-13
  • 打赏
  • 举报
回复
// CComQIPtr<IWebBrowser2> m_spBrowser;
// CComQIPtr<IHTMLDocument2> m_spDocument;
// DWORD m_dwScrollSinkCookie;
// HRESULT m_hrScrollSink;
// LPCONNECTIONPOINT m_pScrollConnPoint;
void CXXX::InstallScrollEventSink(bool binstall)
{ //true: install scroll event sink to the HTMLWindow2 in the browser control
//false:uninstall scroll event sink
HRESULT hr=-1;
m_hrScrollSink=-1;
if(m_spBrowser!=NULL)
{
hr=m_spBrowser->get_Document((IDispatch**)&m_spDocument);
}
if(SUCCEEDED(hr)&&m_spDocument!=NULL)
{
IHTMLWindow2* phtmlwnd2=NULL;
LPCONNECTIONPOINTCONTAINER pCPC = NULL;
hr=m_spDocument->get_parentWindow(&phtmlwnd2);
if(SUCCEEDED(hr)&&phtmlwnd2!=NULL)
{
hr = phtmlwnd2->QueryInterface(IID_IConnectionPointContainer, (LPVOID*)&pCPC);
}
if(SUCCEEDED(hr)&&pCPC!=NULL)
{
hr= pCPC->FindConnectionPoint(DIID_HTMLWindowEvents,&m_pScrollConnPoint);
}
if(SUCCEEDED(hr))
{
if(binstall)
m_hrScrollSink=m_pScrollConnPoint->Advise(this->GetUnknown(),&m_dwScrollSinkCookie);
else
{
m_hrScrollSink = -1;
m_pScrollConnPoint->Unadvise(m_dwScrollSinkCookie);
m_pScrollConnPoint->Release();
}
}
if(pCPC)
pCPC->Release();
if(phtmlwnd2!=NULL)
phtmlwnd2->Release();
}
}

STDMETHODIMP CXXX::Invoke(DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS* pDispParams,
VARIANT* pvarResult,
EXCEPINFO* pExcepInfo,
UINT* puArgErr)
{
if (!pDispParams)
return E_INVALIDARG;
switch (dispidMember)
{
case 0x3f6:
OnScrollEvent();//here is my event handler
break;
default:
//...
break;
}

return S_OK;
}

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧