bdasampl.cpp
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:13k
源码类别:

VC书籍

开发平台:

Visual C++

  1. //------------------------------------------------------------------------------
  2. // File: Bdasampl.cpp
  3. //
  4. // Desc: Sample code implementing BDA graph building.
  5. //
  6. // Copyright (c) 2000-2002, Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8. #include "bdasampl.h"
  9. #include "graph.h"
  10. #include "resource.h"
  11. #include <initguid.h>
  12. #include <objbase.h>
  13. // 
  14. // NOTE: In this sample, text strings are hard-coded for 
  15. // simplicity and for readability.  For product code, you should
  16. // use string tables and LoadString().
  17. //
  18. // Global data
  19. HWND                hwndMain=0;
  20. HWND                g_hwndDlg=0;
  21. HINSTANCE           hInst=0;
  22. TCHAR               szAppName[]  = TEXT("BDASampl");
  23. TCHAR               szAppTitle[] = TEXT("BDA Sample");
  24. CBDAFilterGraph*    g_pfg = NULL;
  25. // Constants
  26. const int MAJOR_CHANNEL_LOWER_LIMIT = -1;
  27. const int MAJOR_CHANNEL_UPPER_LIMIT = 126;
  28. const int MINOR_CHANNEL_LOWER_LIMIT = -1;
  29. const int MINOR_CHANNEL_UPPER_LIMIT = 126;
  30. INT WINAPI
  31. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine,
  32.     INT nCmdShow)
  33. {
  34.     MSG         msg={0};
  35.     WNDCLASS    wndclass={0};
  36.     HWND        hwnd=0;
  37.     HACCEL      hAccel=0;
  38.     // Save application instance handle for later use
  39.     hInst = hInstance;
  40.     // Initialize COM library
  41.     HRESULT hr = CoInitializeEx (NULL, COINIT_APARTMENTTHREADED);
  42.     if (FAILED (hr))
  43.     {
  44.         MessageBox(NULL,  TEXT("Failed to initialize COM library!"),
  45.                    TEXT("Initialization Error"), MB_ICONEXCLAMATION);
  46.         return 0;
  47.     }
  48.     // Register window class
  49.     wndclass.style         = 0;
  50.     wndclass.lpfnWndProc   = WndProc;
  51.     wndclass.cbClsExtra    = 0;
  52.     wndclass.cbWndExtra    = 0;
  53.     wndclass.hInstance     = hInst;
  54.     wndclass.hIcon         = LoadIcon(hInst, TEXT("BDASAMPLICON"));
  55.     wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  56.     wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
  57.     wndclass.lpszMenuName  = szAppName;
  58.     wndclass.lpszClassName = szAppName;
  59.     RegisterClass(&wndclass);
  60.     hwnd = CreateWindow(szAppName, szAppTitle, 
  61.                 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, 
  62.                 200, 200, 500, 280, 
  63.                 NULL, NULL, hInst, NULL);
  64.     ASSERT(hwnd);
  65.     // Create the BDA filter graph and initialize its components
  66.     g_pfg = new CBDAFilterGraph();
  67.     ASSERT(g_pfg);
  68.     // If the graph failed to build, don't go any further.
  69.     if (!g_pfg)
  70.     {
  71.         MessageBox(hwnd, TEXT("Failed to create the filter graph!"),
  72.                    TEXT("Initialization Error"), MB_ICONEXCLAMATION);
  73.         return 0;
  74.     }
  75.     // Display main window and configure accelerators    
  76.     ShowWindow(hwnd, nCmdShow);
  77.     hwndMain = hwnd;
  78.     hAccel = LoadAccelerators(hInst, MAKEINTRESOURCE(ACC_GRAPH));
  79.     // Main message loop
  80.     while(GetMessage(&msg, NULL, 0, 0) > 0)
  81.     {
  82.         if(!TranslateAccelerator(hwnd, hAccel, &msg))
  83.         {
  84.             TranslateMessage(&msg);
  85.             DispatchMessage(&msg);
  86.         }
  87.     }
  88.     // Release the BDA components and clean up
  89.     delete g_pfg;
  90.     CoUninitialize ();
  91.     
  92.     return msg.wParam;
  93. }
  94. // WndProc                                                                    
  95. LRESULT CALLBACK
  96. WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  97. {
  98.     switch(message)
  99.     {
  100.         case WM_CREATE:
  101.         {
  102.             DbgInitialise (hInst);
  103.             break;
  104.         }
  105.         case WM_SIZE:
  106.         {
  107.             if(g_pfg && g_pfg->m_fGraphBuilt)
  108.                 g_pfg->SetVideoWindow(hwndMain);
  109.             break;
  110.         }
  111.         
  112.         case WM_COMMAND:
  113.         switch(LOWORD(wParam))
  114.         {
  115.             case IDM_BUILD_ATSC:
  116.             {
  117.                 if (!g_pfg)
  118.                     break;
  119.                     
  120.                 if(FAILED(g_pfg->BuildGraph(ATSC)))
  121.                 {
  122.                     ErrorMessageBox(TEXT("Could not Build the ATSC BDA FilterGraph."));
  123.                 }
  124.                 else
  125.                 {
  126.                     g_pfg->SetVideoWindow(hwndMain);
  127.                 }
  128.         
  129.                 HMENU hMenu = GetSubMenu(GetMenu(hwnd), 1);
  130.                 EnableMenuItem(hMenu, IDM_BUILD_ATSC, MF_GRAYED | MF_BYCOMMAND);
  131.                 EnableMenuItem(hMenu, IDM_STOP_GRAPH, 
  132.                     (g_pfg->m_fGraphRunning) ? (MF_BYCOMMAND|MF_ENABLED) : (MF_BYCOMMAND|MF_GRAYED));
  133.                 EnableMenuItem(hMenu, IDM_SELECT_CHANNEL, 
  134.                     (g_pfg->m_fGraphBuilt) ? (MF_BYCOMMAND|MF_ENABLED) : (MF_BYCOMMAND|MF_GRAYED));
  135.                 break;
  136.             }
  137.             
  138.             case IDM_RUN_GRAPH:
  139.             {
  140.                 if (!g_pfg)
  141.                     break;
  142.                     
  143.                 if(g_pfg->m_fGraphBuilt)
  144.                 {   
  145.                     if(!g_pfg->m_fGraphRunning)
  146.                     {
  147.                         if(FAILED(g_pfg->RunGraph()))
  148.                         {
  149.                             ErrorMessageBox(TEXT("Could not play the FilterGraph."));
  150.                         }
  151.                     }
  152.                 }
  153.                 else
  154.                 {
  155.                     ErrorMessageBox(TEXT("The FilterGraph is not yet built."));
  156.                 }
  157.                 break;
  158.             }
  159.             
  160.             case IDM_STOP_GRAPH:
  161.             {
  162.                 if (!g_pfg)
  163.                     break;
  164.                     
  165.                 if(g_pfg->m_fGraphBuilt)
  166.                 {
  167.                     if(g_pfg->m_fGraphRunning)
  168.                     {
  169.                         if(FAILED(g_pfg->StopGraph()))
  170.                         {
  171.                             ErrorMessageBox(TEXT("Could not stop the FilterGraph,"));
  172.                         }
  173.                     }
  174.                 }
  175.                 else
  176.                 {
  177.                     ErrorMessageBox(TEXT("The FilterGraph is not yet built."));
  178.                 }
  179.                 HMENU hMenu = GetSubMenu (GetMenu (hwnd), 1);
  180.                 EnableMenuItem (hMenu, IDM_SELECT_CHANNEL, MF_BYCOMMAND | MF_GRAYED);
  181.                 break;
  182.             }
  183.             
  184.             case IDM_SELECT_CHANNEL:
  185.             {
  186.                 if (!g_pfg)
  187.                     break;
  188.                     
  189.                 if(g_pfg->m_fGraphBuilt)
  190.                 {
  191.                     g_hwndDlg = reinterpret_cast <HWND> ( DialogBox(
  192.                         hInst, 
  193.                         MAKEINTRESOURCE(IDD_SELECT_CHANNEL),
  194.                         hwnd, 
  195.                         reinterpret_cast<DLGPROC>(SelectChannelDlgProc)
  196.                         ) );
  197.                 }
  198.                 else
  199.                 {
  200.                     ErrorMessageBox(TEXT("The FilterGraph is not yet built."));
  201.                 }
  202.                 break;
  203.             }
  204.             
  205.             case IDM_ABOUT:
  206.             {
  207.                 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, 
  208.                           reinterpret_cast<DLGPROC>(AboutDlgProc));
  209.                 break;
  210.             }
  211.             
  212.             case IDM_EXIT:
  213.             {
  214.                 PostMessage(hwnd, WM_CLOSE, 0, 0);
  215.                 break;
  216.             }
  217.             
  218.         default:
  219.             break;
  220.         }
  221.     case WM_INITMENU:
  222.         if (!g_pfg)
  223.             break;
  224.                     
  225.         if(g_pfg->m_fGraphFailure)
  226.         {
  227.             EnableMenuItem((HMENU)wParam, IDM_BUILD_ATSC, MF_BYCOMMAND| MF_GRAYED);
  228.             EnableMenuItem((HMENU)wParam, IDM_RUN_GRAPH, MF_BYCOMMAND| MF_GRAYED);
  229.             EnableMenuItem((HMENU)wParam, IDM_STOP_GRAPH, MF_BYCOMMAND| MF_GRAYED);
  230.         }
  231.         else
  232.         {
  233.             EnableMenuItem((HMENU)wParam, IDM_RUN_GRAPH, 
  234.                 g_pfg->m_fGraphRunning ? MF_BYCOMMAND|MF_GRAYED : MF_BYCOMMAND|MF_ENABLED);
  235.             EnableMenuItem((HMENU)wParam, IDM_BUILD_ATSC, 
  236.                 g_pfg->m_fGraphBuilt ? MF_BYCOMMAND|MF_GRAYED : MF_BYCOMMAND|MF_ENABLED);
  237.         
  238.             // we can stop viewing if it's currently viewing
  239.             EnableMenuItem((HMENU)wParam, IDM_STOP_GRAPH, 
  240.                 (g_pfg->m_fGraphRunning) ? MF_BYCOMMAND|MF_ENABLED : MF_BYCOMMAND|MF_GRAYED);
  241.         }
  242.         EnableMenuItem((HMENU)wParam, IDM_SELECT_CHANNEL, 
  243.             g_pfg->m_fGraphBuilt ? MF_BYCOMMAND|MF_ENABLED : MF_BYCOMMAND|MF_GRAYED);
  244.         break;
  245.     case WM_CLOSE:
  246.         DbgTerminate();
  247.         PostQuitMessage(0);
  248.         break;
  249.     default:
  250.         break;
  251.     }
  252.     return DefWindowProc(hwnd, message, wParam, lParam);
  253. }
  254. // SelectChannelDlgProc
  255. // Dialog Procedure for the "Select Channel" dialog box.
  256. //                                                                              
  257. BOOL CALLBACK
  258. SelectChannelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  259. {
  260.     LONG    lChannelMaj, lChannelMin, lChannelPhysical;
  261.     BOOL    bRet = FALSE;
  262.     
  263.     if (!g_pfg)
  264.         return FALSE;;    
  265.     lChannelMaj       = g_pfg->GetMajorChannel ();
  266.     lChannelMin       = g_pfg->GetMinorChannel ();
  267.     lChannelPhysical  = g_pfg->GetPhysicalChannel ();
  268.     switch(message)
  269.     {
  270.     case WM_INITDIALOG:
  271.         {
  272.             //refresh the controls
  273.             SetDlgItemInt (hDlg, IDC_MAJOR_CHANNEL, lChannelMaj, TRUE);
  274.             SetDlgItemInt (hDlg, IDC_MINOR_CHANNEL, lChannelMin, TRUE);
  275.             SetDlgItemInt (hDlg, IDC_PHYSICAL_CHANNEL, lChannelPhysical, TRUE);
  276.             //set the spin controls
  277.             HWND hWndSpin = GetDlgItem (hDlg, IDC_SPIN_MAJOR);
  278.             ::SendMessage(
  279.                 hWndSpin, 
  280.                 UDM_SETRANGE32, 
  281.                 static_cast <WPARAM> (MINOR_CHANNEL_LOWER_LIMIT),
  282.                 static_cast <LPARAM> (MAJOR_CHANNEL_UPPER_LIMIT)
  283.                 ); 
  284.             hWndSpin = GetDlgItem (hDlg, IDC_SPIN_MINOR);
  285.             ::SendMessage(
  286.                 hWndSpin, 
  287.                 UDM_SETRANGE32, 
  288.                 static_cast <WPARAM> (MINOR_CHANNEL_LOWER_LIMIT), 
  289.                 static_cast <LPARAM> (MINOR_CHANNEL_UPPER_LIMIT)
  290.                 ); 
  291.             hWndSpin = GetDlgItem (hDlg, IDC_SPIN_PHYSICAL);
  292.             ::SendMessage(
  293.                 hWndSpin, 
  294.                 UDM_SETRANGE32, 
  295.                 static_cast <WPARAM> (MINOR_CHANNEL_LOWER_LIMIT), 
  296.                 static_cast <LPARAM> (MINOR_CHANNEL_UPPER_LIMIT)
  297.                 );
  298.             break;
  299.         }
  300.     case WM_DESTROY:
  301.         {
  302.             EndDialog (hDlg, 0);
  303.             return TRUE;
  304.         }
  305.     case WM_COMMAND:
  306.         {
  307.             switch(LOWORD(wParam))
  308.             {
  309.             case IDOK:
  310.                 {
  311.                     lChannelMaj = (LONG) GetDlgItemInt(hDlg, IDC_MAJOR_CHANNEL, &bRet, TRUE);
  312.                     lChannelMin = (LONG) GetDlgItemInt(hDlg, IDC_MINOR_CHANNEL, &bRet, TRUE);
  313.                     lChannelPhysical = (LONG) GetDlgItemInt(hDlg, IDC_PHYSICAL_CHANNEL, &bRet, TRUE);
  314.                     g_pfg->ChangeChannel (lChannelPhysical, lChannelMaj, lChannelMin);
  315.                     EndDialog (hDlg, 0);
  316.                     break;
  317.                 }
  318.             case IDC_ENTER:
  319.                 {
  320.                     lChannelMaj = (LONG) GetDlgItemInt(hDlg, IDC_MAJOR_CHANNEL, &bRet, TRUE);
  321.                     lChannelMin = (LONG) GetDlgItemInt(hDlg, IDC_MINOR_CHANNEL, &bRet, TRUE);
  322.                     lChannelPhysical = (LONG) GetDlgItemInt(hDlg, IDC_PHYSICAL_CHANNEL, &bRet, TRUE);
  323.                     g_pfg->ChangeChannel (lChannelPhysical, lChannelMaj, lChannelMin);
  324.                     break;
  325.                 }
  326.             case IDCANCEL:
  327.                 {
  328.                     EndDialog (hDlg, 0);
  329.                     break;
  330.                 }
  331.             }
  332.             break;
  333.         }
  334.     }
  335.     return FALSE;
  336. }
  337. // AboutDlgProc
  338. //
  339. // Dialog Procedure for the "about" dialog box.
  340. //
  341. BOOL CALLBACK 
  342. AboutDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  343. {
  344.     switch(msg) 
  345.     {
  346.         case WM_COMMAND:
  347.             EndDialog(hwnd, TRUE);
  348.             return TRUE;
  349.     
  350.         case WM_INITDIALOG:
  351.             return TRUE;
  352.     }
  353.     return FALSE;
  354. }
  355. // ErrorMessageBox
  356. //
  357. // Opens a Message box with a error message in it.  The user can     
  358. // select the OK button to continue.
  359. //
  360. VOID
  361. ErrorMessageBox(LPTSTR szFormat, ...)
  362. {
  363.     static TCHAR szBuffer[2048];  // Large buffer
  364.     const size_t NUMCHARS = sizeof(szBuffer) / sizeof(szBuffer[0]);
  365.     const int LASTCHAR = NUMCHARS - 1;
  366.     // Format the input string
  367.     va_list pArgs;
  368.     va_start(pArgs, szFormat);
  369.     // Use a bounded buffer size to prevent buffer overruns.  Limit count to
  370.     // character size minus one to allow for a NULL terminating character.
  371.     _vsntprintf(szBuffer, NUMCHARS - 1, szFormat, pArgs);
  372.     va_end(pArgs);
  373.     // Ensure that the formatted string is NULL-terminated
  374.     szBuffer[LASTCHAR] = TEXT('');
  375.     // Display a message box with the formatted string
  376.     MessageBox(hwndMain, szBuffer, TEXT("Error!"), MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL);
  377. }