000 使用DirectShow 在指定的窗口中播放

Post date: 2015/4/20 上午 02:17:52

#include <windows.h> #include <dshow.h> #define WM_GRAPHNOTIFY WM_APP + 1 #define CLASSNAME "VideoWindow" IGraphBuilder *pGraph = NULL; IMediaControl *pMediaControl = NULL; IVideoWindow *pVidWin = NULL; IMediaEventEx *pEvent = NULL; HWND g_hwnd; void PlayFile(void) { // Create the filter graph manager. CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC,IID_IGraphBuilder, (void **)&pGraph); pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl); pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin); // Build the graph. pGraph->RenderFile(L"Fox.avi", NULL); //Set the video window. pVidWin->put_Owner((OAHWND)g_hwnd); pVidWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS); pGraph->QueryInterface(IID_IMediaEventEx, (void **)&pEvent); pEvent->SetNotifyWindow((OAHWND)g_hwnd, WM_GRAPHNOTIFY, 0); RECT grc; GetClientRect(g_hwnd, &grc); pVidWin->SetWindowPosition(0, 0, grc.right, grc.bottom); // Run the graph. pMediaControl->Run(); } void CleanUp(void) { pEvent->SetNotifyWindow(NULL, 0, 0); pEvent->Release(); pEvent = NULL; pMediaControl->Stop(); pVidWin->put_Visible(OAFALSE); pVidWin->put_Owner(NULL); pMediaControl->Release(); pVidWin->Release(); pGraph->Release(); PostQuitMessage(0); } void HandleEvent() { long evCode, param1, param2; HRESULT hr; if (pEvent == NULL) // This avoids a possible race condition when shutting down the graph. return; while (hr = pEvent->GetEvent(&evCode, &param1, &param2, 0), SUCCEEDED(hr)) { hr = pEvent->FreeEventParams(evCode, param1, param2); if ((EC_COMPLETE == evCode) || (EC_USERABORT == evCode)) { CleanUp(); break; } } } void WindowResize() { RECT grc; GetClientRect(g_hwnd, &grc); if(pVidWin!=NULL) pVidWin->SetWindowPosition(0, 0, grc.right, grc.bottom); } // Message handler. long FAR PASCAL WindowProc( HWND hwnd, UINT msg, UINT wParam, LONG lParam) { switch (msg) { case WM_DESTROY: CleanUp(); PostQuitMessage(0); break; case WM_SIZE: WindowResize(); break; case WM_GRAPHNOTIFY: HandleEvent(); break; default: return (DefWindowProc(hwnd, msg, wParam, lParam)); } return(NULL); } // Main. int PASCAL WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow ) { MSG msg; WNDCLASS wc; CoInitialize(NULL); ZeroMemory(&wc, sizeof wc); wc.lpfnWndProc = WindowProc; wc.hInstance = hInst; wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH ); wc.lpszMenuName = NULL; wc.lpszClassName = CLASSNAME; RegisterClass( &wc ); g_hwnd = CreateWindow( CLASSNAME, "唐伯虎点秋香", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL); ShowWindow( g_hwnd, SW_MAXIMIZE);//nCmdShow ); UpdateWindow( g_hwnd ); PlayFile(); while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } CoUninitialize(); return msg.wParam; }