// TwainDemo.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "TwainDemo.h" #include "MainFrm.h" #include "ChildFrm.h" #include "TwainDemoDoc.h" #include "TwainDemoView.h" #include "SupportedCaps.h" #include "ErrorList.h" #include "Template.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CTwainDemoApp BEGIN_MESSAGE_MAP(CTwainDemoApp, CWinApp) //{{AFX_MSG_MAP(CTwainDemoApp) 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_WINDOW_CLOSEALL, OnWindowCloseall) 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() ///////////////////////////////////////////////////////////////////////////// // CTwainDemoApp construction CTwainDemoApp::CTwainDemoApp() { LBase::LoadLibraries(LT_ALL_LEADLIB); WRPUNLOCKSUPPORT(); memset(m_szCaption, 0, sizeof(m_szCaption)); m_twXferMech = TWSX_NATIVE; } ///////////////////////////////////////////////////////////////////////////// // The one and only CTwainDemoApp object CTwainDemoApp theApp; ///////////////////////////////////////////////////////////////////////////// // CTwainDemoApp initialization BOOL CTwainDemoApp::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 // 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(0); // 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_TWAINDTYPE, RUNTIME_CLASS(CTwainDemoDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CTwainDemoView)); 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); //No Child window will be displayed if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew) 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(); LDialogBase::Initialize(DLG_INIT_COLOR); m_MyTwain.SetWindow(m_pMainWnd->m_hWnd); m_bTwainAvailable = m_MyTwain.IsAvailable(); if (m_bTwainAvailable) { APPLICATIONDATA AppData; memset(&AppData, 0, sizeof(APPLICATIONDATA)); AppData.hWnd = m_pMainWnd->m_hWnd; AppData.uStructSize = sizeof(AppData); lstrcpy (AppData.szManufacturerName, _T("LEAD Technologies, Inc.")); lstrcpy (AppData.szAppProductFamily, _T("LEAD Test Applications")); lstrcpy (AppData.szVersionInfo, _T("Version 1.0")); lstrcpy (AppData.szAppName, _T("TWAIN Test Application")); m_MyTwain.InitSession(&AppData); } 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 CTwainDemoApp::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); } ///////////////////////////////////////////////////////////////////////////// // CTwainDemoApp message handlers int CTwainDemoApp::ExitInstance() { if (m_bTwainAvailable) m_MyTwain.EndSession(); LDialogBase::Free(); LBase::UnloadLibraries(LT_ALL_LEADLIB); return CWinApp::ExitInstance(); } void CTwainDemoApp::OnFileOpen() { OPENDLGPARAMS FOParm; OPENFILENAME OpenFileName; memset(&FOParm, 0, sizeof(OPENDLGPARAMS)); memset(&OpenFileName, 0, sizeof(OPENFILENAME)); OpenFileName.lStructSize = sizeof(OPENFILENAME); OpenFileName.lpstrInitialDir = NULL; OpenFileName.Flags = OFN_EXPLORER; FOParm.uStructSize = sizeof(OPENDLGPARAMS); FOParm.uDlgFlags = DLG_OPEN_SHOW_PROGRESSIVE | DLG_OPEN_SHOW_MULTIPAGE | DLG_OPEN_SHOW_LOADROTATED | DLG_OPEN_SHOW_LOADCOMPRESSED | DLG_OPEN_SHOW_FILEINFO | DLG_OPEN_SHOW_DELPAGE | DLG_OPEN_VIEWTOTALPAGES; FOParm.bPreviewEnabled = TRUE; m_LUserBitmap.DialogFile()->SetOpenParams(&FOParm); m_LUserBitmap.DialogFile()->EnablePreview(TRUE); m_LUserBitmap.DialogFile()->SetOpenFileName(&OpenFileName); L_BOOL bOldState = m_LUserBitmap.DialogFile()->EnableAutoProcess(TRUE); m_LUserBitmap.EnableCallBack(FALSE); L_INT nRetCode = m_LUserBitmap.DialogFile()->DoModalOpen(m_pMainWnd->m_hWnd); if (nRetCode == SUCCESS_DLG_OK) { L_TCHAR szFileName[256]; memset(szFileName,0,sizeof(szFileName)); L_UINT uSize=sizeof(szFileName); m_LUserBitmap.GetFileName(szFileName,&uSize); POSITION pos = GetFirstDocTemplatePosition(); CDocTemplate* pDocTemplate = GetNextDocTemplate(pos); pDocTemplate->OpenDocumentFile(szFileName); } } void CTwainDemoApp::CreateNewBitmapWindow(CString csCaption, LBitmapBase* pLBitmapSrc) { if (pLBitmapSrc == NULL) { m_LUserBitmap.DisplayError(m_pMainWnd->m_hWnd,_T("Error: Create window")); return; } memset(&m_szCaption,0,sizeof(m_szCaption)); m_LUserBitmap.SetHandle(pLBitmapSrc->GetHandle()); if (csCaption.IsEmpty()) lstrcpy(m_szCaption,_T("LEAD Bitmap")); else memmove(m_szCaption, csCaption, min(sizeof(m_szCaption), lstrlen(csCaption) * sizeof(L_TCHAR))); if (m_LUserBitmap.IsAllocated()==TRUE) CWinApp::OnFileNew(); } void CTwainDemoApp::OnTwainSelectsource() { m_MyTwain.SelectSource(NULL); } void CTwainDemoApp::OnUpdateTwainSelectsource(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bTwainAvailable); } void CTwainDemoApp::SetXferMode() { TW_CAPABILITY twCap; pTW_ONEVALUE pOneVal = NULL; twCap.Cap = ICAP_XFERMECH; twCap.ConType = TWON_ONEVALUE; twCap.hContainer = GlobalAlloc(GHND, sizeof(TW_ONEVALUE)); pOneVal = (pTW_ONEVALUE)GlobalLock(twCap.hContainer); pOneVal->Item = m_twXferMech; pOneVal->ItemType = TWTY_UINT16; GlobalUnlock(twCap.hContainer); m_MyTwain.SetCapability(&twCap, LTWAIN_CAPABILITY_SET); } void CTwainDemoApp::OnTwainAcquire() { pBITMAPHANDLE pBitmap = NULL; m_MyTwain.EnableCallBack(TRUE); SetXferMode(); L_INT nRet = m_MyTwain.Acquire(pBitmap, sizeof(BITMAPHANDLE), LTWAIN_SHOW_USER_INTERFACE | LTWAIN_MODAL_USER_INTERFACE, NULL); if (nRet == SUCCESS) { if (m_twXferMech == TWSX_FILE) MessageBox(m_pMainWnd->m_hWnd, TEXT("Acquired page(s) is saved to file(s)"), TEXT("Acquire to File"), MB_OK); } else { 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); } } void CTwainDemoApp::OnUpdateTwainAcquire(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bTwainAvailable); } void CTwainDemoApp::OnWindowCloseall() { CloseAllDocuments(FALSE); } void CTwainDemoApp::AddErrorToErrorsList(CString csError) { m_ErrorsList.Add(csError); } void CTwainDemoApp::OnTemplateLeadtemplate() { CTemplate templateDlg; templateDlg.DoModal(); } void CTwainDemoApp::OnUpdateTemplateLeadtemplate(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bTwainAvailable); } void CTwainDemoApp::OnTemplateShowerrorcodes() { CErrorList errorDlg; errorDlg.DoModal(); } void CTwainDemoApp::OnUpdateTemplateShowerrorcodes(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bTwainAvailable); } void CTwainDemoApp::OnTemplateShowsupportedcapabilities() { CSupportedCaps SupCapsDlg; SupCapsDlg.DoModal(); } void CTwainDemoApp::OnUpdateTemplateShowsupportedcapabilities(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bTwainAvailable); }