The following example shows how to enumerate and select ltmmConvert target devices.
#include "ltmm.h"#include "resource.h"#include "tchar.h"#include "stdio.h"HINSTANCE g_hInstance; // application instance handleIltmmConvert* g_pConvert; // convert object's interface pointer//// BuildTargetDeviceList// fills a listbox to match the contents of the collection//// pTargetDevices = device collection's interface pointer// hwndListBox = listbox window handle//void BuildTargetDeviceList(IltmmTargetDevices* pTargetDevices, HWND hwndListBox){long i;long count;int selected = -1;// reset the listbox contentsSendMessage(hwndListBox, LB_RESETCONTENT, 0, 0);// get the collection's item countpTargetDevices->get_Count(&count);for(i = 0; i < count; i++){IltmmTargetDevice* pTargetDevice;BSTR bstr;TCHAR sz[256];int index;VARIANT_BOOL f;// retrieve collection itempTargetDevices->Item(i, &pTargetDevice);// get displayable namepTargetDevice->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 collectionpTargetDevice->get_Selected(&f);if(f)selected = i;// free the collection itempTargetDevice->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;}}}//// TargetDeviceSelectionChanged// called to reflect changes in the listbox selection//// pTargetDevices = device collection's interface pointer// hwndListBox = listbox window handle//void TargetDeviceSelectionChanged(IltmmTargetDevices* pTargetDevices, 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 selectionpTargetDevices->put_Selection(-1);} else{// get the collection item indexitem = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0);// select the item#ifdef _DEBUG{IltmmTargetDevice* pTargetDevice;pTargetDevices->Item(item, &pTargetDevice);hr = pTargetDevice->put_Selected(VARIANT_TRUE);pTargetDevice->Release();}#elsehr = pTargetDevices->put_Selection(item);#endif// if new selection failed, we need to force the listbox to match the actual selectionif(FAILED(hr)){// get the real selectionpTargetDevices->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;}}}}}//// RefreshTargetDeviceList// called when to completely rebuild the collection//// pTargetDevices = device collection's interface pointer// hwndListBox = listbox window handle//void RefreshTargetDeviceList(IltmmTargetDevices* pTargetDevices, HWND hwndListBox){IltmmTargetDevice* pTargetDevice;long item;BSTR bstr;// get the currently selected itempTargetDevices->get_Selection(&item);if(item >= 0){// get it's unique namepTargetDevices->Item(item, &pTargetDevice);pTargetDevice->get_Name(&bstr);pTargetDevice->Release();}else{bstr = NULL;} // rebuild the collectionpTargetDevices->Refresh();if(bstr){// find the previously selected itempTargetDevices->Find(bstr, &item);// make it the current selectionif(item >= 0)pTargetDevices->put_Selection(item);SysFreeString(bstr);}// rebuild the listboxBuildTargetDeviceList(pTargetDevices, hwndListBox);}//// TargetDeviceDlgProc// selects object devices//BOOL CALLBACK TargetDeviceDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){IltmmTargetDevices* pTargetDevices;switch (msg){case WM_INITDIALOG:// initialize the target device listboxg_pConvert->get_TargetDevices(&pTargetDevices);BuildTargetDeviceList(pTargetDevices, GetDlgItem(hwnd, IDC_TARGETDEVICES));pTargetDevices->Release();return TRUE;break;case WM_COMMAND:switch(LOWORD(wParam)){case IDC_REFRESHTARGETDEVICES:// initialize the target device listboxg_pConvert->get_TargetDevices(&pTargetDevices);RefreshTargetDeviceList(pTargetDevices, GetDlgItem(hwnd, IDC_TARGETDEVICES));pTargetDevices->Release();return TRUE;break;case IDC_TARGETDEVICES:if(HIWORD(wParam) == LBN_SELCHANGE){// select the target deviceg_pConvert->get_TargetDevices(&pTargetDevices);TargetDeviceSelectionChanged(pTargetDevices, GetDlgItem(hwnd, IDC_TARGETDEVICES));pTargetDevices->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_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmmConvert, (void**) &g_pConvert);if(FAILED(hr))goto error;DialogBox(g_hInstance, (LPCTSTR)IDD_TARGETDEVICEDLG, NULL, TargetDeviceDlgProc);error:// cleanupif(g_pConvert)g_pConvert->Release();CoUninitialize();return 0;}
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
