Selecting ltmfConvert Object Target Formats Example for C++

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 handle 
IltmfConvert* 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 contents 
   SendMessage(hwndListBox, LB_RESETCONTENT, 0, 0); 
   // get the collection's item count 
   pTargetFormats->get_Count(&count); 
   for(i = 0; i < count; i++) 
   { 
      IltmfTargetFormat* pTargetFormat; 
      BSTR bstr; 
      TCHAR sz[256]; 
      int index; 
      VARIANT_BOOL f; 
      // retrieve collection item 
      pTargetFormats->Item(i, &pTargetFormat); 
 
      // add only formats that are available 
      pTargetFormat->get_Available(&f); 
      if (f) 
      { 
         // get displayable name 
         pTargetFormat->get_Name(&bstr); 
         // convert from unicode 
         _stprintf(sz, _T("%ls"), bstr); 
         // free name 
         SysFreeString(bstr); 
         // add the name to the listbox 
         index = SendMessage(hwndListBox, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)sz); 
         // associate the listbox item with the collection item index 
         SendMessage(hwndListBox, LB_SETITEMDATA, index, i); 
         // remember this listbox item, if it is selected in the collection 
         pTargetFormat->get_Selected(&f); 
         if (f) 
            selected = i; 
         listcount++; 
      } 
    
      // free the collection item 
      pTargetFormat->Release(); 
   } 
   // select the appropriate listbox item 
   for(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 selection 
   index = (int) SendMessage(hwndListBox, LB_GETCURSEL, 0, 0); 
    
   if(index < 0) 
   { 
      // clear selection 
      pTargetFormats->put_Selection(-1); 
   } 
   else 
   { 
      // get the collection item index 
      item = (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(); 
      } 
#else 
      hr = pTargetFormats->put_Selection(item); 
#endif 
      // if new selection failed, we need to force the listbox to match the actual selection 
      if(FAILED(hr)) 
      { 
         // get the real selection 
         pTargetFormats->get_Selection(&item);    
    
         // find the matching listbox item 
         count = (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 listbox 
      g_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 format 
            g_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 library 
   hr = CoInitialize(NULL); 
   if(FAILED(hr)) 
      goto error; 
    
   // create the convert object 
   hr = 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: 
   // cleanup 
   if(g_pConvert) 
      g_pConvert->Release(); 
   CoUninitialize(); 
    
   return 0; 
} 

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Media Foundation C API Help
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.