C++ WMP EventDispatcher:如何知道是谁发送了事件?
我将Windows Media Player嵌入到一个C程序中。我在WMP SDK中找到了C++中的WMP主机示例。它包含一个偶数调度器。但是,当我收到一个事件时,我如何知道是谁发送了该事件,以及如何访问该类对象的变量?例如,我想设置一个类成员(变量)或调用一个方法 CWMPHost对象创建包含WMP对象的窗口并创建事件对象。最低限度的代码是:C++ WMP EventDispatcher:如何知道是谁发送了事件?,c++,windows-media-player,wmp,C++,Windows Media Player,Wmp,我将Windows Media Player嵌入到一个C程序中。我在WMP SDK中找到了C++中的WMP主机示例。它包含一个偶数调度器。但是,当我收到一个事件时,我如何知道是谁发送了该事件,以及如何访问该类对象的变量?例如,我想设置一个类成员(变量)或调用一个方法 CWMPHost对象创建包含WMP对象的窗口并创建事件对象。最低限度的代码是: LRESULT CWMPHost::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&
LRESULT CWMPHost::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
AtlAxWinInit();
CComPtr<IAxWinHostWindow> spHost;
CComPtr<IConnectionPointContainer> spConnectionContainer;
CComWMPEventDispatch *pEventListener = NULL;
CComPtr<IWMPEvents> spEventListener;
HRESULT hr;
RECT rcClient;
m_dwAdviseCookie = 0;
// create window
GetClientRect(&rcClient);
m_wndView.Create(m_hWnd, rcClient, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN, 0);
// load OCX in window
hr = m_wndView.QueryHost(&spHost);
hr = spHost->CreateControl(CComBSTR(_T("{6BF52A52-394A-11d3-B153-00C04F79FAA6}")), m_wndView, 0);
hr = m_wndView.QueryControl(&m_spWMPPlayer);
// start listening to events
hr = CComWMPEventDispatch::CreateInstance(&pEventListener);
spEventListener = pEventListener;
hr = m_spWMPPlayer->QueryInterface(&spConnectionContainer);
// See if OCX supports the IWMPEvents interface
hr = spConnectionContainer->FindConnectionPoint(__uuidof(IWMPEvents), &m_spConnectionPoint);
if (FAILED(hr))
{
// If not, try the _WMPOCXEvents interface, which will use IDispatch
hr = spConnectionContainer->FindConnectionPoint(__uuidof(_WMPOCXEvents), &m_spConnectionPoint);
}
hr = m_spConnectionPoint->Advise(spEventListener, &m_dwAdviseCookie);
return 0;
}
LRESULT CWMPHost::OnCreate(UINT-uMsg、WPARAM-WPARAM、LPARAM-LPARAM、BOOL&bHandled)
{
AtlAxWinInit();
斯波斯特伯爵;
CComPtr spConnectionContainer;
ccommpeventdispatch*pEventListener=NULL;
CComPtr spEventListener;
HRESULT-hr;
RECT-rcClient;
m_=0;
//创建窗口
GetClientRect(&rcClient);
创建(m_hWnd,rcClient,NULL,WS_CHILD | WS|u VISIBLE | WS|u clipcchildren,0);
//在窗口中加载OCX
hr=m_wndView.QueryHost(&spHost);
hr=spHost->CreateControl(CComBSTR({6BF52A52-394A-11d3-B153-00C04F79FAA6})),m_wndView,0;
hr=m_wndView.QueryControl(&m_spWMPPlayer);
//开始收听事件
hr=ccommpeventdispatch::CreateInstance(&pEventListener);
spEventListener=pEventListener;
hr=m_spWMPPlayer->QueryInterface(&spConnectionContainer);
//查看OCX是否支持IWMPEvents接口
hr=spConnectionContainer->FindConnectionPoint(\uuuuIdof(iwmEvents),&m\uSPConnectionPoint);
如果(失败(小时))
{
//如果没有,请尝试使用IDispatch的_WMPOCXEvents接口
hr=spConnectionContainer->FindConnectionPoint(\uuuuuIdof(\uWmpocxevents),&m\uSPConnectionPoint);
}
hr=m_spConnectionPoint->advision(spEventListener和m_dwadvisioncookie);
返回0;
}
完整的示例代码可在编辑:我的改进解决方案满足我的需要: 首先,我解开了include依赖关系,并针对循环include应用了include防护。
CWMPHost
类定义不需要了解CWMPEventDispatch
类;但是,它的实现确实如此,因此CWMPEventDispatch.h
类定义包含在CWMPHost.cpp
文件中,而不包含在CWMPHost.h
文件中
这允许将CWMPEventDispatch
的成员定义为指向所属CWMPHost
对象的指针:
CWMPHost *pCWMPHost;
它是在CWMPHost::Create
方法中设置的:
hr = CComWMPEventDispatch::CreateInstance(&pEventListener);
pEventListener->pCWMPHost= this;
现在,事件调度器可以访问创建该调度器的
CWMPHost
对象的方法和成员。Hmmm…我看到一个反对票和一个接近的建议,但有谁能帮助我或告诉我该怎么做吗?您可以放心地假设是WMP生成了该事件。没有其他人会这样做。@Hans Passant,我打算让多个玩家处于活动状态,所以我必须知道是哪一个。用于实现IwmEvents的类需要一个额外的变量来存储对其中一个SPWMPPlayer的引用。总的来说,这不是一件非常明智的事情。WMP是一个很重的对象,同时有多个播放媒体通常不是很有用。@Hans Pasant,但是看看上面代码中如何创建事件调度器,没有办法传递该引用。这就是问题所在。(请注意:我不是C++程序员,我是C程序员,所以也许我缺少C++所需的一些知识。)