Regarding Desktop Video capture Using VC++

Discussion in 'C++' started by zenotriacme, Feb 18, 2011.

  1. zenotriacme

    zenotriacme New Member

    Joined:
    Feb 18, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    i have download the project from microsoft using WMEncoder to capture a desktop activities as video. Its working fine. But the problem is it capture a full screen but i need to capture the specific window. I am new to VC++ thats y i dono how to change the coding for mu purpose. Please help me to solve this issue....

    Thanks in Advance

    Code is

    Code:
    // WMEncScrnCap.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    #include "WMEncScrnCap.h"
    #include <shellapi.h>
    #include "wmencode.h"
    
    
    #include <Atlbase.h>
    #include <comdef.h>
    
    #define MAKEFOURCC(ch0, ch1, ch2, ch3)                              \
                    ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) |   \
                    ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 ))            //From MMSystem.h
    
    #define MAX_LOADSTRING 100
    
    #define    WM_NOTIFYICON_MESSAGE    WM_USER+1000
    
    #define CONTENT_TYPE_VIDEO_ONLY        16        //Profile Supports Only Video
    #define CONTENT_TYPE_AUDIO_VIDEO    17        //Profile Supports both audio and Video
    
    #define PROFILE_CONTENT_TYPE        CONTENT_TYPE_VIDEO_ONLY        //Change this to Audio_Video for Audio support
    
    #define PROFILE_AUDIENCE_BITRATE    400000    //Target for 400 Kbps
    
    #define VIDEOCODEC                    MAKEFOURCC('M','S','S','2')
    
    #define VIDEOFRAMERATE                30        //15 FPS(Frames Per Second)
    
    #define IMAGEQUALITY                100        // 0 ~ 100: The higher the number the better the quality
    
    #define CAPTURE_FULLSCREEN            false    //By Default We capture Full Screen - If false set the coordinates
    
    #define    WMSRCNCAP_CAPTUREWINDOW        CComBSTR("CaptureWindow")    //Property of Source Video - Fullscreen Mode
    
    #define WMSCRNCAP_WINDOWLEFT        CComBSTR("Left")
    #define WMSCRNCAP_WINDOWTOP         CComBSTR("Top")
    #define WMSCRNCAP_WINDOWRIGHT       CComBSTR("Right")
    #define WMSCRNCAP_WINDOWBOTTOM      CComBSTR("Bottom")
    #define WMSCRNCAP_FLASHRECT         CComBSTR("FlashRect")
    #define WMSCRNCAP_ENTIRESCREEN      CComBSTR("Screen")
    #define WMSCRNCAP_WINDOWTITLE       CComBSTR("WindowTitle")
    
    //#define WMSCRNCAP_WINDOWLEFT        CComBSTR("150")
    //#define WMSCRNCAP_WINDOWTOP         CComBSTR("100")
    //#define WMSCRNCAP_WINDOWRIGHT       CComBSTR("150")
    //#define WMSCRNCAP_WINDOWBOTTOM      CComBSTR("100")
    //#define WMSCRNCAP_FLASHRECT         CComBSTR("FlashRect")
    //#define WMSCRNCAP_ENTIRESCREEN      CComBSTR("Screen")
    //#define WMSCRNCAP_WINDOWTITLE       CComBSTR("WindowTitle")
    
    
    #define ErrorMessage(x)    MessageBox(NULL,x,"Error",MB_OK|MB_ICONERROR)
    
    
    HINSTANCE    hInst;                                
    TCHAR        szTitle[MAX_LOADSTRING];            
    TCHAR        szWindowClass[MAX_LOADSTRING];        
    HMENU        ghMenu=NULL;                                //Menu Handle for the Popup Menu
    
    IWMEncoder2*        g_pEncoder=NULL;    
    IWMEncProfile2*        g_pProfile=NULL;
    
    HRESULT                SetupScreenCaptureProfile();        //Creates and Initializes the Custom Profile Settings for ScreenCapture Codec
    void                Cleanup();                            //The Exit Function for the Encoder Objects
    HRESULT                InitEncoder(LPCTSTR);                //The Entry Function for the Encoder Objects
    
    ATOM                MyRegisterClass(HINSTANCE hInstance);
    BOOL                InitInstance(HINSTANCE, int);
    LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
        MSG msg;
        HACCEL hAccelTable;
    
        CoInitialize(NULL);                    //Initialize COM
    
        LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
        LoadString(hInstance, IDC_WMENCSCRNCAP, szWindowClass, MAX_LOADSTRING);
        MyRegisterClass(hInstance);
    
        if (!InitInstance (hInstance, nCmdShow)) 
        {
            return FALSE;
        }
    
        hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WMENCSCRNCAP);
    
        while (GetMessage(&msg, NULL, 0, 0)) 
        {
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    
        CoUninitialize();                    //Cleanup COM
    
        return (int) msg.wParam;
    }
    
    
    
    //
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    //  COMMENTS:
    //
    //    This function and its usage are only necessary if you want this code
    //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
    //    function that was added to Windows 95. It is important to call this function
    //    so that the application will get 'well formed' small icons associated
    //    with it.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
        WNDCLASSEX wcex;
    
        wcex.cbSize = sizeof(WNDCLASSEX); 
    
        wcex.style            = CS_HREDRAW | CS_VREDRAW ;
        wcex.lpfnWndProc    = (WNDPROC)WndProc;
        wcex.cbClsExtra        = 0;
        wcex.cbWndExtra        = 0;
        wcex.hInstance        = hInstance;
        wcex.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_WMENCSCRNCAP);
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground    = (HBRUSH)(BLACK_BRUSH);
        wcex.lpszMenuName    = (LPCTSTR)IDC_WMENCSCRNCAP;
        wcex.lpszClassName    = szWindowClass;
        wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
    
        return RegisterClassEx(&wcex);
    }
    
    //
    //   FUNCTION: InitInstance(HANDLE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
    
       hInst = hInstance; // Store instance handle in our global variable
    
       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, CW_USEDEFAULT, 480, 320, NULL, NULL, hInstance, NULL);
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    
       return TRUE;
    }
    
    //
    //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
    //
    //  PURPOSE:  Processes messages for the main window.
    //
    //  WM_COMMAND    - process the application menu
    //  WM_PAINT    - Paint the main window
    //  WM_DESTROY    - post a quit message and return
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        static NOTIFYICONDATA    nid;
        static bool    bMinimized=false;
        static RECT    clientRect = {0,0,0,0};
        DWORD    wmId,wmEvent;
        switch (message) 
        {
        case WM_SYSCOMMAND:
            {
                if(g_pEncoder)
                {
                    if(wParam==SC_CLOSE)
                    {
                            SendMessage(hWnd,WM_SYSCOMMAND,SC_MINIMIZE,0);                        //Give the effect of minimize
                            return 0;
                    }            
                    if(wParam==SC_MINIMIZE)
                    {
                        bMinimized=true;
                        LONG_PTR ret=DefWindowProc(hWnd,message,wParam,lParam);                //Run the Minimize Animation
                        SendMessage(hWnd,WM_COMMAND,MAKEWPARAM(ID_FILE_HIDEWINDOW,0),0);    //Then Hide it
                        return ret;
                    }
                }
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        case WM_COMMAND:
            {
                wmId    = LOWORD(wParam); 
                wmEvent = HIWORD(wParam); 
                switch (wmId)
                {
                case IDM_ABOUT<b></b>:
                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
                    break;
                case ID_FILE_STARTCAPTURE:
                case ID_CAPTURE_START:
                    {
                        if(g_pEncoder==NULL)
                        {
                            OPENFILENAME    ofn;
                            char    szFileName[512];
                            strcpy(szFileName,"Output.wmv");
                            ZeroMemory(&ofn,sizeof(ofn));
                            ofn.lStructSize=sizeof(OPENFILENAME);
                            ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST;
                            ofn.lpstrFilter="Video Files (*.wmv)\0*.wmv\0 ";
                            //ofn.lpstrFilter = "All Files (*.*)|*.*";
                            ofn.lpstrDefExt="wmv";    
                            
                            ofn.lpstrFile=szFileName;
                            ofn.nMaxFile=512;
                            if(!GetSaveFileName(&ofn))    break;
                        
                            SendMessage(hWnd,WM_SYSCOMMAND,SC_MINIMIZE,0);
                            ShowWindow(hWnd,SW_HIDE);
    
                            if(FAILED(InitEncoder(szFileName)))    
                            {
                                DestroyWindow(hWnd);
                                //ShowWindow(hWnd,SW_RESTORE);
                                break;
                            }
                        }
                    
                        nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;            //Set the Notification Message
                        nid.uCallbackMessage=WM_NOTIFYICON_MESSAGE;
                        nid.hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON_PAUSE));
                        strcpy(nid.szTip,"Capturing the Screen - Click to Pause");
    
                        if(!Shell_NotifyIcon(NIM_MODIFY,&nid))                //Modify the Icon State
                            MessageBox(NULL,"Unable to Set Notification Icon","Error",MB_ICONINFORMATION|MB_OK);
    
                        SendMessage(hWnd,WM_SYSCOMMAND,SC_MINIMIZE,0);
    
                        if(FAILED(g_pEncoder->Start()))
                            ErrorMessage("Unable to Start Encoding Process");                    
    
                        break;
                    }
                case ID_FILE_PAUSECAPTURE:
                case ID_CAPTURE_PAUSE:
                    {                
                        nid.hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON_START));
                        strcpy(nid.szTip,"Screen Capture Paused - Click to Resume");
    
                        if(!Shell_NotifyIcon(NIM_MODIFY,&nid))                //Modify the Icon State
                            MessageBox(NULL,"Unable to Set Notification Icon","Error",MB_ICONINFORMATION|MB_OK);
    
                        if(FAILED(g_pEncoder->Pause()))
                        {
                            ErrorMessage("Unable to Pause the Encoder");
                        }
                        InvalidateRect(hWnd,NULL,false);
                        break;
                    }
                case ID_FILE_SHOWWINDOW:
                    {
                        if(bMinimized)    ShowWindow(hWnd,SW_RESTORE);
                        else            ShowWindow(hWnd,SW_SHOW);
                        SetForegroundWindow(hWnd);
                        bMinimized=false;
                        break;
                    }
                case ID_FILE_HIDEWINDOW:
                    {
                        ShowWindow(hWnd,SW_HIDE);
                        break;
                    }
                case IDM_EXIT:
                    DestroyWindow(hWnd);
                    break;
                default:
                    return DefWindowProc(hWnd, message, wParam, lParam);
                }
                break;
            }
        case WM_CREATE:
            {
                ZeroMemory(&nid,sizeof(nid));
                nid.cbSize=sizeof(nid);
                nid.uID=1000;
                nid.uFlags=NIF_ICON|NIF_TIP;
                nid.hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_WMENCSCRNCAP));
                nid.hWnd=hWnd;
                strcpy(nid.szTip,"Screen Capture Application");
                if(!Shell_NotifyIcon(NIM_ADD,&nid))    MessageBox(NULL,"Unable to Set Notification Icon","Error",MB_ICONINFORMATION|MB_OK);
                ghMenu=LoadMenu(hInst,MAKEINTRESOURCE(IDC_WMENCSCRNCAP));
                break;
            }
        case WM_CHAR:
            {
                if(wParam==VK_ESCAPE)
                    DestroyWindow(hWnd);    //DestroyWindow calls cleanup() - it would stop the encoder
                break;
            }
        case WM_INITMENU:
            {
                HMENU    hMenu = (HMENU)wParam;
                WMENC_ENCODER_STATE    encState=WMENC_ENCODER_STOPPED ;
                if(g_pEncoder)    g_pEncoder->get_RunState(&encState);
                bool bCapturing = (encState!=WMENC_ENCODER_PAUSED && encState!=WMENC_ENCODER_STOPPED);
                EnableMenuItem(hMenu,ID_FILE_SHOWWINDOW,IsWindowVisible(hWnd)?MF_GRAYED:MF_ENABLED|MF_BYCOMMAND);
                if(g_pEncoder)    EnableMenuItem(hMenu,ID_FILE_HIDEWINDOW,IsWindowVisible(hWnd)?MF_ENABLED:MF_GRAYED|MF_BYCOMMAND);
                EnableMenuItem(hMenu,ID_FILE_STARTCAPTURE,bCapturing?MF_GRAYED|MF_BYCOMMAND:MF_ENABLED);
                EnableMenuItem(hMenu,ID_FILE_PAUSECAPTURE,bCapturing?MF_ENABLED:MF_GRAYED|MF_BYCOMMAND);
                EnableMenuItem(hMenu,ID_CAPTURE_START,bCapturing?MF_GRAYED|MF_BYCOMMAND:MF_ENABLED);
                EnableMenuItem(hMenu,ID_CAPTURE_PAUSE,bCapturing?MF_ENABLED:MF_GRAYED|MF_BYCOMMAND);            
                break;
            }
        case WM_INITMENUPOPUP:
            {
                WMENC_ENCODER_STATE    encState=WMENC_ENCODER_STOPPED ;
                if(g_pEncoder)    g_pEncoder->get_RunState(&encState);
                bool bCapturing = (encState!=WMENC_ENCODER_PAUSED && encState!=WMENC_ENCODER_STOPPED);
                EnableMenuItem(ghMenu,ID_FILE_SHOWWINDOW,IsWindowVisible(hWnd)?MF_GRAYED:MF_ENABLED|MF_BYCOMMAND);
                EnableMenuItem(ghMenu,ID_FILE_HIDEWINDOW,IsWindowVisible(hWnd)?MF_ENABLED:MF_GRAYED|MF_BYCOMMAND);
                EnableMenuItem(ghMenu,ID_FILE_STARTCAPTURE,bCapturing?MF_GRAYED|MF_BYCOMMAND:MF_ENABLED);
                EnableMenuItem(ghMenu,ID_FILE_PAUSECAPTURE,bCapturing?MF_ENABLED:MF_GRAYED|MF_BYCOMMAND);
                EnableMenuItem(ghMenu,ID_CAPTURE_START,bCapturing?MF_GRAYED|MF_BYCOMMAND:MF_ENABLED);
                EnableMenuItem(ghMenu,ID_CAPTURE_PAUSE,bCapturing?MF_ENABLED:MF_GRAYED|MF_BYCOMMAND);            
                break;
            }
        case WM_PAINT:
            {
                WMENC_ENCODER_STATE    encState=WMENC_ENCODER_STOPPED ;
                if(g_pEncoder)    g_pEncoder->get_RunState(&encState);
                bool bCapturing = (encState!=WMENC_ENCODER_PAUSED && encState!=WMENC_ENCODER_STOPPED);
                const char* strMsg[]={"Click Start From Capture Menu to Start/Resume the Screen Capture","Click Pause From Capture Menu to Pause the Screen Capture"};
                PAINTSTRUCT ps;
                HDC hdc=BeginPaint(hWnd,&ps);    
                DrawText(hdc,strMsg[bCapturing],(int)strlen(strMsg[bCapturing]),&clientRect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
                EndPaint(hWnd,&ps);
                break;
            }
        case WM_SIZE:
            {
                clientRect.right = LOWORD(lParam);
                clientRect.bottom= HIWORD(lParam);
                break;
            }
        case WM_NOTIFYICON_MESSAGE:
            {
                switch(lParam)
                {
                    case WM_MOUSEMOVE:break;
                    case WM_LBUTTONDOWN:
                        {                        
                            WMENC_ENCODER_STATE    encState=WMENC_ENCODER_STOPPED;    //By Default Assume Stopped state
                            g_pEncoder->get_RunState(&encState);
                            if(encState == WMENC_ENCODER_RUNNING)
                                SendMessage(hWnd,WM_COMMAND,MAKEWPARAM(ID_FILE_PAUSECAPTURE,0),0);
                            if(encState == WMENC_ENCODER_PAUSED || encState == WMENC_ENCODER_STOPPED)
                                SendMessage(hWnd,WM_COMMAND,MAKEWPARAM(ID_FILE_STARTCAPTURE,0),0);
                            break;
                        }
                    case WM_RBUTTONDOWN:
                        {
                            POINT pt;GetCursorPos(&pt);
                            SetForegroundWindow(hWnd);
                            TrackPopupMenu(GetSubMenu(ghMenu,0),TPM_LEFTALIGN|TPM_BOTTOMALIGN,pt.x,pt.y,0,hWnd,NULL);
                            break;
                        }
                }
                break;
            }
        case WM_DESTROY:
            {
                Shell_NotifyIcon(NIM_DELETE,&nid);
                DestroyMenu(ghMenu);
                Cleanup();
                PostQuitMessage(0);
                break;
            }
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }
    
    LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
        case WM_INITDIALOG:
            return TRUE;
    
        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
            {
                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;
        }
        return FALSE;
    }
    
    
    HRESULT    SetupScreenCaptureProfile()                                        //Called by InitEncoder() Function
    {    
        IWMEncAudienceObj*    pAudience=NULL;
    
        if(FAILED(CoCreateInstance(CLSID_WMEncProfile2,NULL,CLSCTX_INPROC_SERVER,IID_IWMEncProfile2,(void**)&g_pProfile)))
        {
            ErrorMessage("Unable to Create Profile Manager");
            return E_FAIL;
        }
        if(FAILED(g_pProfile->put_ValidateMode(true)))                        //Verify the settings immediately as they are set
        {
            ErrorMessage("Unable to Set Validate Mode");
            return E_FAIL;
        }
        if(FAILED(g_pProfile->put_ProfileName(CComBSTR("Custom Screen Capture Profile"))))
        {
            ErrorMessage("Unable to Set Profile Name");
            return E_FAIL;
        }
        if(FAILED(g_pProfile->put_ProfileDescription(CComBSTR("A Custom Video Profile For Screen Capture"))))
        {
            ErrorMessage("Unable to Set Profile Description");
            return E_FAIL;
        }
        if(FAILED(g_pProfile->put_ContentType(PROFILE_CONTENT_TYPE)))        //Content Type for our Profile - By default it is - CONTENT_TYPE_VIDEO_ONLY
        {
            ErrorMessage("Unable to Set Content Type for Profile");
            return E_FAIL;
        }
        if(FAILED(g_pProfile->put_VBRMode(WMENC_VIDEO,0,WMENC_PVM_NONE)))    //Set CBR Mode - Compatible with Older Version Players
        {
            ErrorMessage("Unable to Set BitRate Mode");
            return E_FAIL;
        }
        if(FAILED(g_pProfile->AddAudience(PROFILE_AUDIENCE_BITRATE,&pAudience)))
        {
            ErrorMessage("Unable to Set Target Audience");
            return E_FAIL;
        }
    
        do
        {
            long lCodecIndex=-1;
            if(FAILED(g_pProfile->GetCodecIndexFromFourCC(WMENC_VIDEO,VIDEOCODEC,&lCodecIndex)))
            {
                ErrorMessage("Unable to Get Codec Index");
                break;
            }
            if(FAILED(pAudience->put_VideoCodec(0,lCodecIndex)))            //Set the Codec for the Target Audience
            {
                ErrorMessage("Unable to Set Codec");
                break;
            }
            if(FAILED(pAudience->put_VideoHeight(0,0)))                        //Use the same as the input
            {
                ErrorMessage("Unable to Set Video Height");
                break;
            }
            if(FAILED(pAudience->put_VideoWidth(0,0)))                        //Use the same width as the input
            {
                ErrorMessage("Unable to Set Video Width");
                break;
            }
            if(FAILED(pAudience->put_VideoBufferSize(0,5000)))                //Set buffer size for 5 seconds
            {
                ErrorMessage("Unable to Set Buffer Size");
                break;
            }
            if(FAILED(pAudience->put_VideoFPS(0,VIDEOFRAMERATE * 1000)))    //Set the Video Frame Rate
            {
                ErrorMessage("Unable to Set Video Frame Rate");
                break;
            }
            if(FAILED(pAudience->put_VideoImageSharpness(0,IMAGEQUALITY)))    //Set the Best Image Sharpness possible
            {
                ErrorMessage("Unable to Set Video Image Sharpness");
                break;
            }
            if(FAILED(g_pProfile->Validate()))                                //Test the Profile Settings
            {
                ErrorMessage("Unable to Validate the Profile Settings");
                break;
            }
            if(pAudience)
            {
                pAudience->Release();
                pAudience=NULL;
            }
            return S_OK;
    
        }while(false);
        
        if(pAudience)                        //Control Reaches here only in case of errors 
        {
            pAudience->Release();
            pAudience=NULL;
        }
        return E_FAIL;
    }
    
    HRESULT    InitEncoder(LPCTSTR szOutputFileName)
    {
        HRESULT    hr = E_FAIL;
        CComVariant varValue;
        IWMEncSourceGroupCollection*    pSrcGrpCollection=NULL;
        IWMEncSourceGroup*    pSrcGrp=NULL;
        IWMEncSource*    pSrc=NULL;
        IPropertyBag*    pPropertyBag=NULL;
        IWMEncVideoSource2*    pSrcVid=NULL;
        IWMEncFile*    pOutFile=NULL;
        IWMEncProfile*    pProfile=NULL;
    
        if(FAILED(CoCreateInstance(CLSID_WMEncoder,NULL,CLSCTX_INPROC_SERVER,IID_IWMEncoder2,(void**)&g_pEncoder)))
        {
            ErrorMessage("Unable to Create Encoder Object");
            return E_FAIL;
        }
        if(FAILED(g_pEncoder->get_SourceGroupCollection(&pSrcGrpCollection)))        //Retrieve the Source Group Collection    - One Application can Have many Source Groups - We need to add as many as we want
        {
            ErrorMessage("Unable to Get Source Group Collection");
            return E_FAIL;
        }
    
        do
        {
            if(FAILED(hr=pSrcGrpCollection->Add(CComBSTR("SourceGroup1"),&pSrcGrp)))//Add a Source Group to the Collection - Each Source can have one video one audio source input
            {
                ErrorMessage("Unable to Add A Source Group to the Collection");
                break;
            }
            if(FAILED(hr=pSrcGrp->AddSource(WMENC_VIDEO,&pSrc)))                    //Add a Video Source to the Group
            {
                ErrorMessage("Unable to Add A Source to the Source Group");
                break;
            }
            if(FAILED(hr=pSrc->QueryInterface(IID_IWMEncVideoSource2,(void**)&pSrcVid)))
            {
                ErrorMessage("Unable to Query interface for Video Source");
                break;
            }
            if(FAILED(hr=pSrcVid->SetInput(CComBSTR("ScreenCap://ScreenCapture1"))))//The Video Input Source Device - Should be "ScreenCap" Device
            {
                ErrorMessage("Unable to Set Video Input Source");
                break;
            }
            if(FAILED(hr=pSrcVid->QueryInterface(IID_IPropertyBag,(void**)&pPropertyBag)))
            {
                ErrorMessage("Unable to Query Interface for Propery bag");
                break;
            }
    
            varValue = CAPTURE_FULLSCREEN;
            if(FAILED(hr=pPropertyBag->Write(WMSCRNCAP_ENTIRESCREEN,&varValue)))    //Set Full Screen Property true/false
            {
                ErrorMessage("Unable to Set Capture Screen Property");
                break;
            }
            //int nLeft, nRight, nTop, nBottom;                                    //Set Capture Area - when not in full screen mode
            //                                                                    // Initialize the capture area. The size must be even.
            //    varValue = false;
            //    if ( SUCCEEDED( hr ) )
            //    {
            //        hr = pPropertyBag->Write( WMSCRNCAP_ENTIRESCREEN, &varValue );
            //    }
            //    varValue = nLeft;
            //    if ( SUCCEEDED( hr ) )
            //    {
            //        hr = pPropertyBag->Write( WMSCRNCAP_WINDOWLEFT, &varValue );
            //    }
            //    varValue = nRight;
            //    if ( SUCCEEDED( hr ) )
            //    {
            //        hr = pPropertyBag->Write( WMSCRNCAP_WINDOWRIGHT, &varValue );
            //    }
            //    varValue = nTop;
            //    if ( SUCCEEDED( hr ) )
            //    {
            //        hr = pPropertyBag->Write( WMSCRNCAP_WINDOWTOP, &varValue );
            //    }
            //    varValue = nBottom;
            //    if ( SUCCEEDED( hr ) )
            //    {
            //        hr = pPropertyBag->Write( WMSCRNCAP_WINDOWBOTTOM, &varValue );
            //    }
            //    varValue = true;
            //    if ( SUCCEEDED( hr ) )
            //    {
            //        hr = pPropertyBag->Write( WMSCRNCAP_FLASHRECT, &varValue );
            //    }
    
            if(FAILED(hr=SetupScreenCaptureProfile()))                                    //Setup the Custom Profile
            {
                break;
            }
            if(FAILED(hr=g_pProfile->QueryInterface(IID_IWMEncProfile,(void**)&pProfile)))
            {
                ErrorMessage("Unable to Query Interface For Profile");
                break;
            }
            if(FAILED(hr=pSrcGrp->put_Profile(variant_t(pProfile))))                    //Select the Custom Profile into the Encoder    
            {
                ErrorMessage("Unable to Set Profile For Source Group");
                break;
            }
            if(FAILED(hr=g_pEncoder->get_File(&pOutFile)))
            {
                ErrorMessage("Unable to Get Encoder Output File Object");
                break;
            }
            if(FAILED(hr=pOutFile->put_LocalFileName(CComBSTR(szOutputFileName))))        //Set the Target Output Filename
            {
                ErrorMessage("Unable to Set Output File Name");
                break;
            }
            if(FAILED(hr=g_pEncoder->PrepareToEncode(VARIANT_TRUE)))                    //Using Prepare optimizes startig latency
            {
                ErrorMessage("Unable to Prepare for Encoding");
                break;
            }
        }while(false);
    
        if(pProfile)
        {
            pProfile->Release();
            pProfile=NULL;
        }
        if(pOutFile)
        {
            pOutFile->Release();
            pOutFile = NULL;
        }
        if(pPropertyBag)
        {
            pPropertyBag->Release();
            pPropertyBag = NULL;
        }
        if(pSrcVid)
        {
            pSrcVid->Release();
            pSrcVid = NULL;
        }
        if(pSrc)
        {
            pSrc->Release();
            pSrc = NULL;
        }
        if(pSrcGrp)
        {
            pSrcGrp->Release();
            pSrcGrp = NULL;
        }
        if(pSrcGrpCollection)
        {
            pSrcGrpCollection->Release();
            pSrcGrpCollection = NULL;
        }
        return hr;
    }
    
    void Cleanup()
    {
        if(g_pProfile)
        {
            g_pProfile->Release();
            g_pProfile =NULL;
        }
        if(g_pEncoder)
        {
            g_pEncoder->PrepareToEncode(VARIANT_FALSE);        //Prepare to Stop
            g_pEncoder->Stop();
            g_pEncoder->Release();
            g_pEncoder=NULL;
        }
    }
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I haven't checked through your code but are you capturing a window by using its window handle? If so then you can capture the full screen simply by capturing from HWND 0. Zero is the HWND of the entire desktop.
     
  3. zenotriacme

    zenotriacme New Member

    Joined:
    Feb 18, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I cannt understand you. Please kindly tell me the thing clearly. And Hwnd is used to create a window but i want to capture particular window....

     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice