// Twain.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "Twain.h" #include "MainFrm.h" #include "ChildFrm.h" #include "TwainDoc.h" #include "TwainView.h" #include "RasterTwainSink.h" #include "SupportedCaps.h" #include "LEADTemplate.h" #include "ErrorList.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CTwainApp BEGIN_MESSAGE_MAP(CTwainApp, CWinApp) //{{AFX_MSG_MAP(CTwainApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) ON_COMMAND(ID_FILE_OPEN, OnFileOpen) ON_COMMAND(ID_TWAIN_SELECTSOURCE, OnTwainSelectSource) ON_UPDATE_COMMAND_UI(ID_TWAIN_SELECTSOURCE, OnUpdateTwainSelectSource) ON_COMMAND(ID_TWAIN_ACQUIRE, OnTwainAcquire) ON_UPDATE_COMMAND_UI(ID_TWAIN_ACQUIRE, OnUpdateTwainAcquire) ON_COMMAND(ID_TEMPLATE_LEADTEMPLATE, OnTemplateLeadTemplate) ON_UPDATE_COMMAND_UI(ID_TEMPLATE_LEADTEMPLATE, OnUpdateTemplateLeadTemplate) ON_COMMAND(ID_TEMPLATE_SHOWERRORCODES, OnTemplateShowErrorCodes) ON_UPDATE_COMMAND_UI(ID_TEMPLATE_SHOWERRORCODES, OnUpdateTemplateShowErrorCodes) ON_COMMAND(ID_TEMPLATE_SHOWSUPPORTEDCAPABILITIES, OnTemplateShowSupportedCapabilities) ON_UPDATE_COMMAND_UI(ID_TEMPLATE_SHOWSUPPORTEDCAPABILITIES, OnUpdateTemplateShowSupportedCapabilities) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTwainApp construction CTwainApp::CTwainApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance m_pRasterKrnDlg = NULL; m_pRasterFileDlg = NULL; m_nPageNum = 0; m_bTwainAvailable = FALSE; m_pRasterTwainSink= NULL; m_twXferMech = L_TWSX_NATIVE; } ///////////////////////////////////////////////////////////////////////////// // The one and only CTwainApp object CTwainApp theApp; ///////////////////////////////////////////////////////////////////////////// // CTwainApp initialization BOOL CTwainApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif CoInitialize(0); // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate( IDR_TWAINTYPE, RUNTIME_CLASS(CTwainDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CTwainView)); AddDocTemplate(pDocTemplate); // create main MDI Frame window CMainFrame* pMainFrame = new CMainFrame; if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) return FALSE; m_pMainWnd = pMainFrame; // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); //Do not create document at startup if (CCommandLineInfo::FileNew == cmdInfo.m_nShellCommand) cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing; // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The main window has been initialized, so show and update it. pMainFrame->ShowWindow(m_nCmdShow); pMainFrame->UpdateWindow(); HRESULT hr = CoCreateInstance(CLSID_LEADRasterDlgKrn_D, NULL, CLSCTX_ALL, IID_ILEADRasterDlgKrn_D, (void**)&m_pRasterKrnDlg); if (FAILED(hr)) return FALSE; m_pRasterKrnDlg->InitDlg(INIT_WITH_COLOR); m_pRasterKrnDlg->EnableMethodErrors = FALSE; hr = CoCreateInstance(CLSID_LEADRasterDlgFile_D, NULL, CLSCTX_ALL, IID_ILEADRasterDlgFile_D, (void**)&m_pRasterFileDlg); if (FAILED(hr)) return FALSE; m_pRasterFileDlg->EnableMethodErrors = FALSE; int nRet = m_Lead.Create(NULL, 0, CRect(0, 0, 50, 50), m_pMainWnd, 100); m_Lead.ShowWindow(SW_HIDE); UNLOCKSUPPORT(m_Lead); m_Lead.SetEnableMethodErrors(FALSE); hr = CoCreateInstance(CLSID_LEADRasterTwain_D, NULL, CLSCTX_ALL, IID_ILEADRasterTwain_D, (void**)&m_pRasterTwain); if (FAILED(hr)) return FALSE; //Establish a connection between source and sink. m_pRasterTwainSink = new CRasterTwainSink; LPUNKNOWN pUnkSink = m_pRasterTwainSink->GetIDispatch(FALSE); AfxConnectionAdvise(m_pRasterTwain, DIID__LEADRasterTwainEvents_D, pUnkSink, FALSE, &m_dwCookie); m_pRasterTwain->EnableMethodErrors = FALSE; m_bTwainAvailable = m_pRasterTwain->IsAvailable((long)m_pMainWnd->m_hWnd); if (m_bTwainAvailable) { m_pRasterTwain->ManName = "LEAD Technologies, Inc."; m_pRasterTwain->ProdFamily = "LEAD Test Applications"; m_pRasterTwain->Version = "Version 1.0"; m_pRasterTwain->AppName = "TWAIN Test Application"; m_pRasterTwain->InitSession((long)m_pMainWnd->m_hWnd); } m_pRasterTwain->EnableLoadTemplateEvent = TRUE; m_pRasterTwain->EnableSaveTemplateEvent = TRUE; return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) // No message handlers //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() // App command to run the dialog void CTwainApp::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); } ///////////////////////////////////////////////////////////////////////////// // CTwainApp message handlers int CTwainApp::ExitInstance() { if (m_pRasterKrnDlg) m_pRasterKrnDlg->Release(); if (m_pRasterFileDlg) m_pRasterFileDlg->Release(); if (m_pRasterTwain) { if (m_bTwainAvailable) { LPUNKNOWN pUnkSink = m_pRasterTwainSink->GetIDispatch(FALSE); AfxConnectionUnadvise(m_pRasterTwain, DIID__LEADRasterTwainEvents_D, pUnkSink, FALSE, m_dwCookie); delete m_pRasterTwainSink; m_pRasterTwain->EndSession(); } m_pRasterTwain->Release(); } CoUninitialize(); return CWinApp::ExitInstance(); } void CTwainApp::OnFileOpen() { BOOL bContinue=FALSE; int nRet; m_pRasterFileDlg->PutFilter(""); m_pRasterFileDlg->PutFileDlgFlags(0); m_pRasterFileDlg->PutFileName(""); m_pRasterFileDlg->PutDialogTitle("Open File"); m_pRasterFileDlg->PutUIFlags(OPEN_SHOW_MULTIPAGE | OPEN_SHOW_PREVIEW | OPEN_USEFILESTAMP | OPEN_SHOW_FILEINFO | OPEN_SHOW_LOADOPTIONS | OPEN_SHOW_LOADCOMPRESSED | OPEN_SHOW_LOADROTATED | OPEN_SHOW_DELPAGE | OPEN_SHOW_PDFOPTIONS | OPEN_SHOW_RASTEROPTIONS | OPEN_SHOW_VECTOROPTIONS | OPEN_VIEWTOTALPAGES | OPEN_ENABLESIZING); nRet = m_pRasterFileDlg->ShowOpenDlg((long)m_pMainWnd->m_hWnd); if (nRet == 0 && m_pRasterFileDlg->GetDialogStatus() == DLG_OK) { m_nPageNum = (short)m_pRasterFileDlg->GetPageNumber(); POSITION pos = GetFirstDocTemplatePosition(); CDocTemplate* pDocTemplate = GetNextDocTemplate(pos); pDocTemplate->OpenDocumentFile(m_pRasterFileDlg->GetFileName()); } } void CTwainApp::CreateNewChild() { CWinApp::OnFileNew(); } void CTwainApp::OnTwainSelectSource() { m_pRasterTwain->SelectSource(); } void CTwainApp::OnUpdateTwainSelectSource(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bTwainAvailable); } void CTwainApp::OnTwainAcquire() { SetXferMode(); INT nRet = m_pRasterTwain->Acquire(L_LTWAIN_SHOW_USER_INTERFACE | L_LTWAIN_MODAL_USER_INTERFACE); if (nRet != 0) { CString csError; csError.Format(TEXT("An error occurred in TWAIN Acquire..., Error No: %d"), nRet); AddErrorToErrorsList(csError); MessageBox(m_pMainWnd->m_hWnd, csError, TEXT("Error!"), MB_OK); } else { if (m_twXferMech == L_TWSX_FILE) MessageBox(m_pMainWnd->m_hWnd, TEXT("Acquired page(s) is saved to file(s)"), TEXT("Acquire to File"), MB_OK); } } void CTwainApp::SetXferMode() { ILEADTwainCapability_D * ptwCap = NULL; ILEADRasterVariant * pRasVariant = NULL; HRESULT hr = CoCreateInstance(CLSID_LEADTwainCapability_D, NULL, CLSCTX_ALL, IID_ILEADTwainCapability_D, (void**)&ptwCap); if (FAILED(hr)) return; hr = CoCreateInstance(CLSID_LEADRasterVariant, NULL, CLSCTX_ALL, IID_ILEADRasterVariant, (void**)&pRasVariant); if (FAILED(hr)) { ptwCap->Release(); return; } ptwCap->EnableMethodErrors = FALSE; ptwCap->CapInfo->Capability = L_ICAP_XFERMECH; ptwCap->CapInfo->ConType = L_TWON_ONEVALUE; pRasVariant->Type = VALUE_USHORT; pRasVariant->LongValue = m_twXferMech; ptwCap->CapOneValue->OneValItemType = L_TWTY_UINT16; ptwCap->CapOneValue->OneValCapValue = pRasVariant; short nRet = m_pRasterTwain->SetCapability2(ptwCap, L_LTWAIN_CAPABILITY_SET); pRasVariant->Release(); ptwCap->Release(); } void CTwainApp::OnUpdateTwainAcquire(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bTwainAvailable); } void CTwainApp::OnTemplateLeadTemplate() { CLEADTemplate TemplateDlg(m_pRasterTwain); TemplateDlg.DoModal(); } void CTwainApp::OnUpdateTemplateLeadTemplate(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bTwainAvailable); } void CTwainApp::OnTemplateShowErrorCodes() { CErrorList ErrorListDlg; ErrorListDlg.DoModal(); } void CTwainApp::OnUpdateTemplateShowErrorCodes(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bTwainAvailable); } void CTwainApp::OnTemplateShowSupportedCapabilities() { CSupportedCaps CapsDlg(m_pRasterTwain); CapsDlg.DoModal(); } void CTwainApp::OnUpdateTemplateShowSupportedCapabilities(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bTwainAvailable); } void CTwainApp::AddErrorToErrorsList(CString csError) { m_ErrorsList.Add(csError); }