Click here to Skip to main content
15,892,809 members
Articles / Mobile Apps / Windows Mobile

Scraping Text from the Screen

Rate me:
Please Sign up or sign in to vote.
4.96/5 (36 votes)
17 Jun 2010CPOL8 min read 108.8K   8.6K   83  
How to programmatically read text from any process at any point on the screen
#pragma once
#include "MappedFileAllocator.hpp"

namespace ScreenScrape {
namespace detail {

/// traits for reading the text of a tab control
struct TabTraits
{
    typedef int item_type;

    static wchar_t* ClassName() { return WC_TABCONTROL; };

    static bool CheckValiditiy( HWND target, DWORD timeout = INFINITE )
    {
        DWORD item_count = 0;
        if( ::SendMessageTimeout( target, 
                                  TCM_GETITEMCOUNT, 
                                  0, 
                                  0, 
                                  SMTO_NORMAL, 
                                  timeout, 
                                  &item_count ) > 0 )
        {
            return item_count > 0;
        }
        return false;
    };

    static BOOL LocateItem( HWND target, 
                            const POINT& pt, 
                            item_type* item, 
                            DWORD timeout = INFINITE )
    {
        MappedBuffer tch_buffer( sizeof( TCHITTESTINFO ) );
        TCHITTESTINFO* tch = 
            reinterpret_cast< TCHITTESTINFO* >( &tch_buffer.front() );
        tch->pt = pt;

        item_type it;
        if( ::SendMessageTimeout( target,
                                  TCM_HITTEST,
                                  0, 
                                  reinterpret_cast< LPARAM >( tch ),
                                  SMTO_NORMAL,
                                  timeout,
                                  reinterpret_cast< DWORD* >( &it ) ) > 0 )
        {
            if( it > -1 )
            {
                *item = it;
                return true;
            }
        }
        return false;
    };

    static DWORD GetTextLength( HWND /*target*/, 
                                const item_type& /*item*/, 
                                DWORD /*timeout*/ = INFINITE )
    {
        return 260;
    };

    static bool GetText( HWND target, 
                         const item_type& item, 
                         DWORD length, 
                         std::wstring* text, 
                         DWORD timeout = INFINITE )
    {
        MappedBuffer tc_buffer( sizeof( TCITEM ) + sizeof( wchar_t ) * length );
        TCITEM* tc = reinterpret_cast< TCITEM* >( &tc_buffer.front() );
        tc->cchTextMax = length;
        tc->mask = TCIF_TEXT;
        tc->pszText = reinterpret_cast< wchar_t* >( 
            &tc_buffer.front() + sizeof( TCITEM ) );

        BOOL success = FALSE;
        if( ::SendMessageTimeout( target, 
                                  TCM_GETITEM, 
                                  item, 
                                  reinterpret_cast< LPARAM >( tc ), 
                                  SMTO_NORMAL, 
                                  timeout, 
                                  reinterpret_cast< DWORD* >( &success ) ) > 0 )
        {
            if( success )
            {
                *text = tc->pszText;
                return true;
            }
        }
        return false;
    }

private:
    /// a sequential byte-buffer backed by a memory-mapped file.
    typedef std::vector< byte, MappedFileAllocator< byte > > MappedBuffer;
}; // struct TabTraits

}; // namespace detail
}; // namespace ScreenScrape

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) An engineering firm in Cedar Rapids, Iowa
United States United States
I'm also on the MSDN forums
http://social.msdn.microsoft.com/profile/paulh79

Comments and Discussions