home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / directshow / bda / bdasample / bdasampl.cpp next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  10.7 KB  |  400 lines

  1. //------------------------------------------------------------------------------
  2. // File: Bdasampl.cpp
  3. //
  4. // Desc: Sample code implementing BDA graph building.
  5. //
  6. // Copyright (c) 2000, Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9. #include "resource.h"
  10. #include "graph.h"
  11. #include <initguid.h>
  12.  
  13. // globals
  14. HWND                hwndMain;
  15. HWND                hwndDlg;
  16. HINSTANCE           hInst;
  17. TCHAR               szAppName[]  = TEXT("BDASampl");
  18. TCHAR               szAppTitle[] = TEXT("BDA Sample");
  19.  
  20. CBDAFilterGraph*    pfg         = NULL;
  21.  
  22.  
  23. INT WINAPI
  24. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine,
  25.     INT nCmdShow)
  26. {
  27.     MSG         msg;
  28.     HWND        hwnd;
  29.     WNDCLASS    wndclass;
  30.     HACCEL      hAccel;
  31.     HDC         hdc;
  32.  
  33.     hdc     = CreateCompatibleDC(NULL);
  34.     hInst   = hInstance;
  35.  
  36.     wndclass.style         = 0; //CS_HREDRAW | CS_VREDRAW;
  37.     wndclass.lpfnWndProc   = WndProc;
  38.     wndclass.cbClsExtra    = 0;
  39.     wndclass.cbWndExtra    = 0;
  40.     wndclass.hInstance     = hInst;
  41.     wndclass.hIcon         = LoadIcon(hInst, "BDASAMPLICON");
  42.     wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  43.     wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
  44.     wndclass.lpszMenuName  = szAppName;
  45.     wndclass.lpszClassName = szAppName;
  46.     RegisterClass(&wndclass);
  47.  
  48.     hwnd = CreateWindow(szAppName, szAppTitle, WS_OVERLAPPEDWINDOW | 
  49.                 WS_CLIPCHILDREN, 200, 200, 500, 280, NULL, NULL, hInst, NULL);
  50.  
  51.     ASSERT(hwnd);
  52.  
  53.     // Create the BDA filter graph and initialize its components
  54.     pfg = new CBDAFilterGraph();
  55.  
  56.     ASSERT(pfg);
  57.     
  58.     ShowWindow(hwnd, nCmdShow);
  59.     hwndMain = hwnd;
  60.  
  61.     hAccel = LoadAccelerators(hInst, MAKEINTRESOURCE(ACC_GRAPH));
  62.  
  63.     while(GetMessage(&msg, NULL, 0, 0) > 0)
  64.     {
  65.         if(!TranslateAccelerator(hwnd, hAccel, &msg))
  66.         {
  67.             if(!IsDialogMessage(hwndDlg, &msg))
  68.             {
  69.                 TranslateMessage(&msg);
  70.                 DispatchMessage(&msg);
  71.             }
  72.         }
  73.     }
  74.  
  75.     // Release the BDA components and clean up
  76.     delete pfg;
  77.     DeleteDC(hdc);
  78.     
  79.     return msg.wParam;
  80. }
  81.  
  82.  
  83. // WndProc                                                                    
  84. LRESULT CALLBACK
  85. WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  86. {
  87.     switch(message)
  88.     {
  89.     case WM_CREATE:
  90.         return 0;
  91.  
  92.     case WM_SIZE:
  93.         if(pfg->m_fGraphBuilt)
  94.             pfg->SetVideoWindow(hwndMain);
  95.         break;
  96.  
  97.     case WM_COMMAND:
  98.         switch(LOWORD(wParam))
  99.         {
  100.         case IDM_BUILD_ATSC:
  101.             if(FAILED(pfg->BuildGraph(ATSC)))
  102.             {
  103.                 ErrorMessageBox("Could not Build the ATSC BDA FilterGraph");
  104.             }
  105.             else
  106.             {
  107.                 pfg->SetVideoWindow(hwndMain);
  108.             }
  109.             
  110.             EnableMenuItem((HMENU)wParam, IDM_BUILD_ATSC, MF_GRAYED);
  111.             // DVB is not implemented for DX8 RC0
  112.             //EnableMenuItem((HMENU)wParam, IDM_BUILD_DVB, MF_GRAYED);
  113.             
  114.             break;
  115.  
  116.         /* DVB is not implemented for DX8 RC0
  117.  
  118.         case IDM_BUILD_DVB:
  119.             if(FAILED(pfg->BuildGraph(DVB)))
  120.             {
  121.                 ErrorMessageBox("Could not Build the DVB BDA FilterGraph");
  122.             }
  123.             else
  124.             {
  125.                 pfg->SetVideoWindow(hwndMain);
  126.             }
  127.             EnableMenuItem((HMENU)wParam, IDM_BUILD_ATSC, MF_GRAYED);
  128.             EnableMenuItem((HMENU)wParam, IDM_BUILD_DVB, MF_GRAYED);
  129.  
  130.             break;
  131.         */
  132.  
  133.         case IDM_RUN_GRAPH:
  134.             if(pfg->m_fGraphBuilt)
  135.             {   
  136.                 if(!pfg->m_fGraphRunning)
  137.                 {
  138.                     if(FAILED(pfg->RunGraph()))
  139.                     {
  140.                         ErrorMessageBox("Could not Play the FilterGraph");
  141.                     }
  142.                 }
  143.             }
  144.             else
  145.             {
  146.                 ErrorMessageBox("The FilterGraph is not built yet");
  147.             }
  148.  
  149.             break;
  150.  
  151.         case IDM_STOP_GRAPH:
  152.             if(pfg->m_fGraphBuilt)
  153.             {
  154.                 if(pfg->m_fGraphRunning)
  155.                 {
  156.                     if(FAILED(pfg->StopGraph()))
  157.                     {
  158.                         ErrorMessageBox("Could not Stop the FilterGraph");
  159.                     }
  160.                 }
  161.             }
  162.             else
  163.             {
  164.                 ErrorMessageBox("The FilterGraph is not Built Yet");
  165.             }
  166.             
  167.             break;
  168.  
  169.         case IDM_SELECT_CHANNEL:
  170.             if(pfg->m_fGraphBuilt)
  171.             {
  172.                 hwndDlg = CreateDialog(hInst, MAKEINTRESOURCE(IDD_SELECT_CHANNEL),
  173.                     hwnd, reinterpret_cast<DLGPROC>(SelectChannelDlgProc));
  174.             }
  175.             else
  176.             {
  177.                 ErrorMessageBox("The FilterGraph is not built yet");
  178.             }
  179.             
  180.             break;
  181.  
  182.         case IDM_ABOUT:
  183.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, 
  184.                 reinterpret_cast<DLGPROC>(AboutDlgProc));
  185.             break;
  186.  
  187.         case IDM_EXIT:
  188.             DbgTerminate();
  189.             PostQuitMessage(0);
  190.             break;
  191.  
  192.         default:
  193.             break;
  194.         }
  195.  
  196.     case WM_PAINT:
  197.         break;
  198.  
  199.     case WM_ERASEBKGND:
  200.         break;
  201.  
  202.     case WM_INITMENU:
  203.         if(pfg->m_fGraphFailure)
  204.         {
  205.             EnableMenuItem((HMENU)wParam, IDM_BUILD_ATSC,       MF_GRAYED);
  206.             // DVB is not implemented for DX8 RC0 
  207.             //EnableMenuItem((HMENU)wParam, IDM_BUILD_DVB,        MF_GRAYED);
  208.             EnableMenuItem((HMENU)wParam, IDM_SELECT_CHANNEL,   MF_GRAYED);
  209.             EnableMenuItem((HMENU)wParam, IDM_RUN_GRAPH,        MF_GRAYED);
  210.             EnableMenuItem((HMENU)wParam, IDM_STOP_GRAPH,       MF_GRAYED);
  211.         }
  212.         else
  213.         {
  214.             EnableMenuItem((HMENU)wParam, IDM_RUN_GRAPH, 
  215.                 pfg->m_fGraphRunning ? MF_GRAYED : MF_ENABLED);
  216.  
  217.             EnableMenuItem((HMENU)wParam, IDM_BUILD_ATSC, 
  218.                 pfg->m_fGraphBuilt ? MF_GRAYED : MF_ENABLED);
  219.         
  220.             // DVB is not implemented for DX8 RC0 
  221.             //EnableMenuItem((HMENU)wParam, IDM_BUILD_DVB, MF_GRAYED
  222.             //    pfg->m_fGraphBuilt ? MF_GRAYED : MF_ENABLED);
  223.  
  224.             // we can stop viewing if it's currently viewing
  225.             EnableMenuItem((HMENU)wParam, IDM_STOP_GRAPH, 
  226.                 (pfg->m_fGraphRunning) ? MF_ENABLED : MF_GRAYED);
  227.  
  228.             EnableMenuItem((HMENU)wParam, IDM_SELECT_CHANNEL, MF_ENABLED);
  229.         }
  230.  
  231.         break;
  232.  
  233.     case WM_CLOSE:
  234.     case WM_DESTROY:
  235.         DbgTerminate();
  236.         PostQuitMessage(0);
  237.         break;
  238.  
  239.     case WM_ACTIVATE:
  240.         if(LOWORD(wParam) != WA_INACTIVE)
  241.         {
  242.             HWND hwndPrevious = (HWND)lParam;
  243.  
  244.             if((GetParent(hwndPrevious) == hwnd) && IsWindow(hwndPrevious) &&
  245.                 IsWindowVisible(hwndPrevious))
  246.             {
  247.                 if(!SetActiveWindow(hwndPrevious))
  248.                 {
  249.                 }
  250.             }
  251.             return 0;
  252.         }
  253.     default:
  254.         break;
  255.     }
  256.  
  257.     return DefWindowProc(hwnd, message, wParam, lParam);
  258. }
  259.  
  260.  
  261. // AboutDlgProc
  262. //
  263. // Dialog Procedure for the "about" dialog box.
  264. //
  265. BOOL CALLBACK 
  266. AboutDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  267. {
  268.     switch(msg) 
  269.     {
  270.     case WM_COMMAND:
  271.         EndDialog(hwnd, TRUE);
  272.         return TRUE;
  273.     
  274.     case WM_INITDIALOG:
  275.         return TRUE;
  276.     }
  277.     return FALSE;
  278. }
  279.  
  280.  
  281. // SelectChannelDlgProc
  282. // Dialog Procedure for the "about" dialog box.
  283. //                                                                              
  284. BOOL CALLBACK
  285. SelectChannelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  286. {
  287.     LONG    ch_maj  = pfg->m_MajorChannel;
  288.     LONG    ch_min  = pfg->m_MinorChannel;
  289.     BOOL    bRet    = FALSE;
  290.     
  291.     switch(message)
  292.     {
  293.     case WM_INITDIALOG:
  294.         EnableWindow(hwndMain, FALSE); //disable parent
  295.         pfg->Refresh(hDlg);
  296.         SetFocus(hDlg);
  297.         
  298.         return FALSE;
  299.  
  300.     case WM_COMMAND:
  301.         switch(LOWORD(wParam))
  302.         {
  303.         case IDC_ENTER:
  304.             ch_maj = (LONG) GetDlgItemInt(hDlg, IDC_MAJOR_CHANNEL, &bRet, FALSE);
  305.             ch_min = (LONG) GetDlgItemInt(hDlg, IDC_MINOR_CHANNEL, &bRet, TRUE);
  306.             
  307.             pfg->ChangeChannel(ch_maj, ch_min);
  308.  
  309.             // update dialog box
  310.             pfg->Refresh(hDlg);
  311.  
  312.             SetFocus(hDlg);
  313.             break;
  314.  
  315.         case IDOK:
  316.             ch_maj = (LONG) GetDlgItemInt(hDlg, IDC_MAJOR_CHANNEL, &bRet, FALSE);
  317.             ch_min = (LONG) GetDlgItemInt(hDlg, IDC_MINOR_CHANNEL, &bRet, TRUE);
  318.             
  319.             pfg->ChangeChannel(ch_maj, ch_min);
  320.  
  321.             // update dialog box
  322.             pfg->Refresh(hDlg);
  323.  
  324.             // fall through
  325.             
  326.         case IDCANCEL:
  327.             DestroyWindow(hDlg);
  328.             break;
  329.  
  330.         case IDC_MAJOR_CHANNEL_UP:
  331.             ch_min = (LONG) GetDlgItemInt(hDlg, IDC_MINOR_CHANNEL, &bRet, TRUE);
  332.             pfg->ChangeChannel(CHANNEL_UP, ch_min); 
  333.             
  334.             // update dialog box
  335.             pfg->Refresh(hDlg);
  336.  
  337.             SetFocus(hDlg);
  338.  
  339.             break;
  340.  
  341.         case IDC_MAJOR_CHANNEL_DOWN:
  342.             ch_min = (LONG) GetDlgItemInt(hDlg, IDC_MINOR_CHANNEL, &bRet, TRUE);
  343.             pfg->ChangeChannel(CHANNEL_DOWN, ch_min);
  344.  
  345.             // update dialog box
  346.             pfg->Refresh(hDlg);
  347.  
  348.             SetFocus(hDlg);
  349.             break;
  350.  
  351.         case IDC_MINOR_CHANNEL_UP:
  352.             ch_maj = (LONG) GetDlgItemInt(hDlg, IDC_MAJOR_CHANNEL, &bRet, FALSE);
  353.             pfg->ChangeChannel(ch_maj, CHANNEL_UP); 
  354.             
  355.             // update dialog box
  356.             pfg->Refresh(hDlg);
  357.  
  358.             SetFocus(hDlg);
  359.  
  360.             break;
  361.  
  362.         case IDC_MINOR_CHANNEL_DOWN:
  363.             ch_maj = (LONG) GetDlgItemInt(hDlg, IDC_MAJOR_CHANNEL, &bRet, FALSE);
  364.             pfg->ChangeChannel(ch_maj, CHANNEL_DOWN);
  365.  
  366.             // update dialog box
  367.             pfg->Refresh(hDlg);
  368.  
  369.             SetFocus(hDlg);
  370.             break;
  371.  
  372.         default:
  373.             EnableWindow(hwndMain, TRUE); //reenable parent
  374.             return TRUE;
  375.         }
  376.     }
  377.  
  378.     return FALSE;
  379. }
  380.  
  381.  
  382. // ErrorMessageBox
  383. //
  384. // Opens a Message box with a error message in it.  The user can     
  385. // select the OK button to continue.
  386. //
  387. VOID
  388. ErrorMessageBox(LPTSTR sz,...)
  389. {
  390.     static TCHAR    ach[2000];
  391.     va_list         va;
  392.  
  393.     va_start(va, sz);
  394.     wvsprintf(ach, sz, va);
  395.     va_end(va);
  396.  
  397.     MessageBox(hwndMain, ach, NULL, MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL);
  398. }
  399.  
  400.