Re: How to handle WM_SIZE message in Doc/View architecture???

From: Awin (Awin_at_discussions.microsoft.com)
Date: 01/11/05


Date: Tue, 11 Jan 2005 02:59:03 -0800

Dear Iain and Thore Karlsen,
        Thanks for your reply, I post my code below, maybe I do something
wrong.
I declare below member varible to CView:
        IGraphBuilder* m_pGraphBuilder;
        IMediaControl* m_pMediaControl;
        IBaseFilter* m_pSourceFilter;
        IVMRWindowlessControl* m_pVMRWindowlessControl;
        ICaptureGraphBuilder2* m_pCaptureGraphBuilder;
        IBaseFilter* m_pVmr;
and one function member : EnumSystemDevice().
and do all initial steps in CView::OnInitialUpdate function, below are the
codes:
        //In CView::OnInitialUpdate()
        CView::OnInitialUpdate();
        CoInitialize(NULL);
        HRESULT hr = CoCreateInstance(CLSID_FilterGraph,
                                                        NULL,
                                                        CLSCTX_INPROC_SERVER,
                                                        IID_IGraphBuilder,
                                                        (void
**)&m_pGraphBuilder);

        //Query m_pGraphBuilder interface IID_IMediaControl
        m_pGraphBuilder->QueryInterface(IID_IMediaControl,
(void**)&m_pMediaControl);

        //Create IID_ICaptureGraphBuilder2
        hr = CoCreateInstance(CLSID_CaptureGraphBuilder2,
                                          NULL,
                                          CLSCTX_INPROC_SERVER,
                                          IID_ICaptureGraphBuilder2,
                                          (void**)&m_pCaptureGraphBuilder);

        //Specifies a filter graph for the capture graph builder
        m_pCaptureGraphBuilder->SetFiltergraph(m_pGraphBuilder);

        //Enumarate all webcam in system, and bind it to m_SourceFilter,
        //then add m_SourceFilter to m_pGraphBuilder
        //EnumSystemDevice() is a helper function to find the first webcam,
        //and add it to FilterGraph
        hr = EnumSystemDevice();

        //Create VMR
        hr = CoCreateInstance(CLSID_VideoMixingRenderer,
                                          NULL,
                                          CLSCTX_INPROC,
                                          IID_IBaseFilter,
                                          (void**)&m_pVmr);

        if (SUCCEEDED(hr))
                hr = m_pGraphBuilder->AddFilter(m_pVmr, L"Video Mixing
Renderer");
        else
                m_pVmr->Release();

        IVMRFilterConfig* pConfig;
        hr = m_pVmr->QueryInterface(IID_IVMRFilterConfig, (void**)&pConfig);
        if (SUCCEEDED(hr))
        {
                hr = pConfig->SetRenderingMode(VMRMode_Windowless);
                pConfig->Release();
        }
        if (SUCCEEDED(hr))
        {
                // Set the window.
                hr = m_pVmr->QueryInterface(IID_IVMRWindowlessControl,
                                                             
(void**)&m_pVMRWindowlessControl);
                if( SUCCEEDED(hr))
                {
                        hr =
m_pVMRWindowlessControl->SetVideoClippingWindow(this->m_hWnd);
                        if (SUCCEEDED(hr))
                        {
                                //AfxMessageBox("SetVideoClippingWindow
Succeeded.");
                        }
                        else
                        {
                                AfxMessageBox("SetVideoClippingWindow
Failed.");
                                // An error occurred, so release the
interface.
                                m_pVMRWindowlessControl->Release();
                        }
                }
        }

        hr = m_pCaptureGraphBuilder->RenderStream(&PIN_CATEGORY_PREVIEW,
                                                                             
&MEDIATYPE_Video,
                                                                             
m_pSourceFilter,
                                                                             
NULL, m_pVmr);

        long lWidth, lHeight;
        hr = m_pVMRWindowlessControl->GetNativeVideoSize(&lWidth, &lHeight,
                                                                             
           NULL, NULL);

        if (SUCCEEDED(hr))
        {
                RECT rcSrc, rcDest;
                // Set the source rectangle.
               ::SetRect(&rcSrc, 0, 0, lWidth, lHeight);
    
                // Get the window client area.
                ::GetClientRect(this->m_hWnd, &rcDest);

                // Set the video position.
                hr = m_pVMRWindowlessControl->SetVideoPosition(&rcSrc,
&rcDest);
        }

        if (SUCCEEDED(hr))
        {
                // Build the graph. For example:
                m_pMediaControl->Run();
        }

and in CView::OnSize() function:
        CView::OnSize(nType, cx, cy);
        long lWidth, lHeight;
        HRESULT hr = m_pVMRWindowlessControl->GetNativeVideoSize(&lWidth,
&lHeight, NULL, NULL);
        if (SUCCEEDED(hr))
        {
                RECT rcSrc, rcDest;
                // Set the source rectangle.
                ::SetRect(&rcSrc, 0, 0, 320, 240);
    
                // Get the window client area.
                ::GetClientRect(this->m_hWnd, &rcDest);
    
                // Set the video position.
                if (m_pVMRWindowlessControl)
                {
                        hr = m_pVMRWindowlessControl->SetVideoPosition(&rcSrc,
                                                                             
                       &rcDest);
                }
        }

and the helper function CView::EnumSystemDevice()
        HRESULT CCamViewView::EnumSystemDevice()
        {
                ICreateDevEnum *pSysDevEnum = NULL;
                HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,
                                                                NULL,
                                                                
CLSCTX_INPROC_SERVER,
                                                                
IID_ICreateDevEnum,
                                                                (void
**)(&pSysDevEnum));
                if (FAILED(hr))
                {
                        AfxMessageBox("Create CLSID_SystemDeviceEnum failed");
                        return hr;
                }

                // Obtain a class enumerator for the video compressor
category.
                IEnumMoniker *pEnumCat = NULL;
                hr=pSysDevEnum->CreateClassEnumerator
                                                                    
(CLSID_VideoInputDeviceCategory,
                                                                     
&pEnumCat, 0);
                if (hr == S_OK)
                {
                        // Enumerate the monikers.
                        IMoniker *pMoniker = NULL;
                        ULONG cFetched;
                        while(pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK)
                        {
                                IPropertyBag *pPropBag;
                                hr = pMoniker->BindToStorage(0, 0,
                                                                             
 IID_IPropertyBag,
                                                                             
 (void**)&pPropBag);
                                if (SUCCEEDED(hr))
                                {
                                        // To retrieve the filter's friendly
name, do the following:
                                        VARIANT varName;
                                        VariantInit(&varName);
                                        hr = pPropBag->Read(L"FriendlyName",
&varName, 0);
                                        if (SUCCEEDED(hr))
                                        {
                                                // Display the name in your
UI somehow.
                                                TCHAR webcamName[2048];
                                                
WideCharToMultiByte(CP_ACP,0,varName.bstrVal, -1, webcamName, 2048, NULL,
NULL);
                                                
//m_WebcamList.AddString(webcamName);
                                        }
                                        VariantClear(&varName);
                                        // To create an instance of the
filter, do the following:
                                        hr = pMoniker->BindToObject(NULL,
NULL,
                                                                             
       ID_IBaseFilter,
                                                                             
       (void**)&m_pSourceFilter);
                                        // Now add the filter to the graph.
                                        
m_pGraphBuilder->AddFilter(m_pSourceFilter, L"Webcam Capture");
                                        //Remember to release pFilter later.
                                        pPropBag->Release();
                                }
                                pMoniker->Release();
                        }
                        pEnumCat->Release();
                }
                else
                        AfxMessageBox("Enumratoe system device failed");

                pSysDevEnum->Release();
                return hr;
        }

These three functions are all I add in my project, to avoid unexpected
release, I remove all release call in my project.
You could add these three functions to your own project to see the error
message.
Thanks again!!!



Relevant Pages

  • Re: Multiple variables
    ... You declare a variable called @codes, presumably all of my old instances of ... @code should now read @codes instead. ... >>script that does all the deletions for each code at once, ... > that only you can execute, you don't have to worry about SQL Injection ...
    (microsoft.public.sqlserver.mseq)
  • Re: which quotation marks to use
    ... which does no harm because the code points where they differ are not ... used for anything meaningful in either ISO 8859-1 or Unicode. ... it is advisable to declare ... non-propriertary codes which then appear in the declarations of HTML ...
    (comp.infosystems.www.authoring.html)
  • Re: Style suggestions?
    ... >> defconst, declaim, declare, values, defsubst ... ... the Forth codes shows more arithmetic operators and less bizarre ...
    (comp.lang.forth)
  • How to make a Case Function.
    ... i'm working on some codes and i need your help. ... data first before transferring them into another sheet. ... what variables to declare, where to take reference ...
    (microsoft.public.excel.programming)
  • thread talking to CView
    ... I would like to create a thread to collect data and display them in CView. ... Maybe window is not created, ... where I can put those codes? ...
    (microsoft.public.vc.mfc)