Text Capture Library COM Server Quick Tutorial - Visual C++ Sample

Download the source code for this article: VCSample.zip                     More samples...

SUMMARY

The following section illustrates how you can create an MFC project and import Text Capture Library as a COM server and capture text from mouse pointed window(ie. Explore of "My Computer").

Step-by-Step Example

1.      

With Microsoft Developer Studio, start a new "MFC AppWizard (exe)" project named "TcServerSampleApp."

2.      

In step 1 of the MFC AppWizard, choose "Dialog Based" for the application type and then click Finish.
  The New Project Information dialog box appears and indicates that the Classes to be created include:
  Application: CTcServerSampleApp in TcServerSample.h and TcServerSample.cpp Dialog: CTcServerSampleDlg in TcServerSampleDlg.h and TcServerSampleDlg.cpp
 

Click OK to create the project.

3. The Dialog box "IDD_TCSERVERSAMPLE_DIALOG" opens in the Visual Studio design/edit area. Modify it according to the instructions in the next two steps.
4. Remove the Label control (IDC_STATIC) and the Cancel button (IDCANCEL).
5. Change the name of the OK button to "IDC_GETTEXT_BUTTON" and the caption to "Capture It!." Close the dialog box design form.
6. Click ClassWizard on the View menu (or press CTRL+W).
7. Select the Message Maps tab. Select IDC_GETTEXT_BUTTONin the Object Ids list box and select "BN_CLICKED" in the Messages list box. Click Add Function and accept the function name "OnGetTextButton". Click OK to close the ClassWizard.

NOTE: This step adds a declaration for the function member "OnGetTextButton();" to the header file named TcServerSampleDlg.h. This step also adds an empty skeleton message handler function named CTcServerSampleDlg ::OnGetTextButton() to the file named TcServerSampleDlg.cpp.
8. Add the following code at the head of TcServerSampleDlg
 


//////////////////////////////////////////////////////////////////////////
// Import Text Capture Library COM object defination
//
#import "..\Lib\_TextCaptureLib.tlb"
//

//////////////////////////////////////////////////////////////////////////

 
      
9. Add a public member variable to the class CTcServerSampleDlg, varialble type is TextCaptureLib::ITextCapturePtr, vairalble name is m_pTextCapture:

public:
      TextCaptureLib::ITextCapturePtr m_pTextCapture;  // Declare the TextCaptureLib COM library object
 

10. Add the following code to the CTcServerSampleDlg::OnInitDialog() so that it appears as shown below:
11.

        // NOTE: Make sure to initialize COM before trying to create the Text capture COM library object.

        ::CoInitialize( NULL );
        // Create the TextCaptureLib COM library object.

        m_pTextCapture.CreateInstance( __uuidof( TextCaptureLib::CTextCapture));

        if (m_pTextCapture == NULL )
        {
                // Handle creation error.

                AfxMessageBox( _T("Fatal error: failed to create Text capture COM library object."), MB_ICONERROR );
                EndDialog( IDCANCEL );

        }
        return TRUE;  // return TRUE  unless you set the focus to a control

12. Add the following code to the CTcServerSampleDlg::OnGetTextButton() so that it appears as shown below:
 

void CTcServerSampleDlg::OnGetTextButton()
{
        if (IDOK ==AfxMessageBox(_T("Three seconds after you close this dialog, the mouse pointed window will be captured."), MB_OKCANCEL))
        {
                ShowWindow(SW_HIDE);
                Sleep(3000);
                TextCaptureLib::IWindowPtr WindowPtr;
                WindowPtr.CreateInstance( __uuidof( TextCaptureLib::CInputWindow));
                if (WindowPtr == NULL )
                {
                        // Handle creation error.
                        AfxMessageBox( _T("Fatal error: failed to create TextCaptureLib::CInputWindow COM object."), MB_ICONERROR );
                        EndDialog( IDCANCEL );
                        return;
                }
                // Capture text from mouse pointed window
                if (WindowPtr->WindowFromCurrentPos() == VARIANT_TRUE)
                {
                        CString strResult;
                        try
                        {
                                _bstr_t bstr = m_pTextCapture->GetText(WindowPtr);
                                strResult = (LPTSTR)bstr;
                                GetDlgItem(IDC_RESULT_EDIT)->SetWindowText(strResult);
                        }
                        catch ( _com_error captureError )
                        {
                                AfxMessageBox( _T("Unable to capture."), MB_ICONINFORMATION );
                        }
                }
                WindowPtr.Release();
                ShowWindow(SW_SHOW);
        }
}

13. Add windows message handler WM_CLOSE to the class CTcServerSampleDlg, select "WM_CLOSE" in the Messages list box. Click Add Function and accept the function name "OnClose". Click OK to close the ClassWizard.
  Add the following code to the CTcServerSampleDlg::OnClose() so that it appears as shown below:
 


void CTcServerSampleDlg::OnClose()
{
         // Closes the TextCaptureLib COM library on the current apartment
        if (m_pTextCapture != NULL)
                m_pTextCapture.Release();

        ::CoUninitialize();
        
        CDialog::OnClose();

}
 

14.

Build and run the project. RESULTS: When you click the "Capture It!" button in the dialog box, a message window will popup. If you clicked "OK", 3 seconds after close this message box, the mouse pointed windows will be captured by Text Capture Library and the result will be displayed.

More samples...