提供操作 GRF 檔的方法(cpp&delphi)

DirectX, MCI Control, Video, Audio Control 等程式操作及技巧
回覆文章
頭像
tim
文章: 1379
註冊時間: 2008年 11月 26日, 00:49

提供操作 GRF 檔的方法(cpp&delphi)

文章 tim »

http://www.progdigy.com/boards/index.ph ... &f=1&t=266
http://support.microsoft.com/default.as ... US;Q313844

c++:

代碼: 選擇全部

HRESULT LoadGraphFile(IGraphBuilder *pGraph, const WCHAR* wszName)
{
   IStorage *pStorage = 0;

   if (S_OK != StgIsStorageFile(wszName))
   {
       return E_FAIL;
   }

   HRESULT hr = StgOpenStorage(wszName, 0,
       STGM_TRANSACTED | STGM_READ | STGM_SHARE_DENY_WRITE, 0, 0, &pStorage);

   if (FAILED(hr))
   {
       return hr;
   }

   IPersistStream *pPersistStream = 0;
   hr = pGraph->QueryInterface(IID_IPersistStream, reinterpret_cast<void**>(&pPersistStream));

   if (SUCCEEDED(hr))
   {
       IStream *pStream = 0;
       pStorage->OpenStream(L"ActiveMovieGraph", 0,
           STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStream);

       if(SUCCEEDED(hr))
       {
           hr = pPersistStream->Load(pStream);
           pStream->Release();
       }
       pPersistStream->Release();
   }
   pStorage->Release();
   return hr;
}
delphi:

1. The routine to load GRF-file in Delphi:

代碼: 選擇全部

function LoadGraphFile( pGraph : IGraphBuilder; wszName : WideString ) : HRESULT;
var
  pStorage : IStorage;
  pPersistStream : IPersistStream;
  pStream : IStream;

  hr : HRESULT;
begin
    //
    hr := E_FAIL;

    pStorage := nil;
    pPersistStream := nil;
    pStream := nil;
    try
       hr := StgIsStorageFile( PWideChar( wszName ) );
       if hr <> S_OK then Abort;

       hr := StgOpenStorage(
                            PWideChar( wszName ),
                            nil,
                            STGM_TRANSACTED or STGM_READ or STGM_SHARE_DENY_WRITE,
                            0,
                            0,
                            pStorage
                            );
       if hr <> S_OK then Abort;

       hr := pGraph.QueryInterface(
                                   IID_IPersistStream,
                                   pPersistStream
                                   );

       if hr <> S_OK then Abort;

       pStorage.OpenStream(
                           'ActiveMovieGraph',
                           0,
                           STGM_READ or STGM_SHARE_EXCLUSIVE,
                           0,
                           pStream
                           );
       if hr <> S_OK then Abort;

       hr := pPersistStream.Load( pStream );
       if hr <> S_OK then Abort;

    except
          on E : Exception do
             ShowMessage( E.Message );
    end;
    pStorage := nil;
    pPersistStream := nil;
    pStream := nil;

    result := hr;
end;
2. PlayWin updated OpenMenuClick handler

代碼: 選擇全部

procedure TformPlayWin.OpenMenuClick(Sender: TObject);
var
  hr : HRESULT;
  Graph : IGraphBuilder;
  wsFileName : WideString;
begin
 Graph := nil;
 try
    if OpenDialog.Execute then
    begin
         if not FilterGraph.Active then FilterGraph.Active := true;
         FilterGraph.ClearGraph;

         // ...updated
         hr := FilterGraph.QueryInterface( IID_IGraphBuilder, Graph );
         if hr <> S_OK then raise Exception.Create( DirectX_GetLastError_MessageString( hr ) );
         wsFileName := OpenDialog.FileName;
         if UpperCase( ExtractFileExt( wsFileName )) = '.GRF'
         then hr := LoadGraphFile( Graph, wsFileName )
         else hr := Graph.RenderFile( PWideChar( wsFileName ), 0 );
         if hr <> S_OK then raise Exception.Create( DirectX_GetLastError_MessageString( hr ) );

         VideoWindow.PopupMenu := PopupMenu;
         SoundLevel.Position := FilterGraph.Volume;
         FilterGraph.Play;
    end;
 except
       on E : Exception do
          ShowMessage( E.Message );
 end;
 Graph := nil;
end;
多多留言, 整理文章, 把經驗累積下來.....
回覆文章