The following code demonstrates how to enumerate, add, and select ltmfConvert object target formats:
#include "ltmf.h"#include "resource.h"#include "tchar.h"#include "stdio.h"HINSTANCE g_hInstance; // application instance handleIltmfConvert* 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(IltmfTargetFormats* pTargetFormats, HWND hwndListBox){long i;long count;int selected = -1;int listcount = 0;// reset the listbox contentsSendMessage(hwndListBox, LB_RESETCONTENT, 0, 0);// get the collection's item countpTargetFormats->get_Count(&count);for(i = 0; i < count; i++){IltmfTargetFormat* pTargetFormat;BSTR bstr;TCHAR sz[256];int index;VARIANT_BOOL f;// retrieve collection itempTargetFormats->Item(i, &pTargetFormat);// add only formats that are availablepTargetFormat->get_Available(&f);if (f){// 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;listcount++;}// free the collection itempTargetFormat->Release();}// select the appropriate listbox itemfor(i = 0; i < listcount; 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(IltmfTargetFormats* 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{IltmfTargetFormat* 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;}}}}}//// TargetFormatDlgProc// selects object formats//BOOL CALLBACK TargetFormatDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){IltmfTargetFormats* pTargetFormats;switch (msg){case WM_INITDIALOG:// initialize the target format listboxg_pConvert->get_TargetFormats(&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_ltmfConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmfConvert, (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;}