Search code examples
c++windowsvisual-studio-2013directshowborderless

Create borderless DirectShow Window


I am referring to this simple video player using DirectShow example provided here.

The code runs and builds however I need to make the video player window borderless.

I could not find a parameter to set in the CoCreateInstance function or the CoInitialize function which I can set to make the window borderless.

Here is my complete code for reference:

#include <dshow.h>
#pragma comment (lib, "strmiids.lib")
void main(void)
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;

    // Initialize the COM library.
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        printf("ERROR - Could not initialize COM library");
        return;
    }

    // Create the filter graph manager and query for interfaces.
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
        IID_IGraphBuilder, (void **)&pGraph);
    if (FAILED(hr))
    {
        printf("ERROR - Could not create the Filter Graph Manager.");
        return;
    }

    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

    // Build the graph. IMPORTANT: Change this string to a file on your system.
    hr = pGraph->RenderFile(L"D:\\dfs.avi", NULL);
    if (SUCCEEDED(hr))
    {
        // Run the graph.
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            // Wait for completion.
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);

            // Note: Do not use INFINITE in a real application, because it
            // can block indefinitely.
        }
    }
    pControl->Release();
    pEvent->Release();
    pGraph->Release();
    CoUninitialize();
}

What additional piece of code do I need to add in order to achieve this?


Solution

  • I found this code here which does the job!

    #pragma comment(lib, "strmiids.lib")
    #pragma comment(lib, "quartz.lib")
    
    #include <string>
    #include <dshow.h>
    
    
    int PlayVideo(const std::string& pFile)
    {
        IGraphBuilder* pGraph = NULL;
        IVideoWindow* pWin = NULL;
        IMediaControl* pControl = NULL;
        IMediaEvent* pEvent = NULL;
    
        // Initialize the COM library.
        HRESULT hr = CoInitialize(NULL);
        if (FAILED(hr)) return 1;
    
        // Create the filter graph manager and query for interfaces.
        hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
        if (FAILED(hr)) return 1;
    
        // Get interfaces
        hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
        hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
        hr = pGraph->QueryInterface(IID_IVideoWindow, (void **)&pWin);
    
        // Build the graph (convert unicode filename)
        size_t _size = mbstowcs(NULL,pFile.c_str(),2); // Add 2 for ZT
        wchar_t* _wfile = new wchar_t[_size + 2];
        _size = mbstowcs(_wfile, pFile.c_str(), pFile.length() + 1); // Add 1 for ZT
        hr = pGraph->RenderFile(_wfile, NULL);
        delete[] _wfile;
    
        // Uncomment next line for borderless window display
        pWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);
    
        // For fullscreen play, get Windows screen parameters and replace
        pWin->SetWindowPosition(0, 0, 800, 600);
    
        if (SUCCEEDED(hr))
        {
            hr = pControl->Run();
            if (SUCCEEDED(hr))
            {
                long evCode;
                pEvent->WaitForCompletion(INFINITE, &evCode);
            }
        }
    
        pControl->Release();
        pEvent->Release();
        pWin->Release();
        pGraph->Release();
        CoUninitialize();
    
        return 0;
    }
    
    
    int main()
    {
        PlayVideo("D:\\dfs.avi"); // play any format Windows can handle, avi/mpg etc.
        return 0;
    }