2016-08-02 53 views
-1

我在C程序中嵌入Windows Media Player。我在WMP SDK中找到了C++中的WMP Host示例。它包含一个调度员。但是,当我收到一个事件时,我怎么知道谁发送了事件,以及如何访问该类对象的变量?例如,我想设置一个类成员(变量)或调用一个方法。WMP EventDispatcher:如何知道谁发送了事件?

CWMPHost对象创建包含WMP对象的窗口并创建事件对象。最小码为:

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; 
} 

完整的示例代码可以在https://github.com/pauldotknopf/WindowsSDK7-Samples/tree/master/multimedia/WMP/cpp/WMPHost

+0

嗯...我看到一个投票和一个密切的建议,但任何人都可以请帮我或告诉我该怎么办? –

+0

您可以放心地假定它是WMP生成的事件。没有人会。 –

+0

@ Hans-Passant,我打算有不止一个玩家活跃,所以我必须知道哪一个。 –

回答

0

编辑发现:我改进的解决方案,以满足我的需求:

首先,我解开了包括依赖关系和应用包括:防范通告包括。 CWMPHost定义不需要知道CWMPEventDispatch类;但是它的实现确实如此,所以CWMPEventDispatch.h类定义包含在CWMPHost.cpp文件中,但不包含在CWMPHost.h文件中。

这允许以限定的CWMPEventDispatch一个成员是指向所属CWMPHost对象:

CWMPHost *pCWMPHost; 

它被设置在CWMPHost::Create方法:

hr = CComWMPEventDispatch::CreateInstance(&pEventListener); 
    pEventListener->pCWMPHost= this; 

现在事件调度可以访问创建调度程序的CWMPHost对象的方法和成员。

相关问题