The following example shows how to enumerate and select ltmfConvert object compressors.
#include "ltmf.h"#include "resource.h"#include "tchar.h"#include "stdio.h"HINSTANCE g_hInstance; // application instance handleIltmfConvert* g_pConvert; // capture object's interface pointer//// BuildCompressorList// fills a listbox to match the contents of the collection//// pCompressors = compressor collection's interface pointer// hwndListBox = listbox window handle//void BuildCompressorList(IltmfCompressors* pCompressors, HWND hwndListBox){long i;long count;int selected = -1;// reset the listbox contentsSendMessage(hwndListBox, LB_RESETCONTENT, 0, 0);// get the collection's item countpCompressors->get_Count(&count);for(i = 0; i < count; i++){IltmfCompressor* pCompressor;BSTR bstr;TCHAR sz[256];int index;VARIANT_BOOL f;// retrieve collection itempCompressors->Item(i, &pCompressor);// get displayable namepCompressor->get_FriendlyName(&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 collectionpCompressor->get_Selected(&f);if(f)selected = i;// free the collection itempCompressor->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;}}}//// CompressorSelectionChanged// called to reflect changes in the listbox selection//// pCompressors = compressor collection's interface pointer// hwndListBox = listbox window handle//void CompressorSelectionChanged(IltmfCompressors* pCompressors, 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 selectionpCompressors->put_Selection(-1);} else{// get the collection item indexitem = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0);// select the item#ifdef _DEBUG{IltmfCompressor* pCompressor;pCompressors->Item(item, &pCompressor);hr = pCompressor->put_Selected(VARIANT_TRUE);pCompressor->Release();}#elsehr = pCompressors->put_Selection(item);#endif// if new selection failed, we need to force the listbox to match the actual selectionif(FAILED(hr)){// get the real selectionpCompressors->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;}}}}}//// RefreshCompressorList// called when to completely rebuild the collection//// pCompressors = compressor collection's interface pointer// hwndListBox = listbox window handle//void RefreshCompressorList(IltmfCompressors* pCompressors, HWND hwndListBox){IltmfCompressor* pCompressor;long item;BSTR bstr;// get the currently selected itempCompressors->get_Selection(&item);if(item >= 0){// get it's unique namepCompressors->Item(item, &pCompressor);pCompressor->get_Name(&bstr);pCompressor->Release();}else{bstr = NULL;}// rebuild the collectionpCompressors->Refresh();if(bstr){// find the previously selected itempCompressors->Find(bstr, &item);// make it the current selectionif(item >= 0)pCompressors->put_Selection(item);SysFreeString(bstr);}// rebuild the listboxBuildCompressorList(pCompressors, hwndListBox);}//// CompressorDlgProc// selects object compressors//BOOL CALLBACK CompressorDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){IltmfCompressors* pCompressors;IltmfCompressor* pCompressor;long item;switch (msg){case WM_INITDIALOG:// initialize the audio compressor listboxg_pConvert->get_AudioCompressors(&pCompressors);BuildCompressorList(pCompressors, GetDlgItem(hwnd, IDC_AUDIOCOMPRESSORS));pCompressors->Release();// initialize the video compressor listboxg_pConvert->get_VideoCompressors(&pCompressors);BuildCompressorList(pCompressors, GetDlgItem(hwnd, IDC_VIDEOCOMPRESSORS));pCompressors->Release();return TRUE;break;case WM_COMMAND:switch(LOWORD(wParam)){case IDC_REFRESHAUDIOCOMPRESSORS:// refresh the audio compressor listboxg_pConvert->get_AudioCompressors(&pCompressors);RefreshCompressorList(pCompressors, GetDlgItem(hwnd, IDC_AUDIOCOMPRESSORS));pCompressors->Release();return TRUE;break;case IDC_AUDIOCOMPRESSORS:if(HIWORD(wParam) == LBN_SELCHANGE){// select the audio compressorg_pConvert->get_AudioCompressors(&pCompressors);CompressorSelectionChanged(pCompressors, GetDlgItem(hwnd, IDC_AUDIOCOMPRESSORS));pCompressors->Release();}break;case IDC_REFRESHVIDEOCOMPRESSORS:// initialize the video compressor listboxg_pConvert->get_VideoCompressors(&pCompressors);RefreshCompressorList(pCompressors, GetDlgItem(hwnd, IDC_VIDEOCOMPRESSORS));pCompressors->Release();return TRUE;break;case IDC_VIDEOCOMPRESSORS:if(HIWORD(wParam) == LBN_SELCHANGE){// select the video compressorg_pConvert->get_VideoCompressors(&pCompressors);CompressorSelectionChanged(pCompressors, GetDlgItem(hwnd, IDC_VIDEOCOMPRESSORS));pCompressors->Release();}break;case IDC_PROPAUDIOCOMPRESSOR:// show audio compressor dialogg_pConvert->get_AudioCompressors(&pCompressors);// get the currently selected itempCompressors->get_Selection(&item);if(item >= 0){// get it's unique namepCompressors->Item(item, &pCompressor);pCompressors->Release();// show properties dialog if availableVARIANT_BOOL f;pCompressor->HasDialog(ltmfCompressor_Dlg_Properties, &f);if (f){pCompressor->ShowDialog(ltmfCompressor_Dlg_Properties,(long)hwnd);}pCompressor->Release();}pCompressors->Release();return TRUE;break;case IDC_PROPVIDEOCOMPRESSOR:// show video compressor dialogg_pConvert->get_VideoCompressors(&pCompressors);// get the currently selected itempCompressors->get_Selection(&item);if(item >= 0){// get it's unique namepCompressors->Item(item, &pCompressor);pCompressors->Release();// show properties dialog if availableVARIANT_BOOL f;pCompressor->HasDialog(ltmfCompressor_Dlg_Properties, &f);if (f){pCompressor->ShowDialog(ltmfCompressor_Dlg_Properties,(long)hwnd);}pCompressor->Release();}pCompressors->Release();return TRUE;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 capture objecthr = CoCreateInstance(CLSID_ltmfConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmfConvert, (void**) &g_pConvert);if(FAILED(hr))goto error;DialogBox(g_hInstance, (LPCTSTR)IDD_COMPRESSORDLG, NULL, CompressorDlgProc); error:// cleanupif(g_pConvert)g_pConvert->Release();CoUninitialize();return 0;}