EnumCapValue Example for C++ 5.0 and later

// This function will fill a combo box with the values of a certain
// capability either by converting the value to a string or have 
// the string value from a previously defined array
// The function will have the ICapability pointer, a handle to the 
// combo box, pointer to an array to get strings from, and a variant
// to have the current value
int TwainFillComboBox (ICapability_U * pCapability, HWND hItem, char szArray[][32], ILEADRasterVariant_U * *pltRasVar)
{
   HRESULT hr; 
   short iConType; 
   ValueType vType; 

   pCapability->get_ConType (&iConType); 
   
   switch (iConType) 
   {
   case L_TWON_ONEVALUE: 
      {
         ICapabilityOneValue_U * pCapOV = NULL; 

         hr = pCapability->QueryInterface (IID_ICapabilityOneValue_U, (void **)&pCapOV); 
         if (FAILED (hr) || !pCapOV) 
            return 1; 

         pCapOV->get_OneValCapValue (pltRasVar); 
         vType = (*pltRasVar)->Type; 
         switch (vType) 
         {
         case VALUE_CHAR: 
         case VALUE_SHORT: 
         case VALUE_BYTE: 
            if (szArray) 
            {
               short index = (*pltRasVar)->ShortValue; 
               ComboBox_InsertString(hItem, 0, szArray[index]); 
            }
            else
            {
               char sztext[32]; 
               sprintf(sztext, "%d", (*pltRasVar)->ShortValue); 
               ComboBox_InsertString(hItem, 0, sztext); 
            }
            ComboBox_SetItemData(hItem, 0, (*pltRasVar)->ShortValue); 
            break; 
         case VALUE_LONG: 
         case VALUE_USHORT: 
            if (szArray) 
            {
               long index = (*pltRasVar)->LongValue; 
               ComboBox_InsertString(hItem, 0, szArray[index]); 
            }
            else
            {
               char sztext[32]; 
               sprintf(sztext, "%d", (*pltRasVar)->LongValue); 
               ComboBox_InsertString (hItem, 0, sztext); 
            }
            ComboBox_SetItemData (hItem, 0, (*pltRasVar)->LongValue); 
            break; 

         case VALUE_ULONG: 
            if (szArray) 
            {
               ULONG index = (ULONG)(*pltRasVar)->DoubleValue; 
               ComboBox_InsertString (hItem, 0, szArray[index]); 
            }
            else
            {
               char sztext[32]; 
               sprintf (sztext, "%d", (*pltRasVar)->DoubleValue); 
               ComboBox_InsertString (hItem, 0, sztext); 
            }
            ComboBox_SetItemData (hItem, 0, (*pltRasVar)->DoubleValue); 
            break; 
         case VALUE_BOOLEAN: 
            if ((*pltRasVar)->GetBooleanValue())
               ComboBox_InsertString (hItem, 0, "TRUE");
            else
               ComboBox_InsertString (hItem, 0, "FALSE");

            ComboBox_SetItemData (hItem, 0, (*pltRasVar)->GetBooleanValue());
            break; 
         case VALUE_FLOAT: 
            {
               char sztext[32]; 
               sprintf (sztext, "%f", (*pltRasVar)->FloatValue); 
               ComboBox_InsertString (hItem, 0, sztext); 
            }
            break; 
         default: 
            pCapOV->Release ();
            return 1; 
         }
         ComboBox_SetCurSel (hItem, 0); 
         pCapOV->Release ();
      }
      break; 
   case L_TWON_ENUMERATION: 
      {
         long nCount = 0, nIndex = 0, nCurIndex = 0, nDefIndex = 0; 
         short iItemType = 0; 
         ICapabilityEnumeration_U * pCapEnum = NULL; 

         hr = pCapability->QueryInterface (IID_ICapabilityEnumeration_U, (void **)&pCapEnum); 
         if (FAILED (hr) || !pCapEnum) 
            return 1; 
         
         pCapEnum->get_EnumNumOfItems (&nCount); 
         pCapEnum->get_EnumItemType (&iItemType); 
         pCapEnum->get_EnumCurrentIndex (&nCurIndex); 
         pCapEnum->get_EnumDefaultIndex (&nDefIndex); 
         for (nIndex = 0 ; nIndex < nCount ; nIndex ++)
         {
            pCapEnum->get_EnumCapValue (nIndex, &(*pltRasVar)); 
            vType = (*pltRasVar)->Type; 
            switch (vType) 
            {
            case VALUE_CHAR: 
            case VALUE_SHORT: 
            case VALUE_BYTE: 
               if (szArray) 
               {
                  short index = (*pltRasVar)->ShortValue; 
                  ComboBox_InsertString(hItem, 0, szArray[index]); 
               }
               else
               {
                  char sztext[32]; 
                  sprintf(sztext, "%d", (*pltRasVar)->ShortValue); 
                  ComboBox_InsertString(hItem, 0, sztext); 
               }
               ComboBox_SetItemData(hItem, 0, (*pltRasVar)->ShortValue); 
               break; 
            case VALUE_LONG: 
            case VALUE_USHORT: 
               if (szArray) 
               {
                  long index = (*pltRasVar)->LongValue; 
                  ComboBox_InsertString(hItem, 0, szArray[index]); 
               }
               else
               {
                  char sztext[32]; 
                  sprintf(sztext, "%d", (*pltRasVar)->LongValue); 
                  ComboBox_InsertString (hItem, 0, sztext); 
               }
               ComboBox_SetItemData (hItem, 0, (*pltRasVar)->LongValue); 
               break; 

            case VALUE_ULONG: 
               if (szArray) 
               {
                  ULONG index = (ULONG)(*pltRasVar)->DoubleValue; 
                  ComboBox_InsertString (hItem, 0, szArray[index]); 
               }
               else
               {
                  char sztext[32]; 
                  sprintf (sztext, "%d", (*pltRasVar)->DoubleValue); 
                  ComboBox_InsertString (hItem, 0, sztext); 
               }
               ComboBox_SetItemData (hItem, 0, (*pltRasVar)->DoubleValue); 
               break; 
            case VALUE_BOOLEAN: 
               if ((*pltRasVar)->GetBooleanValue())
                  ComboBox_InsertString (hItem, 0, "TRUE");
               else
                  ComboBox_InsertString (hItem, 0, "FALSE");

               ComboBox_SetItemData (hItem, 0, (*pltRasVar)->GetBooleanValue());
               break; 
            case VALUE_FLOAT: 
               {
                  char sztext[32]; 
                  sprintf (sztext, "%f", (*pltRasVar)->FloatValue); 
                  ComboBox_InsertString (hItem, 0, sztext); 
               }
               break; 
            case VALUE_STRING: 
               {
                  BSTR bstrVal = (*pltRasVar)->GetStringValue();
                  CString * pstrText = new CString(bstrVal); 
                  if (pstrText) 
                  {
                     ComboBox_InsertString (hItem, nIndex, pstrText->GetBuffer(0)); 
                     pstrText->ReleaseBuffer();
                     delete pstrText; 
                  }
               }
               break; 
            }
         }

         ComboBox_SetCurSel (hItem, nCurIndex); 
         pCapEnum->Release ();
      }
      break; 
   case L_TWON_ARRAY: 
      {
         long nCount = 0, nIndex = 0; 
         short iItemType = 0; 
         ICapabilityArray_U * pCapArray = NULL; 

         hr = pCapability->QueryInterface (IID_ICapabilityArray_U, (void **)&pCapArray); 
         if (FAILED (hr) || !pCapArray) 
            return 1; 

         pCapArray->get_ArrayNumOfItems (&nCount); 
         pCapArray->get_ArrayItemType (&iItemType);         
         for (nIndex = 0 ; nIndex < nCount ; nIndex ++)
         {
            pCapArray->get_ArrayCapValue (nIndex, &(*pltRasVar)); 
            vType = (*pltRasVar)->Type; 
            switch (vType) 
            {
            case VALUE_CHAR: 
            case VALUE_SHORT: 
            case VALUE_BYTE: 
               if (szArray) 
               {
                  short index = (*pltRasVar)->ShortValue; 
                  ComboBox_InsertString(hItem, 0, szArray[index]); 
               }
               else
               {
                  char sztext[32]; 
                  sprintf(sztext, "%d", (*pltRasVar)->ShortValue); 
                  ComboBox_InsertString(hItem, 0, sztext); 
               }
               ComboBox_SetItemData(hItem, 0, (*pltRasVar)->ShortValue); 
               break; 
            case VALUE_LONG: 
            case VALUE_USHORT: 
               if (szArray) 
               {
                  long index = (*pltRasVar)->LongValue; 
                  ComboBox_InsertString(hItem, 0, szArray[index]); 
               }
               else
               {
                  char sztext[32]; 
                  sprintf(sztext, "%d", (*pltRasVar)->LongValue); 
                  ComboBox_InsertString (hItem, 0, sztext); 
               }
               ComboBox_SetItemData (hItem, 0, (*pltRasVar)->LongValue); 
               break; 

            case VALUE_ULONG: 
               if (szArray) 
               {
                  ULONG index = (ULONG)(*pltRasVar)->DoubleValue; 
                  ComboBox_InsertString (hItem, 0, szArray[index]); 
               }
               else
               {
                  char sztext[32]; 
                  sprintf (sztext, "%d", (*pltRasVar)->DoubleValue); 
                  ComboBox_InsertString (hItem, 0, sztext); 
               }
               ComboBox_SetItemData (hItem, 0, (*pltRasVar)->DoubleValue); 
               break; 
            case VALUE_BOOLEAN: 
               if ((*pltRasVar)->GetBooleanValue())
                  ComboBox_InsertString (hItem, 0, "TRUE");
               else
                  ComboBox_InsertString (hItem, 0, "FALSE");

               ComboBox_SetItemData (hItem, 0, (*pltRasVar)->GetBooleanValue());
               break; 
            case VALUE_FLOAT: 
               {
                  char sztext[32]; 
                  sprintf (sztext, "%f", (*pltRasVar)->FloatValue); 
                  ComboBox_InsertString (hItem, 0, sztext); 
               }
               break; 
            case VALUE_STRING: 
               {
                  BSTR bstrVal = (*pltRasVar)->GetStringValue();
                  CString * pstrText = new CString(bstrVal); 
                  if (pstrText) 
                  {
                     ComboBox_InsertString (hItem, nIndex, pstrText->GetBuffer(0)); 
                     pstrText->ReleaseBuffer();
                     delete pstrText; 
                  }
               }
               break; 
            }
         }

         ComboBox_SetCurSel (hItem, 0); 
         pCapArray->Release ();
      }
      break; 
   case L_TWON_RANGE: 
      {
         char sztext[32]; 
         short iItemType; 
         bool bIncrement = true; 
         ILEADRasterVariant_U * pltRasMinVal = NULL; 
         ILEADRasterVariant_U * pltRasMaxVal = NULL; 
         ILEADRasterVariant_U * pltRasStepVal = NULL; 
         ILEADRasterVariant_U * pltRasDefVal = NULL; 

         long nMinVal, nMaxVal, nStep, nDefVal, nCurVal, nValue, nIndex = 0, nCurIdx = 0; 
         ICapabilityRange_U * pCapRange = NULL; 

         hr = pCapability->QueryInterface (IID_ICapabilityRange_U, (void **)&pCapRange); 
         if (FAILED (hr) || !pCapRange) 
            return 1; 

         pCapRange->get_RangeItemType (&iItemType); 
         pCapRange->get_RangeMinimumValue(&pltRasMinVal); 
         pCapRange->get_RangeMaximumValue(&pltRasMaxVal); 
         pCapRange->get_RangeStepSize(&pltRasStepVal); 
         pCapRange->get_RangeDefaultValue(&pltRasDefVal); 
         pCapRange->get_RangeCurrentValue(pltRasVar); 
         pCapRange->Release ();

         if (pltRasMinVal->Type == VALUE_FLOAT) nMinVal = GetLongValue(pltRasMinVal->FloatValue); 
         if (pltRasMaxVal->Type == VALUE_FLOAT) nMaxVal = GetLongValue(pltRasMaxVal->FloatValue); 
         if (pltRasStepVal->Type== VALUE_FLOAT) nStep =   GetLongValue(pltRasStepVal->FloatValue); 
         if (pltRasDefVal->Type == VALUE_FLOAT) nDefVal = GetLongValue(pltRasDefVal->FloatValue); 
         if ((*pltRasVar)->Type == VALUE_FLOAT) nCurVal = GetLongValue((*pltRasVar)->FloatValue); 

         for (nValue = nMinVal ; nValue <= nMaxVal ; nValue += nStep) 
         {
            if (nValue == nCurVal) 
               bIncrement = false; 

            if (bIncrement) 
               nCurIdx ++;
            sprintf (sztext, "%d", nValue); 
            ComboBox_InsertString (hItem, nIndex, sztext); 
            nIndex ++;

            if (nMinVal == nMaxVal) 
               break; 
         }
         ComboBox_SetCurSel (hItem, nCurIdx); 

         pltRasMinVal->Release();
         pltRasMaxVal->Release();
         pltRasStepVal->Release();
         pltRasDefVal->Release();
      }
      break; 
   default: 
      return 1; 
      break; 
   }
   return 0; 
}

long GetLongValue (float fVal) 
{
   long lResult; 

   lResult = (long)fVal; 
   if (fVal < 0) 
   {
      fVal *= -1; 
      lResult = (long)fVal; 
      lResult *= -1; 
   }
   return lResult; 
}