Selecting ltmmCapture Object Sizes for C

The following example shows how to enumerate and select ltmmCapture object sizes.

// define helper macros for using interfaces under C
#define COBJMACROS

#include "ltmm.h"
#include "resource.h"
#include "tchar.h"
#include "stdio.h"

HINSTANCE g_hInstance;         // application instance handle
IltmmCapture* g_pCapture;      // capture object's interface pointer

//
// BuildSizeList
// fills a listbox to match the contents of the collection
//
// pSizes = size collection's interface pointer
// hwndListBox = listbox window handle
//
void BuildSizeList(IltmmCaptureSizes* pSizes, HWND hwndListBox) 
{
   long i; 
   long count; 
   int selected = -1; 

   // reset the listbox contents
   SendMessage(hwndListBox, LB_RESETCONTENT, 0, 0); 

   // get the collection's item count
   IltmmCaptureSizes_get_Count(pSizes, &count); 

   for(i = 0; i < count; i++)
   {
      IltmmCaptureSize* pSize; 
      TCHAR sz[256]; 
      int index; 
      VARIANT_BOOL f; 
      long width; 
      long height; 

      // retrieve collection item
      IltmmCaptureSizes_Item(pSizes, i, &pSize); 
   
      // get displayable name
      IltmmCaptureSize_get_Width(pSize, &width); 
      IltmmCaptureSize_get_Height(pSize, &height); 
      _stprintf(sz, _T("%d x %d"), width, height); 

      // 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
      IltmmCaptureSize_get_Selected(pSize, &f); 
      if(f) 
         selected = i; 
   
      // free the collection item
      IUnknown_Release(pSize); 
   }

   // select the appropriate listbox item
   for(i = 0; i < count; i++)
   {
      if(selected == (long) SendMessage(hwndListBox, LB__getITEMDATA, i, 0)) 
      {
         SendMessage(hwndListBox, LB_SETCURSEL, i, 0); 
         break; 
      }
   }

}

//
// SizeSelectionChanged
// called to reflect changes in the listbox selection
//
// pSizes = size collection's interface pointer
// hwndListBox = listbox window handle
//
void SizeSelectionChanged(IltmmCaptureSizes* pSizes, 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) 
   {

      // get the collection item index
      item = (long) SendMessage(hwndListBox, LB__getITEMDATA, index, 0); 
   
      // select the item
#ifdef _DEBUG
      {
         IltmmCaptureSize* pSize; 
         IltmmCaptureSizes_Item(pSizes, item, &pSize); 
         hr = IltmmCaptureSize_put_Selected(pSize, VARIANT_TRUE); 
         IUnknown_Release(pSize);
      }
#else
      hr = IltmmCaptureSizes_put_Selection(pSizes, item); 
#endif
      // if new selection failed, we need to force the listbox to match the actual selection
      if(FAILED(hr)) 
      {
         // get the real selection
         IltmmCaptureSizes_get_Selection(pSizes, &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; 
            }
         }

      }
   
   }

}


//
// SizeDlgProc
// selects object sizes
//
BOOL CALLBACK SizeDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{
   IltmmCaptureSizes* pSizes; 
   IltmmDevices* pDevices; 
   long index; 
   
   switch (msg) 
   {
   case WM_INITDIALOG: 
      // select the first video capture device
      IltmmCapture_get_VideoDevices(g_pCapture, &pDevices); 
      IltmmDevices__put_Selection(pDevices, 0); 
      IUnknown_Release(pDevices); 

      IltmmCapture_get_VideoCaptureSizes(g_pCapture, &pSizes); 

      // setup an initial size of 320 x 240
      IltmmCaptureSizes_Find(pSizes, 320, 240, &index); 
      IltmmCaptureSizes_put_Selection(pSizes, index); 

      // initialize the video size listbox
      BuildSizeList(pSizes, GetDlgItem(hwnd, IDC_CAPTURESIZES)); 
      IUnknown_Release(pSizes); 

      
      return TRUE; 
      break; 

   case WM_COMMAND: 
      switch(LOWORD(wParam)) 
      {
      case IDC_CAPTURESIZES: 
         if(HIWORD(wParam) == LBN_SELCHANGE) 
         {
            // select the video size
            IltmmCapture_get_VideoCaptureSizes(g_pCapture, &pSizes); 
            SizeSelectionChanged(pSizes, GetDlgItem(hwnd, IDC_CAPTURESIZES)); 
            IUnknown_Release(pSizes); 
         }
         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 capture object
   hr = CoCreateInstance(&CLSID_ltmmCapture, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmCapture, (void**) &g_pCapture); 
   if(FAILED(hr)) 
      goto error; 
   
   
   DialogBox(g_hInstance, (LPCTSTR)IDD_SIZEDLG, NULL, SizeDlgProc); 


error: 
   // cleanup

   if(g_pCapture) 
      IUnknown_Release(g_pCapture); 

   CoUninitialize();
   
   return 0; 
}