// MSVC5OCR.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "MSVC5OCR.h" #include "MainFrm.h" #include "MSVC5OCRDoc.h" #include "MSVC5OCRView.h" #include "leadraster.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMSVC5OCRApp BEGIN_MESSAGE_MAP(CMSVC5OCRApp, CWinApp) //{{AFX_MSG_MAP(CMSVC5OCRApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) ON_COMMAND(ID_FILE_OPEN, OnFileOpen) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMSVC5OCRApp construction CMSVC5OCRApp::CMSVC5OCRApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance m_pRaster = NULL; m_pRasterIO = NULL; m_pRasterProc = NULL; m_pRasterDlgKrn = NULL; m_pRasterDlgFile = NULL; m_bTwnAvailable = FALSE; m_pRasterTwainSink = NULL; } CMSVC5OCRApp::~CMSVC5OCRApp() { } ///////////////////////////////////////////////////////////////////////////// // The one and only CMSVC5OCRApp object CMSVC5OCRApp theApp; ///////////////////////////////////////////////////////////////////////////// // CMSVC5OCRApp initialization BOOL CMSVC5OCRApp::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(NULL); // Change the registry key under which our settings are stored. // 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. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMSVC5OCRDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CMSVC5OCRView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; m_Pos = pDocTemplate->GetFirstDocPosition(); CMSVC5OCRDoc * pDoc = (CMSVC5OCRDoc *)pDocTemplate->GetNextDoc(m_Pos); CMSVC5OCRView * pView = (CMSVC5OCRView*)pDoc->GetView(); pView->DoFit(); //create a temp LEADRaster object and use it //to unlock support for the Inet object HRESULT hr = S_OK; IClassFactory2 *pCF2=NULL; CLSID clsid; #ifndef _UNICODE static const WCHAR BASED_CODE _szID[] = L"LEADRaster.LEADRaster"; #else static const WCHAR BASED_CODE _szID[] = L"LEADRaster_U.LEADRaster_U"; #endif CLSIDFromProgID(_szID, &clsid); hr = CoGetClassObject(clsid, CLSCTX_ALL, NULL, IID_IClassFactory, (void**)&pCF2); if (FAILED (hr)) { AfxMessageBox (_TEXT("Failed To Create A Class Factory Object, The Demo will not work properly ...")); } else { //This object is licensed, so we need to pass a license key static const WCHAR BASED_CODE _szLic[] = L"LEADTOOLS OCX Copyright (c) 1991-2004 LEAD Technologies, Inc."; BSTR lpLic = SysAllocString(_szLic); pCF2->CreateInstanceLic(NULL, NULL, IID_IUnknown, lpLic, (void**)&m_pRaster); SysFreeString(lpLic); pCF2->Release(); UNLOCKSUPPORT(*m_pRaster); } m_pRasterTwainSink = new CRasterTwainSink; //Create the RasterTwain object hr = CoCreateInstance(CLSID_LEADRasterTwain, NULL, CLSCTX_ALL, IID_ILEADRasterTwain, (void**)&m_pRasterTwain); if (SUCCEEDED(hr)) { m_bTwnAvailable = m_pRasterTwain->IsAvailable((long)m_pMainWnd->m_hWnd); if(m_bTwnAvailable) { 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); } } else return FALSE; //Establish a connection between source and sink. LPUNKNOWN pUnkSink = m_pRasterTwainSink->GetIDispatch(FALSE); AfxConnectionAdvise(m_pRasterTwain, DIID__LEADRasterTwainEvents, pUnkSink, FALSE, &m_dwCookie); hr = CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&m_pRasterIO); if (FAILED(hr)) { AfxMessageBox(_TEXT("Can't instantiate the Raster IO Object...")); return FALSE; } hr = CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL, IID_ILEADRasterProcess, (void**)&m_pRasterProc); if (FAILED(hr)) { AfxMessageBox(_TEXT("Can't instantiate the Raster Process Object...")); return FALSE; } hr = CoCreateInstance(CLSID_LEADRasterDlgKrn, NULL, CLSCTX_ALL, IID_ILEADRasterDlgKrn, (void**)&m_pRasterDlgKrn); if (FAILED(hr)) { AfxMessageBox(_TEXT("Can't instantiate the Raster Kernel Dialog Object...")); return FALSE; } hr = CoCreateInstance(CLSID_LEADRasterDlgFile, NULL, CLSCTX_ALL, IID_ILEADRasterDlgFile, (void**)&m_pRasterDlgFile); if (FAILED(hr)) { AfxMessageBox(_TEXT("Can't instantiate the Raster File Dialog Object...")); return FALSE; } m_pRasterDlgKrn->InitDlg(INIT_WITHOUT_COLOR); m_pRaster->PutEnableMethodErrors(FALSE); m_pRasterIO->PutEnableMethodErrors(FALSE); m_pRasterProc->PutEnableMethodErrors(FALSE); m_pRasterDlgKrn->PutEnableMethodErrors(FALSE); m_pRasterDlgFile->PutEnableMethodErrors(FALSE); // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); if (pView->InitEngine()) { TCHAR szTempPath[MAX_PATH]; GetTempPath(MAX_PATH, szTempPath); pView->m_csRecogDataFileName.Format(_TEXT("%s%s"), szTempPath, _TEXT("OCRPages.RDF")); } m_nPageNum = 1; 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 CMSVC5OCRApp::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); } ///////////////////////////////////////////////////////////////////////////// // CMSVC5OCRApp message handlers int CMSVC5OCRApp::ExitInstance() { if (m_pRasterDlgFile) m_pRasterDlgFile->Release(); if (m_pRasterDlgKrn) { m_pRasterDlgKrn->FreeDlg(); m_pRasterDlgKrn->Release(); } if (m_pRasterTwain) { LPUNKNOWN pUnkSink = m_pRasterTwainSink->GetIDispatch(FALSE); AfxConnectionUnadvise(m_pRasterTwain, DIID__LEADRasterTwainEvents, pUnkSink, FALSE, m_dwCookie); delete m_pRasterTwainSink; if(m_bTwnAvailable) m_pRasterTwain->EndSession(); m_pRasterTwain->Release(); } if (m_pRasterIO) m_pRasterIO->Release(); if (m_pRasterProc) m_pRasterProc->Release(); if (m_pRaster) m_pRaster->Release(); CoUninitialize(); return CWinApp::ExitInstance(); } void CMSVC5OCRApp::OnFileOpen() { m_pRasterDlgFile->PutEnableMethodErrors(FALSE); m_pRasterDlgFile->PutFilter(""); m_pRasterDlgFile->PutFileDlgFlags(0); m_pRasterDlgFile->PutDialogTitle("Open File"); m_pRasterDlgFile->PutUIFlags( OPEN_SHOW_PROGRESSIVE | OPEN_SHOW_MULTIPAGE | OPEN_SHOW_LOADROTATED | OPEN_SHOW_LOADCOMPRESSED | OPEN_SHOW_FILEINFO | OPEN_SHOW_PREVIEW | OPEN_SHOW_DELPAGE | OPEN_SHOW_LOADOPTIONS | OPEN_SHOW_RASTEROPTIONS | OPEN_SHOW_PDFOPTIONS | OPEN_SHOW_VECTOROPTIONS | OPEN_VIEWTOTALPAGES | OPEN_ENABLESIZING | OPEN_NOFILEMUSTEXIST | OPEN_NOPATHMUSTEXIST | OPEN_USEFILESTAMP); int nRet = m_pRasterDlgFile->ShowOpenDlg((long)m_pMainWnd->m_hWnd); if (nRet == 0) { if(m_pRasterDlgFile->GetDialogStatus() == DLG_OK) { CString strFileName = (LPCTSTR)m_pRasterDlgFile->FileName; m_nPageNum = m_pRasterDlgFile->GetPageNumber(); POSITION pos = GetFirstDocTemplatePosition(); CDocTemplate* pDocTemplate = GetNextDocTemplate(pos); pDocTemplate->OpenDocumentFile(strFileName); } } } void CMSVC5OCRApp::AddTwainImage() { CWinApp::OnFileNew(); } ///////////////////////////////////////////////////////////////////////////// // CAsciiEdit Edit box #ifdef UNICODE CAsciiEdit::CAsciiEdit ( ) { } CAsciiEdit::~CAsciiEdit () { } LRESULT CAsciiEdit::WindowProc ( UINT message, WPARAM wParam, LPARAM lParam) { switch ( message ) { case WM_PASTE: { if ( !CheckPasteText ( ) ) { MessageBox ( _T ( "You should enter ANSII characters." ) ) ; return 0 ; } break ; } case WM_CHAR: { // Enter only English char if ( wParam > 128) { MessageBox ( _T ( "You should enter ANSII characters." ) ) ; return 0 ; } break ; } } return CEdit::WindowProc ( message, wParam, lParam ) ; } BOOL CAsciiEdit::CheckPasteText ( ) { CString strText ; LPTSTR lptstrPaste = NULL; if ( !OpenClipboard( ) ) { return FALSE; } #ifdef UNICODE HGLOBAL hglbPaste = ::GetClipboardData( CF_UNICODETEXT ) ; #else HGLOBAL hglbPaste = ::GetClipboardData( CF_TEXT ); #endif if ( hglbPaste != NULL ) { lptstrPaste = ( TCHAR* )::GlobalLock( hglbPaste ); if ( lptstrPaste != NULL ) { ::GlobalUnlock( hglbPaste ); } } ::CloseClipboard(); if ( lptstrPaste == NULL ) { return FALSE; } strText = lptstrPaste; if ( ! CheckText ( strText ) ) { return FALSE; } return TRUE ; } BOOL CAsciiEdit::CheckText ( CString &str ) { int iLen = str.GetLength () ; for ( int i = 0; i < iLen; ++i ) { if ( str[i] > 128 ) { return FALSE ; } } return TRUE ; } #endif