The following code demonstrates how to enumerate, add, and select ltmmConvert
object target formats:
#include "ltmm.h"#include "resource.h"#include "tchar.h"#include "stdio.h"HINSTANCE g_hInstance; // application instance handleIltmmConvert* g_pConvert; // convert object's interface pointer//// BuildTargetFormatList// fills a listbox to match the contents of the collection//// pTargetFormats = format collection's interface pointer// hwndListBox = listbox window handle//void BuildTargetFormatList(IltmmTargetFormats* pTargetFormats, HWND hwndListBox){long i;long count;int selected = -1;// reset the listbox contentsSendMessage(hwndListBox, LB_RESETCONTENT, 0, 0);// get the collection's item countpTargetFormats->get_Count(&count);for(i = 0; i < count; i++){IltmmTargetFormat* pTargetFormat;BSTR bstr;TCHAR sz[256];int index;VARIANT_BOOL f;// retrieve collection itempTargetFormats->Item(i, &pTargetFormat);// get displayable namepTargetFormat->get_Name(&bstr);// convert from unicode_stprintf(sz, _T("%ls"), bstr);// free nameSysFreeString(bstr);// add the name to the listboxindex = SendMessage(hwndListBox, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) sz);// associate the listbox item with the collection item indexSendMessage(hwndListBox, LB_SETITEMDATA, index, i);// remember this listbox item, if it is selected in the collectionpTargetFormat->get_Selected(&f);if(f)selected = i;// free the collection itempTargetFormat->Release();}// select the appropriate listbox itemfor(i = 0; i < count; i++){if(selected == (long) SendMessage(hwndListBox, LB_GETITEMDATA, i, 0)){SendMessage(hwndListBox, LB_SETCURSEL, i, 0);break;}}}//// TargetFormatSelectionChanged// called to reflect changes in the listbox selection//// pTargetFormats = format collection's interface pointer// hwndListBox = listbox window handle//void TargetFormatSelectionChanged(IltmmTargetFormats* pTargetFormats, HWND hwndListBox){int index;long item;int i;int count;HRESULT hr;// get the index of current listbox selectionindex = (int) SendMessage(hwndListBox, LB_GETCURSEL, 0, 0);if(index < 0){// clear selectionpTargetFormats->put_Selection(-1);}else{// get the collection item indexitem = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0);// select the item#ifdef _DEBUG{IltmmTargetFormat* pTargetFormat;pTargetFormats->Item(item, &pTargetFormat);hr = pTargetFormat->put_Selected(VARIANT_TRUE);pTargetFormat->Release();}#elsehr = pTargetFormats->put_Selection(item);#endif// if new selection failed, we need to force the listbox to match the actual selectionif(FAILED(hr)){// get the real selectionpTargetFormats->get_Selection(&item);// find the matching listbox itemcount = (int) SendMessage(hwndListBox, LB_GETCOUNT, 0, 0);for(i = 0; i < count; i++){if(item == (long) SendMessage(hwndListBox, LB_GETITEMDATA, i, 0)){SendMessage(hwndListBox, LB_SETCURSEL, i, 0);break;}}}}#ifdef _DEBUG{long item;pTargetFormats->get_Selection(&item);if(item >= 0){IltmmTargetFormat* pTargetFormat;TCHAR sz[2048];LPTSTR p;VARIANT_BOOL b;BSTR bstr;sz[0] = _T('\0');p = sz;pTargetFormats->Item(item, &pTargetFormat);pTargetFormat->get_AudioCompressor(&bstr);p += _stprintf(p, _T("AComp = %ls"), bstr);SysFreeString(bstr);pTargetFormat->get_VideoCompressor(&bstr);p += _stprintf(p, _T(", VComp = %ls"), bstr);SysFreeString(bstr);pTargetFormat->get_AVMux(&bstr);p += _stprintf(p, _T(", AVMux = %ls"), bstr);SysFreeString(bstr);pTargetFormat->get_Mux(&bstr);p += _stprintf(p, _T(", Mux = %ls"), bstr);SysFreeString(bstr);pTargetFormat->get_SinkSubType(&bstr);p += _stprintf(p, _T(", SubType = %ls"), bstr);SysFreeString(bstr);pTargetFormat->get_Sink(&bstr);p += _stprintf(p, _T(", Sink = %ls"), bstr);SysFreeString(bstr);pTargetFormat->get_ReadOnly(&b);p += _stprintf(p, _T(", ReadOnly = %s"), b != 0 ? _T("Yes") : _T("No"));pTargetFormat->Release();MessageBox(NULL, sz, _T("Target Format"), MB_OK);}}#endif}//// AddCustomTargetFormats// called to append custom target formats//// pTargetFormats = format collection's interface pointer//void AddCustomTargetFormats(IltmmTargetFormats* pTargetFormats){BSTR bstr;long index;IltmmTargetFormat* pTargetFormat;// remove all custom formats#ifdef _DEBUGpTargetFormats->get_Count(&index);pTargetFormats->Remove(index - 1);#elsepTargetFormats->RemoveAll();#endifbstr = SysAllocString(L"CUSTOM FORMAT 1");pTargetFormats->Add(bstr, -1);pTargetFormats->Find(bstr, &index);SysFreeString(bstr);pTargetFormats->Item(index, &pTargetFormat);// assign an optional audio compressor// this case: MP3bstr = SysAllocString(L"@device:cm:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\85MPEG Layer-3");pTargetFormat->put_AudioCompressor(bstr);SysFreeString(bstr);// assign an optional video compressor// this case: LEAD MCMPbstr = SysAllocString(L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD MCMP/MJPEG Codec (2.0)");pTargetFormat->put_VideoCompressor(bstr);SysFreeString(bstr);// assign an optional AVMux compressor// this case: no premuxbstr = SysAllocString(L"");pTargetFormat->put_AVMux(bstr);SysFreeString(bstr);// assign the required Mux// this case: AVIbstr = SysAllocString(L"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\\{E2510970-F137-11CE-8B67-00AA00A3F1A6}");pTargetFormat->put_Mux(bstr);SysFreeString(bstr);// assign the optional SinkSubType// this case: MEDIASUBTYPE_Avibstr = SysAllocString(L"{E436EB88-524F-11CE-9F53-0020AF0BA770}");pTargetFormat->put_SinkSubType(bstr);SysFreeString(bstr);// assign an optional Sink// this case: defaultbstr = SysAllocString(L"");pTargetFormat->put_Sink(bstr);SysFreeString(bstr);pTargetFormat->Release();}//// TargetFormatDlgProc// selects object formats//BOOL CALLBACK TargetFormatDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){IltmmTargetFormats* pTargetFormats;switch (msg){case WM_INITDIALOG:// initialize the target format listboxg_pConvert->get_TargetFormats(&pTargetFormats);AddCustomTargetFormats(pTargetFormats);BuildTargetFormatList(pTargetFormats, GetDlgItem(hwnd, IDC_TARGETFORMATS));pTargetFormats->Release();return TRUE;break;case WM_COMMAND:switch(LOWORD(wParam)){case IDC_TARGETFORMATS:if(HIWORD(wParam) == LBN_SELCHANGE){// select the target formatg_pConvert->get_TargetFormats(&pTargetFormats);TargetFormatSelectionChanged(pTargetFormats, GetDlgItem(hwnd, IDC_TARGETFORMATS));pTargetFormats->Release();}break;case IDOK:EndDialog(hwnd, IDOK);return TRUE;break;}break;}return FALSE;}int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){HRESULT hr;g_hInstance = hInstance;// initialize COM libraryhr = CoInitialize(NULL);if(FAILED(hr))goto error;// create the convert objecthr = CoCreateInstance(CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmmConvert, (void**) &g_pConvert);if(FAILED(hr))goto error;DialogBox(g_hInstance, (LPCTSTR)IDD_TARGETFORMATDLG, NULL, TargetFormatDlgProc);error:// cleanupif(g_pConvert)g_pConvert->Release();CoUninitialize();return 0;}
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
