Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
10 KiB

  1. // WiacapDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "wiatest.h"
  5. #include "WiacapDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CWiacapDlg dialog
  13. CWiacapDlg::CWiacapDlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CWiacapDlg::IDD, pParent)
  15. {
  16. m_pIWiaItem = NULL;
  17. m_NumCaps = 0;
  18. m_bCommandSent = FALSE;
  19. //{{AFX_DATA_INIT(CWiacapDlg)
  20. // NOTE: the ClassWizard will add member initialization here
  21. //}}AFX_DATA_INIT
  22. }
  23. void CWiacapDlg::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. //{{AFX_DATA_MAP(CWiacapDlg)
  27. DDX_Control(pDX, IDC_SEND_COMMAND_BUTTON, m_SendCommandButton);
  28. DDX_Control(pDX, IDC_CAPABILITIES_LISTCTRL, m_CapablitiesListCtrl);
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(CWiacapDlg, CDialog)
  32. //{{AFX_MSG_MAP(CWiacapDlg)
  33. ON_WM_CLOSE()
  34. ON_BN_CLICKED(IDC_SEND_COMMAND_BUTTON, OnSendCommandButton)
  35. ON_NOTIFY(NM_CLICK, IDC_CAPABILITIES_LISTCTRL, OnClickCapabilitiesListctrl)
  36. ON_NOTIFY(LVN_ITEMCHANGED, IDC_CAPABILITIES_LISTCTRL, OnItemchangedCapabilitiesListctrl)
  37. //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CWiacapDlg message handlers
  41. BOOL CWiacapDlg::OnInitDialog()
  42. {
  43. CDialog::OnInitDialog();
  44. SetupColumnHeaders();
  45. AddCapabilitiesToListBox(WIA_DEVICE_EVENTS);
  46. AddCapabilitiesToListBox(WIA_DEVICE_COMMANDS);
  47. return TRUE; // return TRUE unless you set the focus to a control
  48. // EXCEPTION: OCX Property Pages should return FALSE
  49. }
  50. void CWiacapDlg::SetupColumnHeaders()
  51. {
  52. LVCOLUMN lv;
  53. TCHAR szColumnName[MAX_PATH];
  54. memset(szColumnName,0,sizeof(szColumnName));
  55. HINSTANCE hInstance = NULL;
  56. hInstance = AfxGetInstanceHandle();
  57. if(hInstance){
  58. int i = 0;
  59. // initialize item property list control column headers
  60. // Capability name
  61. LoadString(hInstance,IDS_WIATESTCOLUMN_CAPABILITYNAME,szColumnName,MAX_PATH);
  62. lv.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  63. lv.fmt = LVCFMT_LEFT ;
  64. lv.cx = 100;
  65. lv.pszText = szColumnName;
  66. lv.cchTextMax = 0;
  67. lv.iSubItem = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYNAME;
  68. lv.iImage = 0;
  69. lv.iOrder = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYNAME;
  70. i = m_CapablitiesListCtrl.InsertColumn(ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYNAME,&lv);
  71. // Capability description
  72. LoadString(hInstance,IDS_WIATESTCOLUMN_CAPABILITYDESCRIPTION,szColumnName,MAX_PATH);
  73. lv.cx = 125;
  74. lv.iOrder = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYDESCRIPTION;
  75. lv.iSubItem = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYDESCRIPTION;
  76. lv.pszText = szColumnName;
  77. i = m_CapablitiesListCtrl.InsertColumn(ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYDESCRIPTION,&lv);
  78. // Capability Value
  79. LoadString(hInstance,IDS_WIATESTCOLUMN_CAPABILITYVALUE,szColumnName,MAX_PATH);
  80. lv.cx = 125;
  81. lv.iOrder = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYVALUE;
  82. lv.iSubItem = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYVALUE;
  83. lv.pszText = szColumnName;
  84. i = m_CapablitiesListCtrl.InsertColumn(ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYVALUE,&lv);
  85. // Capability type (event or command)
  86. LoadString(hInstance,IDS_WIATESTCOLUMN_CAPABILITYTYPE,szColumnName,MAX_PATH);
  87. lv.cx = 85;
  88. lv.iOrder = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYTYPE;
  89. lv.iSubItem = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYTYPE;
  90. lv.pszText = szColumnName;
  91. i = m_CapablitiesListCtrl.InsertColumn(ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYTYPE,&lv);
  92. }
  93. }
  94. void CWiacapDlg::AddCapabilitiesToListBox(LONG lType)
  95. {
  96. WIA_DEV_CAP DevCap;
  97. IEnumWIA_DEV_CAPS* pIEnumWiaDevCaps = NULL;
  98. HRESULT hr = S_OK;
  99. INT ItemNumber = m_NumCaps;
  100. hr = m_pIWiaItem->EnumDeviceCapabilities(lType,&pIEnumWiaDevCaps);
  101. if(S_OK == hr){
  102. do {
  103. memset(&DevCap,0,sizeof(DevCap));
  104. hr = pIEnumWiaDevCaps->Next(1,&DevCap,NULL);
  105. if (S_OK == hr){
  106. // DevCap.ulFlags;
  107. // DevCap.bstrIcon;
  108. // DevCap.bstrCommanline;
  109. //DevCap.guid;
  110. TCHAR szText[MAX_PATH];
  111. memset(szText,0,sizeof(szText));
  112. LV_ITEM lvitem;
  113. lvitem.mask = LVIF_TEXT;
  114. lvitem.iItem = ItemNumber;
  115. lvitem.iSubItem = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYNAME;
  116. lvitem.pszText = szText;
  117. lvitem.iImage = NULL;
  118. // Write capability name to list control
  119. #ifndef UNICODE
  120. WideCharToMultiByte(CP_ACP,0,DevCap.bstrName,-1,szText,MAX_PATH,NULL,NULL);
  121. #else
  122. lstrcpy(szText,DevCap.bstrName);
  123. #endif
  124. m_CapablitiesListCtrl.InsertItem(&lvitem);
  125. // Write capability description to list control
  126. memset(szText,0,sizeof(szText));
  127. lvitem.iSubItem = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYDESCRIPTION;
  128. #ifndef UNICODE
  129. WideCharToMultiByte(CP_ACP,0,DevCap.bstrDescription,-1,szText,MAX_PATH,NULL,NULL);
  130. #else
  131. lstrcpy(szText,DevCap.bstrDescription);
  132. #endif
  133. m_CapablitiesListCtrl.SetItem(&lvitem);
  134. // Write capability value
  135. memset(szText,0,sizeof(szText));
  136. lvitem.iSubItem = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYVALUE;
  137. UCHAR *pwszUUID = NULL;
  138. UuidToString(&DevCap.guid,&pwszUUID);
  139. TSPRINTF(szText,"%s",pwszUUID);
  140. // free allocated string
  141. RpcStringFree(&pwszUUID);
  142. m_CapablitiesListCtrl.SetItem(&lvitem);
  143. // Write capability type
  144. memset(szText,0,sizeof(szText));
  145. lvitem.iSubItem = ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYTYPE;
  146. if(lType == WIA_DEVICE_COMMANDS){
  147. RC2TSTR(IDS_WIATESTCOMMAND,szText, sizeof(szText));
  148. } else {
  149. RC2TSTR(IDS_WIATESTEVENT,szText, sizeof(szText));
  150. }
  151. m_CapablitiesListCtrl.SetItem(&lvitem);
  152. // increment Row counter
  153. ItemNumber++;
  154. // free allocated strings
  155. if(DevCap.bstrName){
  156. SysFreeString(DevCap.bstrName);
  157. }
  158. if(DevCap.bstrDescription){
  159. SysFreeString(DevCap.bstrDescription);
  160. }
  161. }
  162. }while(hr == S_OK);
  163. pIEnumWiaDevCaps->Release();
  164. pIEnumWiaDevCaps = NULL;
  165. }
  166. // auto resize columns
  167. for (int Col = 0; Col <4;Col++){
  168. m_CapablitiesListCtrl.SetColumnWidth(Col, LVSCW_AUTOSIZE);
  169. }
  170. m_NumCaps = ItemNumber;
  171. }
  172. void CWiacapDlg::SetIWiaItem(IWiaItem *pIWiaItem)
  173. {
  174. pIWiaItem->AddRef();
  175. m_pIWiaItem = pIWiaItem;
  176. }
  177. void CWiacapDlg::OnClose()
  178. {
  179. if(m_pIWiaItem){
  180. m_pIWiaItem->Release();
  181. m_pIWiaItem = NULL;
  182. }
  183. CDialog::OnClose();
  184. }
  185. void CWiacapDlg::OnSendCommandButton()
  186. {
  187. HRESULT hr = S_OK;
  188. GUID guidCommand;
  189. memset(&guidCommand,0,sizeof(GUID));
  190. IWiaItem *pIWiaItem = NULL;
  191. GetCommandGUID(GetSelectedCapability(),&guidCommand);
  192. if(guidCommand != GUID_NULL){
  193. hr = m_pIWiaItem->DeviceCommand(0,&guidCommand,&pIWiaItem);
  194. if(FAILED(hr)){
  195. ErrorMessageBox(IDS_WIATESTERROR_DEVICECOMMAND,hr);
  196. } else {
  197. //m_bCommandSent = TRUE;
  198. if(pIWiaItem){
  199. pIWiaItem->Release();
  200. pIWiaItem = NULL;
  201. }
  202. TCHAR szCommandSuccess[MAX_PATH];
  203. memset(szCommandSuccess,0,sizeof(szCommandSuccess));
  204. RC2TSTR(IDS_WIATESTSUCCESS_COMMAND,szCommandSuccess,sizeof(szCommandSuccess));
  205. MessageBox(szCommandSuccess);
  206. }
  207. } else {
  208. ErrorMessageBox(IDS_WIATESTERROR_NOCOMMANDGUID);
  209. }
  210. }
  211. void CWiacapDlg::OnClickCapabilitiesListctrl(NMHDR* pNMHDR, LRESULT* pResult)
  212. {
  213. HD_NOTIFY* phdn = (HD_NOTIFY *) pNMHDR;
  214. if(IsCommand(phdn->iItem)){
  215. m_SendCommandButton.EnableWindow(TRUE);
  216. } else {
  217. m_SendCommandButton.EnableWindow(FALSE);
  218. }
  219. *pResult = 0;
  220. }
  221. BOOL CWiacapDlg::IsCommand(INT iItem)
  222. {
  223. // is an item selected?
  224. if (iItem < ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYNAME)
  225. return FALSE;
  226. TCHAR pszCapabilityType[MAX_PATH];
  227. TCHAR szCapTypeText[MAX_PATH];
  228. memset(pszCapabilityType,0,sizeof(pszCapabilityType));
  229. memset(szCapTypeText,0,sizeof(szCapTypeText));
  230. // load "Command" text label
  231. RC2TSTR(IDS_WIATESTCOMMAND,szCapTypeText,sizeof(szCapTypeText));
  232. m_CapablitiesListCtrl.GetItemText(iItem, ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYTYPE,
  233. pszCapabilityType, sizeof(pszCapabilityType));
  234. return (lstrcmpi(pszCapabilityType,szCapTypeText) == 0);
  235. }
  236. void CWiacapDlg::GetCommandGUID(INT iItem, GUID *pguid)
  237. {
  238. memset(pguid,0,sizeof(GUID));
  239. TCHAR szGUID[MAX_PATH];
  240. memset(szGUID,0,sizeof(szGUID));
  241. m_CapablitiesListCtrl.GetItemText(iItem, ITEMPROPERTYLISTCTRL_COLUMN_CAPABILITYVALUE,
  242. szGUID, sizeof(szGUID));
  243. #ifndef UNICODE
  244. UuidFromString((UCHAR*)szGUID,pguid);
  245. #else
  246. WideCharToMultiByte(CP_ACP, 0,szGUID,-1,szbuffer,MAX_PATH,NULL,NULL);
  247. UuidFromString((UCHAR*)szbuffer,pguid);
  248. #endif
  249. }
  250. void CWiacapDlg::OnItemchangedCapabilitiesListctrl(NMHDR* pNMHDR, LRESULT* pResult)
  251. {
  252. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  253. if(IsCommand(GetSelectedCapability())){
  254. m_SendCommandButton.EnableWindow(TRUE);
  255. } else {
  256. m_SendCommandButton.EnableWindow(FALSE);
  257. }
  258. *pResult = 0;
  259. }
  260. INT CWiacapDlg::GetSelectedCapability()
  261. {
  262. INT iItem = 0;
  263. POSITION pos = NULL;
  264. pos = m_CapablitiesListCtrl.GetFirstSelectedItemPosition();
  265. if (NULL != pos){
  266. iItem = m_CapablitiesListCtrl.GetNextSelectedItem(pos);
  267. }
  268. return iItem;
  269. }