Leaked source code of windows server 2003
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.

1511 lines
46 KiB

  1. // MSQSCANDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MSQSCAN.h"
  5. #include "MSQSCANDlg.h"
  6. #include "ProgressDlg.h"
  7. #include "uitables.h"
  8. #include "ADFDlg.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. #define PIXELS_PER_INCH_FACTOR 32
  15. #define PREVIEW_WINDOW_OFFSET 11
  16. DWORD g_dwCookie = 0;
  17. IGlobalInterfaceTable *g_pGIT = NULL;
  18. //
  19. // global UI lookup tables
  20. //
  21. extern WIA_FORMAT_TABLE_ENTRY g_WIA_FORMAT_TABLE[];
  22. extern WIA_DATATYPE_TABLE_ENTRY g_WIA_DATATYPE_TABLE[];
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CMSQSCANDlg dialog
  25. CMSQSCANDlg::CMSQSCANDlg(CWnd* pParent /*=NULL*/)
  26. : CDialog(CMSQSCANDlg::IDD, pParent)
  27. {
  28. //{{AFX_DATA_INIT(CMSQSCANDlg)
  29. m_MAX_Brightness = _T("");
  30. m_MAX_Contrast = _T("");
  31. m_MIN_Brightness = _T("");
  32. m_MIN_Contrast = _T("");
  33. m_XResolution = 0;
  34. m_YResolution = 0;
  35. //}}AFX_DATA_INIT
  36. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  37. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  38. m_DataAcquireInfo.bTransferToClipboard = FALSE;
  39. m_DataAcquireInfo.bPreview = TRUE;
  40. m_DataAcquireInfo.bTransferToFile = FALSE;
  41. m_DataAcquireInfo.dwCookie = 0;
  42. m_DataAcquireInfo.hBitmap = NULL;
  43. m_DataAcquireInfo.hClipboardData = NULL;
  44. m_DataAcquireInfo.pProgressFunc = NULL;
  45. m_DataAcquireInfo.hBitmapData = NULL;
  46. m_pConnectEventCB = NULL;
  47. }
  48. void CMSQSCANDlg::DoDataExchange(CDataExchange* pDX)
  49. {
  50. CDialog::DoDataExchange(pDX);
  51. //{{AFX_DATA_MAP(CMSQSCANDlg)
  52. DDX_Control(pDX, IDC_CHANGE_BOTH_CHECKBOX, m_ChangeBothResolutionsCheckBox);
  53. DDX_Control(pDX, IDC_EDIT_YRES_SPIN_BUDDY, m_YResolutionBuddy);
  54. DDX_Control(pDX, IDC_EDIT_XRES_SPIN_BUDDY, m_XResolutionBuddy);
  55. DDX_Control(pDX, IDC_SCAN_BUTTON, m_ScanButton);
  56. DDX_Control(pDX, IDC_PREVIEW_BUTTON, m_PreviewButton);
  57. DDX_Control(pDX, IDC_IMAGE_FILETYPE_COMBO, m_FileTypeComboBox);
  58. DDX_Control(pDX, IDC_DATATYPE_COMBO, m_DataTypeComboBox);
  59. DDX_Control(pDX, IDC_CONTRAST_SLIDER, m_ContrastSlider);
  60. DDX_Control(pDX, IDC_BRIGHTNESS_SLIDER, m_BrightnessSlider);
  61. DDX_Control(pDX, IDC_PREVIEW_WINDOW, m_PreviewRect);
  62. DDX_Text(pDX, IDC_MAX_BRIGHTNESS, m_MAX_Brightness);
  63. DDX_Text(pDX, IDC_MAX_CONTRAST, m_MAX_Contrast);
  64. DDX_Text(pDX, IDC_MIN_BRIGHTNESS, m_MIN_Brightness);
  65. DDX_Text(pDX, IDC_MIN_CONTRAST, m_MIN_Contrast);
  66. DDX_Text(pDX, IDC_EDIT_XRES, m_XResolution);
  67. DDX_Text(pDX, IDC_EDIT_YRES, m_YResolution);
  68. DDX_Control(pDX, IDC_DATA_TO_FILE, m_DataToFile);
  69. DDX_Control(pDX, IDC_DATA_TO_CLIPBOARD, m_DataToClipboard);
  70. //}}AFX_DATA_MAP
  71. }
  72. BEGIN_MESSAGE_MAP(CMSQSCANDlg, CDialog)
  73. //{{AFX_MSG_MAP(CMSQSCANDlg)
  74. ON_WM_PAINT()
  75. ON_WM_QUERYDRAGICON()
  76. ON_NOTIFY(UDN_DELTAPOS, IDC_EDIT_XRES_SPIN_BUDDY, OnDeltaposEditXresSpinBuddy)
  77. ON_NOTIFY(UDN_DELTAPOS, IDC_EDIT_YRES_SPIN_BUDDY, OnDeltaposEditYresSpinBuddy)
  78. ON_EN_SETFOCUS(IDC_EDIT_XRES, OnSetfocusEditXres)
  79. ON_EN_KILLFOCUS(IDC_EDIT_XRES, OnKillfocusEditXres)
  80. ON_EN_KILLFOCUS(IDC_EDIT_YRES, OnKillfocusEditYres)
  81. ON_EN_SETFOCUS(IDC_EDIT_YRES, OnSetfocusEditYres)
  82. ON_BN_CLICKED(IDC_SCAN_BUTTON, OnScanButton)
  83. ON_BN_CLICKED(IDC_PREVIEW_BUTTON, OnPreviewButton)
  84. ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
  85. ON_COMMAND(ID_FILE_SELECT_DEVICE, OnFileSelectDevice)
  86. ON_BN_CLICKED(IDC_ADF_SETTINGS, OnAdfSettings)
  87. //}}AFX_MSG_MAP
  88. END_MESSAGE_MAP()
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CMSQSCANDlg message handlers
  91. BOOL CMSQSCANDlg::OnInitDialog()
  92. {
  93. CDialog::OnInitDialog();
  94. // Set the icon for this dialog. The framework does this automatically
  95. // when the application's main window is not a dialog
  96. SetIcon(m_hIcon, TRUE); // Set big icon
  97. SetIcon(m_hIcon, FALSE); // Set small icon
  98. //
  99. // Set radio button setting (Data to File as DEFAULT setting)
  100. //
  101. m_DataToFile.SetCheck(1);
  102. m_DataAcquireInfo.hBitmap = NULL;
  103. OnFileSelectDevice();
  104. return TRUE; // return TRUE unless you set the focus to a control
  105. }
  106. // If you add a minimize button to your dialog, you will need the code below
  107. // to draw the icon. For MFC applications using the document/view model,
  108. // this is automatically done for you by the framework.
  109. void CMSQSCANDlg::OnPaint()
  110. {
  111. if (IsIconic()) {
  112. CPaintDC dc(this); // device context for painting
  113. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  114. // Center icon in client rectangle
  115. int cxIcon = GetSystemMetrics(SM_CXICON);
  116. int cyIcon = GetSystemMetrics(SM_CYICON);
  117. CRect rect;
  118. GetClientRect(&rect);
  119. int x = (rect.Width() - cxIcon + 1) / 2;
  120. int y = (rect.Height() - cyIcon + 1) / 2;
  121. // Draw the icon
  122. dc.DrawIcon(x, y, m_hIcon);
  123. }
  124. else {
  125. CDialog::OnPaint();
  126. }
  127. }
  128. // The system calls this to obtain the cursor to display while the user drags
  129. // the minimized window.
  130. HCURSOR CMSQSCANDlg::OnQueryDragIcon()
  131. {
  132. return (HCURSOR) m_hIcon;
  133. }
  134. /////////////////////////////////////////////////////////////////////////////
  135. // CMSQSCANDlg message handlers
  136. BOOL CMSQSCANDlg::InitDialogSettings()
  137. {
  138. //
  139. // fill common resolution combo box
  140. //
  141. if (InitResolutionEditBoxes()) {
  142. //
  143. // fill data type combo box
  144. //
  145. if (InitDataTypeComboBox()) {
  146. //
  147. // set min/max contrast slider control
  148. //
  149. if (InitContrastSlider()) {
  150. //
  151. // set min/max brightness slider control
  152. //
  153. if (InitBrightnessSlider()) {
  154. //
  155. // fill supported file type combo box
  156. //
  157. if (!InitFileTypeComboBox()) {
  158. return FALSE;
  159. }
  160. } else {
  161. MessageBox("Brightness Slider control failed to initialize");
  162. return FALSE;
  163. }
  164. } else {
  165. MessageBox("Contrast Slider control failed to initialize");
  166. return FALSE;
  167. }
  168. } else {
  169. MessageBox("Data Type combobox failed to initialize");
  170. return FALSE;
  171. }
  172. } else {
  173. MessageBox("Resolution edit boxes failed to initialize");
  174. return FALSE;
  175. }
  176. return TRUE;
  177. }
  178. BOOL CMSQSCANDlg::InitResolutionEditBoxes()
  179. {
  180. //
  181. // Set buddy controls to their "buddy"
  182. //
  183. LONG lMin = 0;
  184. LONG lMax = 0;
  185. LONG lCurrent = 0;
  186. HRESULT hr = S_OK;
  187. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_XRES,WIA_RANGE_MIN,&lMin);
  188. if (SUCCEEDED(hr)) {
  189. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_XRES,WIA_RANGE_MAX,&lMax);
  190. if (SUCCEEDED(hr)) {
  191. hr = m_WIA.ReadLong(NULL,WIA_IPS_XRES,&lCurrent);
  192. if (FAILED(hr)) {
  193. MessageBox("Application Failed to read x resolution (Min Setting)");
  194. return FALSE;
  195. }
  196. } else {
  197. MessageBox("Application Failed to read x resolution (Max Setting)");
  198. return FALSE;
  199. }
  200. } else {
  201. MessageBox("Application Failed to read x resolution (Current Setting)");
  202. return FALSE;
  203. }
  204. m_XResolutionBuddy.SetBuddy(GetDlgItem(IDC_EDIT_XRES));
  205. m_XResolutionBuddy.SetRange(lMin,lMax);
  206. m_XResolutionBuddy.SetPos(lCurrent);
  207. m_XResolution = m_XResolutionBuddy.GetPos();
  208. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_XRES,WIA_RANGE_MIN,&lMin);
  209. if (SUCCEEDED(hr)) {
  210. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_XRES,WIA_RANGE_MAX,&lMax);
  211. if (SUCCEEDED(hr)) {
  212. hr = m_WIA.ReadLong(NULL,WIA_IPS_XRES,&lCurrent);
  213. if (FAILED(hr)) {
  214. MessageBox("Application Failed to read y resolution (Min Setting)");
  215. return FALSE;
  216. }
  217. } else {
  218. MessageBox("Application Failed to read y resolution (Max Setting)");
  219. return FALSE;
  220. }
  221. } else {
  222. MessageBox("Application Failed to read y resolution (Current Setting)");
  223. return FALSE;
  224. }
  225. m_YResolutionBuddy.SetBuddy(GetDlgItem(IDC_EDIT_YRES));
  226. m_YResolutionBuddy.SetRange(lMin,lMax);
  227. m_YResolutionBuddy.SetPos(lCurrent);
  228. m_YResolution = m_YResolutionBuddy.GetPos();
  229. //
  230. // set current selection, to be scanner's current setting
  231. //
  232. UpdateData(FALSE);
  233. //
  234. // check 'change both resolutions' check box
  235. //
  236. m_ChangeBothResolutionsCheckBox.SetCheck(1);
  237. return TRUE;
  238. }
  239. BOOL CMSQSCANDlg::InitDataTypeComboBox()
  240. {
  241. //
  242. // reset data type combo box
  243. //
  244. m_DataTypeComboBox.ResetContent();
  245. //
  246. // set current selection, to be scanner's current setting
  247. //
  248. //
  249. // Below is a hard coded supported data type list. This should be obtained from the
  250. // device itself. (ie. some scanners may not support color..)
  251. // This was done for testing purposes.
  252. //
  253. ULONG ulCount = 3;
  254. TCHAR szDataType[MAX_PATH];
  255. LONG plDataType[3] = {
  256. WIA_DATA_THRESHOLD,
  257. WIA_DATA_COLOR,
  258. WIA_DATA_GRAYSCALE
  259. };
  260. for(ULONG index = 0;index < ulCount;index++) {
  261. //
  262. // add data type to combo box
  263. //
  264. INT TableIndex = GetIDAndStringFromDataType(plDataType[index],szDataType);
  265. INT InsertIndex = m_DataTypeComboBox.AddString(szDataType);
  266. m_DataTypeComboBox.SetItemData(InsertIndex, TableIndex);
  267. }
  268. return TRUE;
  269. }
  270. BOOL CMSQSCANDlg::InitContrastSlider()
  271. {
  272. LONG lMin = 0;
  273. LONG lMax = 0;
  274. LONG lCurrent = 0;
  275. HRESULT hr = S_OK;
  276. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_CONTRAST,WIA_RANGE_MIN,&lMin);
  277. if(SUCCEEDED(hr)){
  278. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_CONTRAST,WIA_RANGE_MAX,&lMax);
  279. if(SUCCEEDED(hr)) {
  280. hr = m_WIA.ReadLong(NULL,WIA_IPS_CONTRAST,&lCurrent);
  281. if(FAILED(hr)){
  282. MessageBox("Application Failed to read contrast (Current Setting)");
  283. return FALSE;
  284. }
  285. } else {
  286. MessageBox("Application Failed to read contrast (Max Setting)");
  287. return FALSE;
  288. }
  289. }else {
  290. MessageBox("Application Failed to read contrast (Min Setting)");
  291. return FALSE;
  292. }
  293. m_ContrastSlider.SetRange(lMin,lMax,TRUE);
  294. m_ContrastSlider.SetPos(lCurrent);
  295. m_ContrastSlider.SetTicFreq(lMax/11);
  296. m_MIN_Contrast.Format("%d",lMin);
  297. m_MAX_Contrast.Format("%d",lMax);
  298. UpdateData(FALSE);
  299. //
  300. // set current selection, to be scanner's current setting
  301. //
  302. m_DataTypeComboBox.SetCurSel(0);
  303. return TRUE;
  304. }
  305. BOOL CMSQSCANDlg::InitBrightnessSlider()
  306. {
  307. LONG lMin = 0;
  308. LONG lMax = 0;
  309. LONG lCurrent = 0;
  310. HRESULT hr = S_OK;
  311. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_BRIGHTNESS,WIA_RANGE_MIN,&lMin);
  312. if (SUCCEEDED(hr)) {
  313. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_BRIGHTNESS,WIA_RANGE_MAX,&lMax);
  314. if (SUCCEEDED(hr)) {
  315. hr = m_WIA.ReadLong(NULL,WIA_IPS_BRIGHTNESS,&lCurrent);
  316. if (FAILED(hr)) {
  317. MessageBox("Application Failed to read brightness (Current Setting)");
  318. return FALSE;
  319. }
  320. } else {
  321. MessageBox("Application Failed to read brightness (Max Setting)");
  322. return FALSE;
  323. }
  324. } else {
  325. MessageBox("Application Failed to read brightness (Min Setting)");
  326. return FALSE;
  327. }
  328. m_BrightnessSlider.SetRange(lMin,lMax,TRUE);
  329. m_BrightnessSlider.SetPos(lCurrent);
  330. m_BrightnessSlider.SetTicFreq(lMax/11);
  331. m_MIN_Brightness.Format("%d",lMin);
  332. m_MAX_Brightness.Format("%d",lMax);
  333. UpdateData(FALSE);
  334. //
  335. // set current selection, to be scanner's current setting
  336. //
  337. return TRUE;
  338. }
  339. BOOL CMSQSCANDlg::InitFileTypeComboBox()
  340. {
  341. //
  342. // reset file type combo box
  343. //
  344. m_FileTypeComboBox.ResetContent();
  345. HRESULT hr = S_OK;
  346. TCHAR szguidFormat[MAX_PATH];
  347. //
  348. // set current selection, to be scanner's current setting
  349. //
  350. //
  351. // enumerate supported file types
  352. //
  353. WIA_FORMAT_INFO *pSupportedFormats = NULL;
  354. ULONG ulCount = 0;
  355. hr = m_WIA.EnumerateSupportedFormats(NULL, &pSupportedFormats, &ulCount);
  356. if(SUCCEEDED(hr)) {
  357. //
  358. // filter out TYMED_FILE formats only
  359. //
  360. for(ULONG index = 0;index < ulCount;index++) {
  361. if(pSupportedFormats[index].lTymed == TYMED_FILE) {
  362. //
  363. // add supported file format to combo box
  364. //
  365. INT TableIndex = GetIDAndStringFromGUID(pSupportedFormats[index].guidFormatID,szguidFormat);
  366. INT InsertIndex = m_FileTypeComboBox.AddString(szguidFormat);
  367. m_FileTypeComboBox.SetItemData(InsertIndex, TableIndex);
  368. }
  369. }
  370. //
  371. // free the memory allocated by the CWIA call
  372. //
  373. GlobalFree(pSupportedFormats);
  374. m_FileTypeComboBox.SetCurSel(0);
  375. return TRUE;
  376. }
  377. return FALSE;
  378. }
  379. BOOL CMSQSCANDlg::SetDeviceNameToWindowTitle(BSTR bstrDeviceName)
  380. {
  381. //
  382. // convert BSTR to a CString
  383. //
  384. CString DeviceName = bstrDeviceName;
  385. //
  386. // write the new title to the window
  387. //
  388. SetWindowText("Microsoft Quick Scan: [ " + DeviceName + " ]");
  389. return TRUE;
  390. }
  391. void CMSQSCANDlg::OnDeltaposEditXresSpinBuddy(NMHDR* pNMHDR, LRESULT* pResult)
  392. {
  393. NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
  394. if(m_ChangeBothResolutionsCheckBox.GetCheck() == 1) {
  395. m_YResolution = (pNMUpDown->iPos + pNMUpDown->iDelta);
  396. UpdateData(FALSE);
  397. }
  398. *pResult = 0;
  399. }
  400. void CMSQSCANDlg::OnDeltaposEditYresSpinBuddy(NMHDR* pNMHDR, LRESULT* pResult)
  401. {
  402. NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
  403. if(m_ChangeBothResolutionsCheckBox.GetCheck() == 1) {
  404. m_XResolution = (pNMUpDown->iPos + pNMUpDown->iDelta);
  405. UpdateData(FALSE);
  406. }
  407. *pResult = 0;
  408. }
  409. void CMSQSCANDlg::OnSetfocusEditXres()
  410. {
  411. UpdateData(TRUE);
  412. if(m_ChangeBothResolutionsCheckBox.GetCheck() == 1) {
  413. m_XResolution = m_YResolution;
  414. UpdateData(FALSE);
  415. }
  416. }
  417. void CMSQSCANDlg::OnKillfocusEditXres()
  418. {
  419. UpdateData(TRUE);
  420. if(m_ChangeBothResolutionsCheckBox.GetCheck() == 1) {
  421. m_YResolution = m_XResolution;
  422. UpdateData(FALSE);
  423. }
  424. }
  425. void CMSQSCANDlg::OnKillfocusEditYres()
  426. {
  427. UpdateData(TRUE);
  428. if(m_ChangeBothResolutionsCheckBox.GetCheck() == 1) {
  429. m_XResolution = m_YResolution;
  430. UpdateData(FALSE);
  431. }
  432. }
  433. void CMSQSCANDlg::OnSetfocusEditYres()
  434. {
  435. UpdateData(TRUE);
  436. if(m_ChangeBothResolutionsCheckBox.GetCheck() == 1) {
  437. m_XResolution = m_YResolution;
  438. UpdateData(FALSE);
  439. }
  440. }
  441. void CMSQSCANDlg::OnScanButton()
  442. {
  443. memset(m_DataAcquireInfo.szFileName,0,sizeof(m_DataAcquireInfo.szFileName));
  444. if(m_DataToFile.GetCheck() == 1) {
  445. //
  446. // scan to file
  447. //
  448. m_DataAcquireInfo.bTransferToFile = TRUE;
  449. //
  450. // allow user to set the file name
  451. //
  452. CHAR szFilter[256];
  453. memset(szFilter,0,sizeof(szFilter));
  454. CFileDialog FileDialog(FALSE);
  455. //
  456. // Get filter from selected combobox (file type)
  457. //
  458. INT CurrentSelection = m_FileTypeComboBox.GetCurSel();
  459. m_FileTypeComboBox.GetLBText(CurrentSelection, szFilter);
  460. FileDialog.m_ofn.lpstrFilter = szFilter;
  461. //
  462. // Show the SaveAs dialog to user
  463. //
  464. if(FileDialog.DoModal() == IDOK) {
  465. //
  466. // save user selected filename
  467. //
  468. strcpy(m_DataAcquireInfo.szFileName,FileDialog.m_ofn.lpstrFile);
  469. DeleteFile(m_DataAcquireInfo.szFileName);
  470. } else {
  471. //
  472. // do nothing... the user decided not to enter a file name
  473. //
  474. return;
  475. }
  476. } else {
  477. //
  478. // scan to clipboard
  479. //
  480. m_DataAcquireInfo.bTransferToFile = FALSE;
  481. m_DataAcquireInfo.bTransferToClipboard = TRUE;
  482. }
  483. //
  484. // Write settings from dialog to device
  485. //
  486. if(WriteScannerSettingsToDevice()) {
  487. ADF_SETTINGS ADFSettings;
  488. if(SUCCEEDED(ReadADFSettings(&ADFSettings))){
  489. //
  490. // check file type, and warn user about BMP files.
  491. //
  492. CHAR szFormat[256];
  493. memset(szFormat,0,sizeof(szFormat));
  494. INT FILEFORMAT = m_FileTypeComboBox.GetCurSel();
  495. m_FileTypeComboBox.GetLBText(FILEFORMAT, szFormat);
  496. if (NULL != strstr(szFormat,"BMP")) {
  497. if(ADFSettings.lPages > 1) {
  498. MessageBox(TEXT("BMP Files will only save the last page scanned, because there\nis no Multi-page BMP file format."),TEXT("BMP File Format Warning"),MB_ICONWARNING);
  499. } else if(ADFSettings.lPages == 0) {
  500. MessageBox(TEXT("BMP Files will only save the last page scanned, because there\nis no Multi-page BMP file format."),TEXT("BMP File Format Warning"),MB_ICONWARNING);
  501. }
  502. }
  503. }
  504. //
  505. // create progress dialog object
  506. //
  507. CProgressDlg ProgDlg(this);
  508. //
  509. // set preview flag, and data acquire information
  510. //
  511. m_DataAcquireInfo.bPreview = FALSE; // this is a 'final' scan
  512. ProgDlg.SetAcquireData(&m_DataAcquireInfo);
  513. //
  514. // activate scan progress dialog
  515. //
  516. ProgDlg.DoModal();
  517. if(m_DataAcquireInfo.bTransferToClipboard ) {
  518. //
  519. // Put memory on clipboard
  520. //
  521. PutDataOnClipboard();
  522. m_DataAcquireInfo.bTransferToClipboard = FALSE;
  523. }
  524. }
  525. }
  526. void CMSQSCANDlg::OnPreviewButton()
  527. {
  528. memset(m_DataAcquireInfo.szFileName,0,sizeof(m_DataAcquireInfo.szFileName));
  529. m_DataAcquireInfo.bTransferToFile = FALSE;
  530. //
  531. // Write settings from dialog to device
  532. //
  533. if(WriteScannerSettingsToDevice(TRUE)) {
  534. //
  535. // create progress dialog object
  536. //
  537. CProgressDlg ProgDlg(this);
  538. //
  539. // set preview flag, and data acquire information
  540. //
  541. m_DataAcquireInfo.bPreview = TRUE; // this is a 'preview' scan
  542. if(m_DataAcquireInfo.hBitmapData != NULL) {
  543. GlobalUnlock(m_DataAcquireInfo.hBitmapData);
  544. //
  545. // free previous preview scan
  546. //
  547. GlobalFree(m_DataAcquireInfo.hBitmapData);
  548. m_DataAcquireInfo.hBitmapData = NULL;
  549. }
  550. ProgDlg.SetAcquireData(&m_DataAcquireInfo);
  551. //
  552. // activate scan progress dialog
  553. //
  554. ProgDlg.DoModal();
  555. Invalidate();
  556. }
  557. }
  558. INT CMSQSCANDlg::GetIDAndStringFromGUID(GUID guidFormat, TCHAR *pszguidString)
  559. {
  560. INT index = 0;
  561. while(*(g_WIA_FORMAT_TABLE[index].pguidFormat) != guidFormat && index < NUM_WIA_FORMAT_INFO_ENTRIES) {
  562. index++;
  563. }
  564. if(index > NUM_WIA_FORMAT_INFO_ENTRIES)
  565. index = NUM_WIA_FORMAT_INFO_ENTRIES;
  566. lstrcpy(pszguidString, g_WIA_FORMAT_TABLE[index].szFormatName);
  567. return index;
  568. }
  569. GUID CMSQSCANDlg::GetGuidFromID(INT iID)
  570. {
  571. return *(g_WIA_FORMAT_TABLE[iID].pguidFormat);
  572. }
  573. LRESULT CMSQSCANDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  574. {
  575. if(message == WM_UPDATE_PREVIEW) {
  576. m_PreviewWindow.SetHBITMAP(m_DataAcquireInfo.hBitmap);
  577. }
  578. return CDialog::WindowProc(message, wParam, lParam);
  579. }
  580. INT CMSQSCANDlg::GetIDAndStringFromDataType(LONG lDataType, TCHAR *pszString)
  581. {
  582. INT index = 0;
  583. while(g_WIA_DATATYPE_TABLE[index].lDataType != lDataType && index < NUM_WIA_DATATYPE_ENTRIES) {
  584. index++;
  585. }
  586. if(index > NUM_WIA_DATATYPE_ENTRIES)
  587. index = NUM_WIA_DATATYPE_ENTRIES;
  588. lstrcpy(pszString, g_WIA_DATATYPE_TABLE[index].szDataTypeName);
  589. return index;
  590. }
  591. LONG CMSQSCANDlg::GetDataTypeFromID(INT iID)
  592. {
  593. return (g_WIA_DATATYPE_TABLE[iID].lDataType);
  594. }
  595. BOOL CMSQSCANDlg::WriteScannerSettingsToDevice(BOOL bPreview)
  596. {
  597. HRESULT hr = S_OK;
  598. int SelectionIndex = 0;
  599. int TableIndex = 0;
  600. SelectionIndex = m_DataTypeComboBox.GetCurSel();
  601. TableIndex = (int)m_DataTypeComboBox.GetItemData(SelectionIndex);
  602. //
  603. // Set data type
  604. //
  605. hr = m_WIA.WriteLong(NULL,WIA_IPA_DATATYPE,GetDataTypeFromID(TableIndex));
  606. if(SUCCEEDED(hr)){
  607. LONG lBrightness = m_BrightnessSlider.GetPos();
  608. //
  609. // Set Brightness
  610. //
  611. hr = m_WIA.WriteLong(NULL,WIA_IPS_BRIGHTNESS,lBrightness);
  612. if(SUCCEEDED(hr)){
  613. LONG lContrast = m_ContrastSlider.GetPos();
  614. //
  615. // Set Contrast
  616. //
  617. hr = m_WIA.WriteLong(NULL,WIA_IPS_CONTRAST,lContrast);
  618. if(FAILED(hr)) {
  619. MessageBox("Application Failed to set Data Type");
  620. return FALSE;
  621. }
  622. } else {
  623. MessageBox("Application Failed to set Brightness");
  624. return FALSE;
  625. }
  626. } else {
  627. MessageBox("Application Failed to set Data Type");
  628. return FALSE;
  629. }
  630. //
  631. // Reset selection rect, to be full bed.
  632. // This is good to do if you want a solid starting
  633. // place, for extent setting
  634. //
  635. if(!ResetWindowExtents()) {
  636. return FALSE;
  637. }
  638. if(bPreview) {
  639. //
  640. // set to preview X resolution
  641. //
  642. hr = m_WIA.WriteLong(NULL,WIA_IPS_XRES,PREVIEW_RES);
  643. if(SUCCEEDED(hr)) {
  644. //
  645. // set to preview Y resolution
  646. //
  647. hr = m_WIA.WriteLong(NULL,WIA_IPS_YRES,PREVIEW_RES);
  648. if(SUCCEEDED(hr)) {
  649. //
  650. // set to memory bitmap for preview display
  651. //
  652. hr = m_WIA.WriteGuid(NULL,WIA_IPA_FORMAT,WiaImgFmt_MEMORYBMP);
  653. if(FAILED(hr)) {
  654. MessageBox("Application Failed to set format to Memory BMP");
  655. return FALSE;
  656. }
  657. } else {
  658. MessageBox("Application Failed to set Y Resolution (Preview)");
  659. return FALSE;
  660. }
  661. } else {
  662. MessageBox("Application Failed to set X Resolution (Preview)");
  663. return FALSE;
  664. }
  665. } else {
  666. //
  667. // Are we scanning to the clipboard??
  668. //
  669. if(m_DataAcquireInfo.bTransferToClipboard ) {
  670. //
  671. // Do only Banded transfer, and WiaImgFmt_MEMORYBMP for clipboard
  672. // transfers. (This application can only do this function using
  673. // those specific settings).
  674. // Note: Other applications are not restricted by this. This is
  675. // is a design issue with this sample only!!!
  676. //
  677. //
  678. // set to memory bitmap for clipboard scanning
  679. //
  680. hr = m_WIA.WriteGuid(NULL,WIA_IPA_FORMAT,WiaImgFmt_MEMORYBMP);
  681. if(FAILED(hr)) {
  682. MessageBox("Memory BMP could not be set to Device");
  683. return FALSE;
  684. }
  685. }
  686. //
  687. // write dialog setting for resolution
  688. //
  689. UpdateData(TRUE);
  690. //
  691. // set X resolution
  692. //
  693. hr = m_WIA.WriteLong(NULL,WIA_IPS_XRES,m_XResolution);
  694. if(SUCCEEDED(hr)) {
  695. //
  696. // set Y resolution
  697. //
  698. hr = m_WIA.WriteLong(NULL,WIA_IPS_YRES,m_YResolution);
  699. if(FAILED(hr)) {
  700. MessageBox("Application Failed to set Y Resolution (Preview)");
  701. return FALSE;
  702. }
  703. } else {
  704. MessageBox("Application Failed to set X Resolution (Preview)");
  705. return FALSE;
  706. }
  707. //
  708. // write extent values
  709. //
  710. CRect SelectionRect;
  711. m_PreviewWindow.GetSelectionRect(SelectionRect);
  712. CRect PreviewRect;
  713. m_PreviewWindow.GetWindowRect(PreviewRect);
  714. LONG lXPos = 0;
  715. LONG lYPos = 0;
  716. LONG lMaxXExtent = 0;
  717. LONG lMaxYExtent = 0;
  718. LONG lXExtent = 0;
  719. LONG lYExtent = 0;
  720. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_XEXTENT,WIA_RANGE_MAX,&lMaxXExtent);
  721. if(SUCCEEDED(hr)) {
  722. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_YEXTENT,WIA_RANGE_MAX,&lMaxYExtent);
  723. if(FAILED(hr)) {
  724. MessageBox("Application failed to read y extent (Max value)");
  725. return FALSE;
  726. }
  727. } else {
  728. MessageBox("Application failed to read x extent (Max value)");
  729. return FALSE;
  730. }
  731. FLOAT fxRatio = ((FLOAT)lMaxXExtent/(FLOAT)PreviewRect.Width());
  732. FLOAT fyRatio = ((FLOAT)lMaxYExtent/(FLOAT)PreviewRect.Height());
  733. lXPos = (LONG)(SelectionRect.left * fxRatio);
  734. lYPos = (LONG)(SelectionRect.top * fyRatio);
  735. lXExtent = (LONG)(SelectionRect.Width() * fxRatio);
  736. lYExtent = (LONG)(SelectionRect.Height() * fyRatio);
  737. hr = m_WIA.WriteLong(NULL,WIA_IPS_XPOS,lXPos);
  738. if(SUCCEEDED(hr)) {
  739. hr = m_WIA.WriteLong(NULL,WIA_IPS_YPOS,lYPos);
  740. if(SUCCEEDED(hr)) {
  741. hr = m_WIA.WriteLong(NULL,WIA_IPS_XEXTENT,lXExtent);
  742. if(SUCCEEDED(hr)) {
  743. hr = m_WIA.WriteLong(NULL,WIA_IPS_YEXTENT,lYExtent);
  744. if(FAILED(hr)){
  745. MessageBox("Application failed to set Y Extent");
  746. return FALSE;
  747. }
  748. } else {
  749. MessageBox("Application failed to set X Extent");
  750. return FALSE;
  751. }
  752. }else {
  753. MessageBox("Application failed to set Y Position");
  754. return FALSE;
  755. }
  756. } else {
  757. MessageBox("Application failed to set X Position");
  758. return FALSE;
  759. }
  760. }
  761. return TRUE;
  762. }
  763. void CMSQSCANDlg::OnFileClose()
  764. {
  765. CDialog::OnOK();
  766. }
  767. void CMSQSCANDlg::OnFileSelectDevice()
  768. {
  769. CRect DialogClientRect;
  770. GetClientRect(DialogClientRect);
  771. //
  772. // use scan window place holder, as template for placing the scan
  773. // preview window
  774. //
  775. CRect WindowRect;
  776. m_PreviewRect.GetWindowRect(WindowRect);
  777. ScreenToClient(WindowRect);
  778. HRESULT hr = S_OK;
  779. hr = CoCreateInstance(CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER,
  780. IID_IGlobalInterfaceTable,(void**)&g_pGIT);
  781. if(SUCCEEDED(hr)) {
  782. hr = CoCreateInstance(CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER,
  783. IID_IWiaDevMgr,(void**)&m_pIWiaDevMgr);
  784. if (SUCCEEDED(hr)) {
  785. //
  786. // An example on how to register for Device Connection Events
  787. //
  788. m_pConnectEventCB = new CEventCallback;
  789. if (m_pConnectEventCB) {
  790. IWiaEventCallback* pIWiaEventCallback = NULL;
  791. IUnknown* pIUnkRelease;
  792. // register connected event
  793. m_pConnectEventCB->Initialize(ID_WIAEVENT_CONNECT);
  794. m_pConnectEventCB->QueryInterface(IID_IWiaEventCallback,(void **)&pIWiaEventCallback);
  795. GUID guidConnect = WIA_EVENT_DEVICE_CONNECTED;
  796. hr = m_pIWiaDevMgr->RegisterEventCallbackInterface(WIA_REGISTER_EVENT_CALLBACK,
  797. NULL,
  798. &guidConnect,
  799. pIWiaEventCallback,
  800. &pIUnkRelease);
  801. m_pConnectEventCB->m_pIUnkRelease = pIUnkRelease;
  802. //
  803. // An Example on how to register for events with this application
  804. //
  805. WCHAR szMyApplicationLaunchPath[MAX_PATH];
  806. memset(szMyApplicationLaunchPath,0,sizeof(szMyApplicationLaunchPath));
  807. GetModuleFileNameW(NULL,szMyApplicationLaunchPath,sizeof(szMyApplicationLaunchPath));
  808. BSTR bstrMyApplicationLaunchPath = SysAllocString(szMyApplicationLaunchPath);
  809. WCHAR szMyApplicationName[MAX_PATH];
  810. memset(szMyApplicationName,0,sizeof(szMyApplicationName));
  811. HINSTANCE hInst = AfxGetInstanceHandle();
  812. if(hInst){
  813. LoadStringW(hInst, IDS_MYAPPLICATION_NAME, szMyApplicationName, (sizeof(szMyApplicationName)/sizeof(WCHAR)));
  814. BSTR bstrMyApplicationName = SysAllocString(szMyApplicationName);
  815. GUID guidScanButtonEvent = WIA_EVENT_SCAN_IMAGE;
  816. hr = m_pIWiaDevMgr->RegisterEventCallbackProgram(
  817. WIA_REGISTER_EVENT_CALLBACK,
  818. NULL,
  819. &guidScanButtonEvent,
  820. bstrMyApplicationLaunchPath,
  821. bstrMyApplicationName,
  822. bstrMyApplicationName,
  823. bstrMyApplicationLaunchPath);
  824. if(FAILED(hr)){
  825. MessageBox("Could not Register Application for Events");
  826. hr = S_OK; // continue and try to use device
  827. }
  828. SysFreeString(bstrMyApplicationName);
  829. bstrMyApplicationName = NULL;
  830. }
  831. SysFreeString(bstrMyApplicationLaunchPath);
  832. bstrMyApplicationLaunchPath = NULL;
  833. }
  834. if (SUCCEEDED(hr)) {
  835. //
  836. // select your scanning device here
  837. //
  838. IWiaItem *pIWiaRootItem = NULL;
  839. hr = m_pIWiaDevMgr->SelectDeviceDlg(m_hWnd,StiDeviceTypeScanner,0,NULL,&pIWiaRootItem);
  840. if (hr == S_OK) {
  841. //
  842. // Write interface to Global Interface Table
  843. //
  844. hr = WriteInterfaceToGlobalInterfaceTable(&m_DataAcquireInfo.dwCookie,
  845. pIWiaRootItem);
  846. if (SUCCEEDED(hr)) {
  847. //
  848. // save root item (device) created
  849. //
  850. m_WIA.SetRootItem(pIWiaRootItem);
  851. //
  852. // query selected device for it's name
  853. //
  854. BSTR bstrDeviceName;
  855. hr = m_WIA.ReadStr(pIWiaRootItem,WIA_DIP_DEV_NAME,&bstrDeviceName);
  856. if (SUCCEEDED(hr)) {
  857. SetDeviceNameToWindowTitle(bstrDeviceName);
  858. SysFreeString(bstrDeviceName);
  859. }
  860. //
  861. // query selected device for scanner bed size, so we can create
  862. // a scanner preview window
  863. //
  864. LONG MaxScanBedWidth = 0;
  865. LONG MaxScanBedHeight = 0;
  866. FLOAT fRatio = 0;
  867. FLOAT fXFactor = 0.0f;
  868. FLOAT fYFactor = 0.0f;
  869. FLOAT fTheFactor = 0.0f;
  870. m_WIA.ReadLong(pIWiaRootItem,WIA_DPS_HORIZONTAL_BED_SIZE,&MaxScanBedWidth);
  871. m_WIA.ReadLong(pIWiaRootItem,WIA_DPS_VERTICAL_BED_SIZE,&MaxScanBedHeight);
  872. fRatio = (FLOAT)((FLOAT)MaxScanBedHeight / (FLOAT)MaxScanBedWidth);
  873. fXFactor = (FLOAT)WindowRect.Width()/(FLOAT)MaxScanBedWidth;
  874. fYFactor = (FLOAT)WindowRect.Height()/(FLOAT)MaxScanBedHeight;
  875. if (fXFactor > fYFactor)
  876. fTheFactor = fYFactor;
  877. else
  878. fTheFactor = fXFactor;
  879. //
  880. // adjust the pixel returned size so it will fit on the dialog correctly
  881. //
  882. WindowRect.right = (LONG)(fTheFactor * MaxScanBedWidth) + WindowRect.left;
  883. WindowRect.bottom = (LONG)(fTheFactor * MaxScanBedHeight) + WindowRect.top;
  884. //WindowRect.right = (MaxScanBedWidth/PIXELS_PER_INCH_FACTOR);
  885. //WindowRect.bottom = (LONG)(WindowRect.right * fRatio);
  886. //
  887. // check scanner bed size, against actual window size, and adjust
  888. //
  889. if (DialogClientRect.bottom < WindowRect.bottom) {
  890. CRect DialogRect;
  891. GetWindowRect(DialogRect);
  892. DialogRect.InflateRect(0,0,0,(WindowRect.bottom - DialogClientRect.bottom) + 10);
  893. MoveWindow(DialogRect);
  894. }
  895. } else {
  896. MessageBox("Failed to Set IWiaRootItem Interface in to Global Interface Table");
  897. }
  898. } else {
  899. MessageBox("No Scanner was selected.");
  900. return;
  901. }
  902. } else {
  903. MessageBox("Could not Register for Device Disconnect Events");
  904. }
  905. m_PreviewWindow.DestroyWindow();
  906. //
  907. // create the preview window
  908. //
  909. if(!m_PreviewWindow.Create(NULL,
  910. TEXT("Preview Window"),
  911. WS_CHILD|WS_VISIBLE,
  912. WindowRect,
  913. this,
  914. PREVIEW_WND_ID)){
  915. MessageBox("Failed to create preview window");
  916. return;
  917. }
  918. m_DataAcquireInfo.hWnd = m_PreviewWindow.m_hWnd;
  919. //
  920. // intialize selection rect to entire bed for preview
  921. //
  922. m_PreviewWindow.SetPreviewRect(WindowRect);
  923. //
  924. // InitDialogSettings
  925. //
  926. InitDialogSettings();
  927. } else {
  928. MessageBox("CoCreateInstance for WIA Device Manager failed");
  929. return;
  930. }
  931. } else {
  932. MessageBox("CoCreateInstance for Global Interface Table failed");
  933. return;
  934. }
  935. }
  936. BOOL CMSQSCANDlg::PutDataOnClipboard()
  937. {
  938. BOOL bSuccess = FALSE;
  939. if(OpenClipboard()){
  940. if(EmptyClipboard()){
  941. BYTE* pbBuf = (BYTE*)GlobalLock(m_DataAcquireInfo.hClipboardData);
  942. VerticalFlip(pbBuf);
  943. GlobalUnlock(m_DataAcquireInfo.hClipboardData);
  944. if(SetClipboardData(CF_DIB, m_DataAcquireInfo.hClipboardData) == NULL) {
  945. MessageBox("SetClipboardData failed");
  946. } else {
  947. //
  948. // We succeeded to give memory handle to clipboard
  949. //
  950. bSuccess = TRUE;
  951. }
  952. } else {
  953. MessageBox("EmptyClipboard failed");
  954. }
  955. if (!CloseClipboard()) {
  956. MessageBox("CloseClipboard failed");
  957. }
  958. } else {
  959. MessageBox("OpenClipboard failed");
  960. }
  961. if(!bSuccess) {
  962. //
  963. // Free the memory ourselves, because the Clipboard failed to accept it.
  964. //
  965. GlobalFree(m_DataAcquireInfo.hClipboardData);
  966. }
  967. //
  968. // Set handle to NULL, to mark it fresh when scanning more data.
  969. // This handle is now owned by the Clipbpard...so freeing it would be a bad idea.
  970. //
  971. m_DataAcquireInfo.hClipboardData = NULL;
  972. return bSuccess;
  973. }
  974. VOID CMSQSCANDlg::VerticalFlip(BYTE *pBuf)
  975. {
  976. HRESULT hr = S_OK;
  977. LONG lHeight;
  978. LONG lWidth;
  979. BITMAPINFOHEADER *pbmih;
  980. PBYTE pTop = NULL;
  981. PBYTE pBottom = NULL;
  982. pbmih = (BITMAPINFOHEADER*) pBuf;
  983. if (pbmih->biHeight > 0) {
  984. return;
  985. }
  986. pTop = pBuf + pbmih->biSize + ((pbmih->biClrUsed) * sizeof(RGBQUAD));
  987. lWidth = ((pbmih->biWidth * pbmih->biBitCount + 31) / 32) * 4;
  988. pbmih->biHeight = abs(pbmih->biHeight);
  989. lHeight = pbmih->biHeight;
  990. PBYTE pTempBuffer = (PBYTE)LocalAlloc(LPTR, lWidth);
  991. if (pTempBuffer) {
  992. LONG index = 0;
  993. pBottom = pTop + (lHeight-1) * lWidth;
  994. for (index = 0;index < (lHeight/2);index++) {
  995. memcpy(pTempBuffer, pTop, lWidth);
  996. memcpy(pTop, pBottom, lWidth);
  997. memcpy(pBottom,pTempBuffer, lWidth);
  998. pTop += lWidth;
  999. pBottom -= lWidth;
  1000. }
  1001. LocalFree(pTempBuffer);
  1002. }
  1003. }
  1004. BOOL CMSQSCANDlg::ResetWindowExtents()
  1005. {
  1006. LONG lMaxXExtent = 0;
  1007. LONG lMaxYExtent = 0;
  1008. HRESULT hr = S_OK;
  1009. hr = m_WIA.WriteLong(NULL,WIA_IPS_XPOS,0);
  1010. if(SUCCEEDED(hr)) {
  1011. hr = m_WIA.WriteLong(NULL,WIA_IPS_YPOS,0);
  1012. if(SUCCEEDED(hr)){
  1013. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_XEXTENT,WIA_RANGE_MAX,&lMaxXExtent);
  1014. if(SUCCEEDED(hr)){
  1015. hr = m_WIA.ReadRangeLong(NULL,WIA_IPS_YEXTENT,WIA_RANGE_MAX,&lMaxYExtent);
  1016. if(SUCCEEDED(hr)){
  1017. hr = m_WIA.WriteLong(NULL,WIA_IPS_XEXTENT,lMaxXExtent);
  1018. if(SUCCEEDED(hr)){
  1019. hr = m_WIA.WriteLong(NULL,WIA_IPS_YEXTENT,lMaxYExtent);
  1020. if(FAILED(hr)) {
  1021. MessageBox("Application failed to write y extent");
  1022. return FALSE;
  1023. }
  1024. } else {
  1025. MessageBox("Application failed to write x extent");
  1026. return FALSE;
  1027. }
  1028. } else {
  1029. MessageBox("Application failed to read y extent");
  1030. return FALSE;
  1031. }
  1032. } else {
  1033. MessageBox("Application failed to read x extent");
  1034. return FALSE;
  1035. }
  1036. } else {
  1037. MessageBox("Application failed to write y pos");
  1038. return FALSE;
  1039. }
  1040. } else {
  1041. MessageBox("Application failed to write x pos");
  1042. return FALSE;
  1043. }
  1044. return TRUE;
  1045. }
  1046. BOOL CMSQSCANDlg::ReadADFSettings(ADF_SETTINGS *pADFSettings)
  1047. {
  1048. //#define USE_FAKE_ADFCAPS
  1049. #ifdef USE_FAKE_ADFCAPS
  1050. pADFSettings->lDocumentHandlingCapabilites = FEED| // Feeder
  1051. FLAT| // Flatbed
  1052. DUP; // Duplex
  1053. pADFSettings->lDocumentHandlingCapacity = 20; // 20 pages max
  1054. pADFSettings->lDocumentHandlingSelect = FLATBED| // Feeder Mode is ON
  1055. FRONT_FIRST|// scan front page first
  1056. FRONT_ONLY; // scan front only
  1057. pADFSettings->lDocumentHandlingStatus = FLAT_READY; // Feeder is ready
  1058. pADFSettings->lPages = 1; // Initialize pages to 1
  1059. return TRUE;
  1060. #endif
  1061. HRESULT hr = S_OK;
  1062. if(pADFSettings!= NULL) {
  1063. //
  1064. // Read Settings From Root Item
  1065. //
  1066. IWiaItem *pRootItem = NULL;
  1067. pRootItem = m_WIA.GetRootItem();
  1068. if(pRootItem != NULL) {
  1069. hr = m_WIA.ReadLong(pRootItem,WIA_DPS_DOCUMENT_HANDLING_SELECT,&pADFSettings->lDocumentHandlingSelect);
  1070. if(SUCCEEDED(hr)){
  1071. hr = m_WIA.ReadLong(pRootItem,WIA_DPS_DOCUMENT_HANDLING_CAPABILITIES,&pADFSettings->lDocumentHandlingCapabilites);
  1072. if(SUCCEEDED(hr)){
  1073. hr = m_WIA.ReadLong(pRootItem,WIA_DPS_DOCUMENT_HANDLING_STATUS,&pADFSettings->lDocumentHandlingStatus);
  1074. if(SUCCEEDED(hr)){
  1075. hr = m_WIA.ReadLong(pRootItem,WIA_DPS_PAGES,&pADFSettings->lPages);
  1076. if (SUCCEEDED(hr)) {
  1077. hr = m_WIA.ReadLong(pRootItem,WIA_DPS_DOCUMENT_HANDLING_CAPACITY,&pADFSettings->lDocumentHandlingCapacity);
  1078. if (FAILED(hr)) {
  1079. MessageBox("Application failed to read the Document Handling Capacity");
  1080. return FALSE;
  1081. }
  1082. } else {
  1083. MessageBox("Application failed to read the Pages Property");
  1084. return FALSE;
  1085. }
  1086. } else {
  1087. MessageBox("Application failed to read the Document Handling Status");
  1088. return FALSE;
  1089. }
  1090. } else {
  1091. MessageBox("Application failed to read the Document Handling Capabilites");
  1092. return FALSE;
  1093. }
  1094. } else {
  1095. MessageBox("Application failed to read the Document Handling Select Property");
  1096. return FALSE;
  1097. }
  1098. } else {
  1099. MessageBox("Application failed to find the Root Item.");
  1100. return FALSE;
  1101. }
  1102. } else {
  1103. MessageBox("Application failed to read ADF settings, because the pointer to the Settings structure is NULL");
  1104. return FALSE;
  1105. }
  1106. return TRUE;
  1107. }
  1108. BOOL CMSQSCANDlg::WriteADFSettings(ADF_SETTINGS *pADFSettings)
  1109. {
  1110. HRESULT hr = S_OK;
  1111. if(pADFSettings!= NULL) {
  1112. //
  1113. // Write Settings to the Root Item
  1114. //
  1115. IWiaItem *pRootItem = NULL;
  1116. pRootItem = m_WIA.GetRootItem();
  1117. if(pRootItem != NULL) {
  1118. hr = m_WIA.WriteLong(pRootItem,WIA_DPS_DOCUMENT_HANDLING_SELECT,pADFSettings->lDocumentHandlingSelect);
  1119. if(FAILED(hr)){
  1120. MessageBox("Application failed to write ADF settings, because the Document Handling Select value failed to set");
  1121. return FALSE;
  1122. }
  1123. hr = m_WIA.WriteLong(pRootItem,WIA_DPS_PAGES,pADFSettings->lPages);
  1124. if(FAILED(hr)){
  1125. MessageBox("Application failed to write ADF settings, because the Pages property failed to set");
  1126. return FALSE;
  1127. }
  1128. } else {
  1129. MessageBox("Application failed to find the Root Item.");
  1130. return FALSE;
  1131. }
  1132. } else {
  1133. MessageBox("Application failed to write ADF settings, because the pointer to the Settings structure is NULL");
  1134. return FALSE;
  1135. }
  1136. return TRUE;
  1137. }
  1138. void CMSQSCANDlg::OnAdfSettings()
  1139. {
  1140. if(ReadADFSettings(&m_ADFSettings)) {
  1141. //
  1142. // create ADF dialog object
  1143. //
  1144. CADFDlg ADFDlg(&m_ADFSettings);
  1145. //
  1146. // display setting dialog
  1147. //
  1148. if(ADFDlg.DoModal() == IDOK) {
  1149. //
  1150. // write ADF settings back to the scanner if the user pushes the "OK" button
  1151. //
  1152. WriteADFSettings(&m_ADFSettings);
  1153. }
  1154. }
  1155. }
  1156. /////////////////////////////////////////////////////////////////////////////
  1157. // CEventCallback message handlers
  1158. HRESULT _stdcall CEventCallback::QueryInterface(const IID& iid, void** ppv)
  1159. {
  1160. *ppv = NULL;
  1161. if (iid == IID_IUnknown || iid == IID_IWiaEventCallback)
  1162. *ppv = (IWiaEventCallback*) this;
  1163. else
  1164. return E_NOINTERFACE;
  1165. AddRef();
  1166. return S_OK;
  1167. }
  1168. ULONG _stdcall CEventCallback::AddRef()
  1169. {
  1170. InterlockedIncrement((long*)&m_cRef);
  1171. return m_cRef;
  1172. }
  1173. ULONG _stdcall CEventCallback::Release()
  1174. {
  1175. ULONG ulRefCount = m_cRef - 1;
  1176. if (InterlockedDecrement((long*) &m_cRef) == 0)
  1177. {
  1178. delete this;
  1179. return 0;
  1180. }
  1181. return ulRefCount;
  1182. }
  1183. CEventCallback::CEventCallback()
  1184. {
  1185. m_cRef = 0;
  1186. m_pIUnkRelease = NULL;
  1187. }
  1188. CEventCallback::~CEventCallback()
  1189. {
  1190. Release();
  1191. }
  1192. HRESULT _stdcall CEventCallback::Initialize(int EventID)
  1193. {
  1194. if((EventID > 1)||(EventID < 0))
  1195. return S_FALSE;
  1196. m_EventID = EventID;
  1197. return S_OK;
  1198. }
  1199. HRESULT _stdcall CEventCallback::ImageEventCallback(
  1200. const GUID *pEventGUID,
  1201. BSTR bstrEventDescription,
  1202. BSTR bstrDeviceID,
  1203. BSTR bstrDeviceDescription,
  1204. DWORD dwDeviceType,
  1205. BSTR bstrFullItemName,
  1206. ULONG *plEventType,
  1207. ULONG ulReserved)
  1208. {
  1209. switch(m_EventID)
  1210. {
  1211. case ID_WIAEVENT_CONNECT:
  1212. MessageBox(NULL,"a connect event has been trapped...","Event Notice",MB_OK);
  1213. break;
  1214. case ID_WIAEVENT_DISCONNECT:
  1215. MessageBox(NULL,"a disconnect event has been trapped...","Event Notice",MB_OK);
  1216. break;
  1217. default:
  1218. AfxMessageBox("Ah HA!..an event just happened!!!!\n and...I have no clue what is was..");
  1219. break;
  1220. }
  1221. return S_OK;
  1222. }