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.

197 lines
6.1 KiB

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "wiatest.h"
  5. #include "MainFrm.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CMainFrame
  13. IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
  14. BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  15. //{{AFX_MSG_MAP(CMainFrame)
  16. // NOTE - the ClassWizard will add and remove mapping macros here.
  17. // DO NOT EDIT what you see in these blocks of generated code !
  18. ON_WM_CREATE()
  19. //}}AFX_MSG_MAP
  20. END_MESSAGE_MAP()
  21. static UINT indicators[] =
  22. {
  23. ID_SEPARATOR, // status line indicator
  24. ID_INDICATOR_CAPS,
  25. ID_INDICATOR_NUM,
  26. ID_INDICATOR_SCRL,
  27. };
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CMainFrame construction/destruction
  30. CMainFrame::CMainFrame()
  31. {
  32. }
  33. CMainFrame::~CMainFrame()
  34. {
  35. }
  36. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  37. {
  38. if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
  39. return -1;
  40. if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
  41. | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  42. !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  43. {
  44. TRACE0("Failed to create toolbar\n");
  45. return -1; // fail to create
  46. }
  47. if (!m_wndStatusBar.Create(this) ||
  48. !m_wndStatusBar.SetIndicators(indicators,
  49. sizeof(indicators)/sizeof(UINT)))
  50. {
  51. TRACE0("Failed to create status bar\n");
  52. return -1; // fail to create
  53. }
  54. // TODO: Delete these three lines if you don't want the toolbar to
  55. // be dockable
  56. m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  57. EnableDocking(CBRS_ALIGN_ANY);
  58. DockControlBar(&m_wndToolBar);
  59. // register for events
  60. RegisterForEvents();
  61. return 0;
  62. }
  63. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  64. {
  65. if( !CMDIFrameWnd::PreCreateWindow(cs) )
  66. return FALSE;
  67. // TODO: Modify the Window class or styles here by modifying
  68. // the CREATESTRUCT cs
  69. return TRUE;
  70. }
  71. /////////////////////////////////////////////////////////////////////////////
  72. // CMainFrame diagnostics
  73. #ifdef _DEBUG
  74. void CMainFrame::AssertValid() const
  75. {
  76. CMDIFrameWnd::AssertValid();
  77. }
  78. void CMainFrame::Dump(CDumpContext& dc) const
  79. {
  80. CMDIFrameWnd::Dump(dc);
  81. }
  82. #endif //_DEBUG
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CMainFrame message handlers
  85. void CMainFrame::RegisterForEvents()
  86. {
  87. HRESULT hr = S_OK;
  88. IWiaDevMgr *pIWiaDevMgr = NULL;
  89. hr = CoCreateInstance(CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr,(void**)&pIWiaDevMgr);
  90. if(FAILED(hr)){
  91. // creation of device manager failed, so we can not continue
  92. ErrorMessageBox(IDS_WIATESTERROR_COCREATEWIADEVMGR,hr);
  93. return;
  94. }
  95. IWiaEventCallback* pIWiaEventCallback = NULL;
  96. hr = m_WiaEventCallback.QueryInterface(IID_IWiaEventCallback,(void **)&pIWiaEventCallback);
  97. if (SUCCEEDED(hr)) {
  98. GUID guidEvent = WIA_EVENT_DEVICE_CONNECTED;
  99. BOOL bFailedOnce = FALSE;
  100. hr = pIWiaDevMgr->RegisterEventCallbackInterface(0,
  101. NULL,
  102. &guidEvent,
  103. pIWiaEventCallback,
  104. &m_WiaEventCallback.m_pIUnkRelease[0]);
  105. if (FAILED(hr)) {
  106. // display one error message... instead of one for each event.
  107. if (!bFailedOnce) {
  108. ErrorMessageBox(IDS_WIATESTERROR_REGISTER_EVENT_CALLBACK,hr);
  109. }
  110. bFailedOnce = TRUE;
  111. }
  112. guidEvent = WIA_EVENT_DEVICE_DISCONNECTED;
  113. hr = pIWiaDevMgr->RegisterEventCallbackInterface(0,
  114. NULL,
  115. &guidEvent,
  116. pIWiaEventCallback,
  117. &m_WiaEventCallback.m_pIUnkRelease[1]);
  118. if (FAILED(hr)) {
  119. // display one error message... instead of one for each event.
  120. if (!bFailedOnce) {
  121. ErrorMessageBox(IDS_WIATESTERROR_REGISTER_EVENT_CALLBACK,hr);
  122. }
  123. bFailedOnce = TRUE;
  124. }
  125. }
  126. m_WiaEventCallback.SetViewWindowHandle(m_hWnd);
  127. //
  128. // register for action events by command-line
  129. //
  130. WCHAR szMyApplicationLaunchPath[MAX_PATH];
  131. memset(szMyApplicationLaunchPath,0,sizeof(szMyApplicationLaunchPath));
  132. GetModuleFileNameW(NULL,szMyApplicationLaunchPath,sizeof(szMyApplicationLaunchPath));
  133. BSTR bstrMyApplicationLaunchPath = SysAllocString(szMyApplicationLaunchPath);
  134. WCHAR szMyApplicationName[MAX_PATH];
  135. memset(szMyApplicationName,0,sizeof(szMyApplicationName));
  136. HINSTANCE hInst = AfxGetInstanceHandle();
  137. if (hInst) {
  138. LoadStringW(hInst, IDS_MYAPPLICATION_NAME, szMyApplicationName, (sizeof(szMyApplicationName)/sizeof(WCHAR)));
  139. BSTR bstrMyApplicationName = SysAllocString(szMyApplicationName);
  140. GUID guidScanButtonEvent = WIA_EVENT_SCAN_IMAGE;
  141. hr = pIWiaDevMgr->RegisterEventCallbackProgram(
  142. WIA_REGISTER_EVENT_CALLBACK,
  143. NULL,
  144. &guidScanButtonEvent,
  145. bstrMyApplicationLaunchPath,
  146. bstrMyApplicationName,
  147. bstrMyApplicationName,
  148. bstrMyApplicationLaunchPath);
  149. if (FAILED(hr)) {
  150. }
  151. SysFreeString(bstrMyApplicationName);
  152. bstrMyApplicationName = NULL;
  153. }
  154. SysFreeString(bstrMyApplicationLaunchPath);
  155. bstrMyApplicationLaunchPath = NULL;
  156. // release DevMgr
  157. pIWiaDevMgr->Release();
  158. pIWiaDevMgr = NULL;
  159. }