Click here to Skip to main content
15,886,026 members
Articles / Web Development / HTML

Mouse Gestures for Internet Explorer

Rate me:
Please Sign up or sign in to vote.
4.84/5 (99 votes)
21 Sep 200514 min read 1.3M   13.4K   235  
Adding mouse gesture recognition to Internet Explorer.
///////////////////////////////////////////////////////////////
//
// BrowserWatcher60.cpp
//
// Created: 23/09/2003
// Copyright (c) 2003 Ralph Hare (ralph.hare@ysgyfarnog.co.uk)
// All rights reserved.
//
// The code and information is provided "as-is" without
// warranty of any kind, either expressed or implied.
//
///////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BrowserWatcher60.h"
#include "WindowHelper.h"
#include "ComUtils.h"

namespace IE60
{
    HRESULT GetElementOffset( IHTMLElement *pElement, SIZE &offset )
    {
        if( !pElement )
        {
            return E_POINTER;
        }
        
    //
    // as we're zeroing the offset structure here, be sure
    // to perform the recursive step before adding the offset
    // of the current element
    //
        ::ZeroMemory( &offset, sizeof( SIZE ) );

        ThrowHResult    hr;

        try
        {
        //
        // do we need to recurse up the element hierarchy?
        //
            CComPtr< IHTMLElement > pParent;
            hr = pElement->get_parentElement( &pParent );
            if( pParent )
            {
                hr = GetElementOffset( pParent, offset );
            }

            long    cx, cy;
            hr = pElement->get_offsetLeft( &cx );
            hr = pElement->get_offsetTop( &cy );

            offset.cx += cx;
            offset.cy += cy;
        }
        catch( HRESULT )
        {
        }

        return hr;
    }

/**
 * Ascend the frame hierarchy (if any) until we find the first non-frame HTML 
 * element at the specified position
 **/
    HRESULT HitTest( IWebBrowser2 *pWB, const POINT &pt, IHTMLElement **ppElement )
    {
        if( pWB == NULL )
        {
            return E_INVALIDARG;
        }

        if( !ppElement )
        {
            return E_POINTER;
        }

        *ppElement = NULL;

        ThrowHResult    hr;

        try
        {
        //
        // get the document associated with this web browser object
        //
            CComPtr< IDispatch >    pDisp;
            hr = pWB->get_Document( &pDisp );

        //
        // is it an HTML document?
        //
            CComQIPtr< IHTMLDocument2 >   pDoc = pDisp;

            if( pDoc == NULL )
            {
            //
            // no - nothing more doing
            //
                return S_OK;
            }

        //
        // get the element at the specified point
        //
            CComPtr< IHTMLElement > pElement;
            hr = pDoc->elementFromPoint( pt.x, pt.y, &pElement );

            if( pElement == NULL )
            {
                return S_OK;
            }

        //
        // it could be a nested frame - if so we need to
        // drill down
        //
            CComQIPtr< IWebBrowser2 >   pFrame = pElement;

            if( pFrame )
            {
            //
            // how far is the frame nested within the document?
            //
                SIZE    offset;
                hr = GetElementOffset( pElement, offset );

                POINT   ptInFrame = { pt.x - offset.cx, pt.y - offset.cy };

                return HitTest( pFrame, ptInFrame, ppElement );
            }

        //
        // return the element we've found to the user
        //
            hr = pElement->QueryInterface( 
                                    IID_IHTMLElement,
                                    reinterpret_cast< void ** >( ppElement )
                                    );
        }
        catch( HRESULT )
        {
        }

        return hr;
    }
}

BrowserWatcher60::BrowserWatcher60( 
                                IWebBrowser2 *pWebBrowser, 
                                GestureTracker::Observer *pObserver 
                                ) :
    BrowserWatcher( pWebBrowser, pObserver )
{
}

HRESULT BrowserWatcher60::HitTest( HWND, const POINT &pt, IHTMLElement **ppElement )
{
    return IE60::HitTest( GetWebBrowser(), pt, ppElement );
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions