The following code demonstrates how to enumerate, add, and select ltmmCapture object target
formats:
#ifndef COBJMACROS#define COBJMACROS#endif#include "ltmm.h"#include "resource.h"#include "tchar.h"#include "stdio.h"HINSTANCE g_hInstance; // application instance handleIltmmCapture* g_pCapture; // capture 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 countIltmmTargetFormats_get_Count (pTargetFormats, &count);for(i = 0; i < count; i++){IltmmTargetFormat* pTargetFormat;BSTR bstr;TCHAR sz[256];int index;VARIANT_BOOL f;// retrieve collection itemIltmmTargetFormats_Item (pTargetFormats, i, &pTargetFormat);// get displayable nameIltmmTargetFormat_get_Name(pTargetFormat, &bstr);// capture 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 collectionIltmmTargetFormat_get_Selected(pTargetFormat, &f);if(f)selected = i;// free the collection itemIUnknown_Release(pTargetFormat);}// 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 selectionIltmmTargetFormats_put_Selection(pTargetFormats, -1);}else{// get the collection item indexitem = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0);// select the item#ifdef _DEBUG{IltmmTargetFormat* pTargetFormat;IltmmTargetFormats_Item (pTargetFormats, item, &pTargetFormat);hr = IltmmTargetFormat_put_Selected(pTargetFormat, VARIANT_TRUE);IUnknown_Release(pTargetFormat);}#elsehr = IltmmTargetFormats_put_Selection(pTargetFormats, item);#endif// if new selection failed, we need to force the listbox to match the actual selectionif(FAILED(hr)){// get the real selectionIltmmTargetFormats_get_Selection (pTargetFormats, &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;IltmmTargetFormats_get_Selection (pTargetFormats, &item);if(item >= 0){IltmmTargetFormat* pTargetFormat;TCHAR sz[2048];LPTSTR p;VARIANT_BOOL b;BSTR bstr;sz[0] = _T('\0');p = sz;IltmmTargetFormats_Item (pTargetFormats, item, &pTargetFormat);IltmmTargetFormat_get_AudioCompressor(pTargetFormat, &bstr);p += _stprintf(p, _T("AComp = %ls"), bstr);SysFreeString(bstr);IltmmTargetFormat_get_VideoCompressor(pTargetFormat, &bstr);p += _stprintf(p, _T(", VComp = %ls"), bstr);SysFreeString(bstr);IltmmTargetFormat_get_AVMux(pTargetFormat, &bstr);p += _stprintf(p, _T(", AVMux = %ls"), bstr);SysFreeString(bstr);IltmmTargetFormat_get_Mux(pTargetFormat, &bstr);p += _stprintf(p, _T(", Mux = %ls"), bstr);SysFreeString(bstr);IltmmTargetFormat_get_SinkSubType (pTargetFormat, &bstr);p += _stprintf(p, _T(", SubType = %ls"), bstr);SysFreeString(bstr);IltmmTargetFormat_get_Sink(pTargetFormat, &bstr);p += _stprintf(p, _T(", Sink = %ls"), bstr);SysFreeString(bstr);IltmmTargetFormat_get_ReadOnly(pTargetFormat, &b);p += _stprintf(p, _T(", ReadOnly = %s"), b != 0 ? _T("Yes") : _T("No"));IUnknown_Release(pTargetFormat);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 _DEBUGIltmmTargetFormats_get_Count (pTargetFormats, &index);IltmmTargetFormats_Remove(pTargetFormats, index - 1);#elseIltmmTargetFormats_RemoveAll(pTargetFormats);#endifbstr = SysAllocString(L"CUSTOM FORMAT 1");IltmmTargetFormats_Add(pTargetFormats, bstr, -1);IltmmTargetFormats_Find(pTargetFormats, bstr, &index);SysFreeString(bstr);IltmmTargetFormats_Item(pTargetFormats, index, &pTargetFormat);// assign an optional audio compressor// this case: MP3bstr = SysAllocString(L"@device:cm:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\85MPEG Layer-3");IltmmTargetFormat_put_AudioCompressor (pTargetFormat, 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)");IltmmTargetFormat_put_VideoCompressor (pTargetFormat, bstr);SysFreeString(bstr);// assign an optional AVMux compressor// this case: no premuxbstr = SysAllocString(L"");IltmmTargetFormat_put_AVMux(pTargetFormat, bstr);SysFreeString(bstr);// assign the required Mux// this case: AVIbstr = SysAllocString(L"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\\{E2510970-F137-11CE-8B67-00AA00A3F1A6}");IltmmTargetFormat_put_Mux(pTargetFormat, bstr);SysFreeString(bstr);// assign the optional SinkSubType// this case: MEDIASUBTYPE_Avibstr = SysAllocString(L"{E436EB88-524F-11CE-9F53-0020AF0BA770}");IltmmTargetFormat_put_SinkSubType(pTargetFormat, bstr);SysFreeString(bstr);// assign an optional Sink// this case: defaultbstr = SysAllocString(L"");IltmmTargetFormat_put_Sink(pTargetFormat, bstr);SysFreeString(bstr);IUnknown_Release(pTargetFormat);}//// TargetFormatDlgProc// selects object formats//BOOL CALLBACK TargetFormatDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){IltmmTargetFormats* pTargetFormats;lParam = lParam ;switch (msg){case WM_INITDIALOG:// initialize the target format listboxIltmmCapture_get_TargetFormats(g_pCapture, &pTargetFormats);AddCustomTargetFormats(pTargetFormats);BuildTargetFormatList(pTargetFormats, GetDlgItem(hwnd, IDC_TARGETFORMATS));IUnknown_Release(pTargetFormats);return TRUE;break;case WM_COMMAND:switch(LOWORD(wParam)){case IDC_TARGETFORMATS:if(HIWORD(wParam) == LBN_SELCHANGE){// select the target formatIltmmCapture_get_TargetFormats(g_pCapture, &pTargetFormats);TargetFormatSelectionChanged(pTargetFormats, GetDlgItem(hwnd, IDC_TARGETFORMATS));IUnknown_Release(pTargetFormats);}break;case IDOK:EndDialog(hwnd, IDOK);return TRUE;break;}break;}return FALSE;}void SelectingltmmCaptureObjectTargetFormats_Example ( HINSTANCE hInstance ){HRESULT hr;g_hInstance = hInstance;// initialize COM libraryhr = CoInitialize(NULL);if(FAILED(hr))goto error;// create the capture objecthr = CoCreateInstance(&CLSID_ltmmCapture, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmCapture, (void**) &g_pCapture);if(FAILED(hr))goto error;DialogBox(g_hInstance, (LPCTSTR)IDD_TARGETFORMATDLG, NULL, TargetFormatDlgProc);error:// cleanupif(g_pCapture)IUnknown_Release(g_pCapture);CoUninitialize();}
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
