Click here to Skip to main content
15,885,082 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.2K   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 in a listbox control
struct ListBoxTraits
{
    typedef DWORD item_type;

    static wchar_t* ClassName() { return L"listbox"; };

    static bool CheckValiditiy( HWND target, DWORD timeout = INFINITE )
    {
        DWORD item_count = 0;
        if( ::SendMessageTimeout( target, 
                                  LB_GETCOUNT, 
                                  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 )
    {
        LPARAM lp = pt.y << 16 | pt.x;
        item_type it;
        if( ::SendMessageTimeout( target,
                                  LB_ITEMFROMPOINT,
                                  0, 
                                  lp,
                                  SMTO_NORMAL,
                                  timeout,
                                  &it ) > 0 )
        {
            if( HIWORD( it ) == 0 && LOWORD( it ) > -1 )
            {
                *item = it;
                return true;
            }
        }
        return false;
    };

    static DWORD GetTextLength( HWND target, 
                                const item_type& item, 
                                DWORD timeout = INFINITE )
    {
        DWORD text_length = 0;
        if( ::SendMessageTimeout( target, 
                                  LB_GETTEXTLEN, 
                                  item, 
                                  0, 
                                  SMTO_NORMAL, 
                                  timeout, 
                                  &text_length ) > 0 )
        {
            return text_length;
        }
        return 0;
    };

    static bool GetText( HWND target, 
                         const item_type& item, 
                         DWORD length, 
                         std::wstring* text, 
                         DWORD timeout = INFINITE )
    {
        MappedBuffer lb_buffer( ( length + 1 ) * sizeof( wchar_t ) );
        wchar_t* result = reinterpret_cast< wchar_t* >( &lb_buffer.front() );

        DWORD text_length = 0;
        if( ::SendMessageTimeout( target, 
                                  LB_GETTEXT, 
                                  item, 
                                  reinterpret_cast< LPARAM >( result ), 
                                  SMTO_NORMAL, 
                                  timeout, 
                                  &text_length ) > 0 )
        {
            *text = result;
            return true;
        }
        return false;
    };

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

}; // 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