你所要做的只是创建一个控件窗口,包含一个IWMPPlayer接口指针,并且对WMP事件进行响应。ATL可以用比MFC更加简便的方式来完成这些任务,当然你也可以使用MFC。你的程序现在就可以播放Windows媒体文件,比如WMA和WMV。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; ... // 创建窗口 GetClientRect(&rcClient); m_wndView.Create(m_hWnd, rcClient, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS); if (NULL == m_wndView.m_hWnd) goto FAILURE; // 在窗口中装载OCX hr = m_wndView.QueryHost(&spHost); if (FAILMSG(hr)) goto FAILURE; hr = spHost->CreateControl(CComBSTR(_T("WMPlayer.OCX")), m_wndView, 0); if (FAILMSG(hr)) goto FAILURE; hr = m_wndView.QueryControl(&m_spWMPPlayer); if (FAILMSG(hr)) goto FAILURE; // 开始监听事件 hr = CComWMPEventDispatch::CreateInstance(&pEventListener); spEventListener = pEventListener; if (FAILMSG(hr)) goto FAILURE; hr = m_spWMPPlayer->QueryInterface(__uuidof(IConnectionPointContainer), (void**)&spConnectionContainer); if (FAILMSG(hr)) goto FAILURE; // 看OCX 是否支持IWMPEvents接口 hr = spConnectionContainer->FindConnectionPoint(__uuidof(IWMPEvents), &m_spConnectionPoint); if (FAILMSG(hr)) goto FAILURE; hr = m_spConnectionPoint->Advise(spEventListener, &m_dwAdviseCookie); if (FAILMSG(hr)) goto FAILURE; return 0;FAILURE: ::PostQuitMessage(0); return 0; }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9403012/viewspace-5490/ ,如需转载,请注明出处,否则将追究法律责任。