Click here to Skip to main content
15,886,724 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.
///////////////////////////////////////////////////////////////
//
// WindowHook.cpp
//
// Created: 29/07/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 "WindowHook.h"
#include "WindowHelper.h"

WindowHook::WindowHook() :
    m_hook( NULL ),
    m_pObserver( NULL )
{
    m_hook = ::SetWindowsHookEx( 
                        WH_CALLWNDPROCRET,
                        CallWndRetProc,
                        _Module.GetModuleInstance(),
                        ::GetCurrentThreadId()
                        );

    if( m_hook == NULL )
    {
        ATLASSERT( FALSE );
    }
}

WindowHook::~WindowHook()
{
    if( m_hook )
    {
        ::UnhookWindowsHookEx( m_hook );
    }
}

LRESULT WindowHook::CallWndRetProc( int nCode, WPARAM wParam, LPARAM lParam )
{
    return WindowHookManager::GetInstance().GetHook()->OnHook( nCode, wParam, lParam );
}

LRESULT WindowHook::OnHook( int nCode, WPARAM wParam, LPARAM lParam )
{
    if( nCode == HC_ACTION )
    {
        if( lParam && m_pObserver )
        {
            CWPRETSTRUCT    *pCWPR = reinterpret_cast< CWPRETSTRUCT * >( lParam );

            switch( pCWPR->message )                
            {
            case WM_CREATE:
            case WM_DESTROY:
                if( pCWPR->message == WM_CREATE )
                {
                    if( pCWPR->lResult != -1 )
                    {
                        m_pObserver->OnCreate( pCWPR->hwnd );
                    }
                }
                else
                {
                    if( pCWPR->lResult == 0 )
                    {
                        m_pObserver->OnDestroy( pCWPR->hwnd );
                    }
                }
                break;
            }
        }
    }

    return ::CallNextHookEx( m_hook, nCode, wParam, lParam );
}

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

WindowHookManager::WindowHookManager() :
    m_tlsIndex( ::TlsAlloc() )
{        
    ATLASSERT( m_tlsIndex != 0 );
}

WindowHookManager::~WindowHookManager()
{
    if( m_tlsIndex )
    {
        ::TlsFree( m_tlsIndex );
    }
}

WindowHook * WindowHookManager::GetHook()
{
    if( m_tlsIndex == 0 )
    {
        return NULL;
    }

    WindowHook *pHook = reinterpret_cast< WindowHook * >( ::TlsGetValue( m_tlsIndex ) );

    if( pHook == NULL )
    {
        pHook = new WindowHook();
        ::TlsSetValue( m_tlsIndex, pHook );
    }
#ifdef _DEBUG
    else
    {
        ATLASSERT( ::IsBadReadPtr( pHook, sizeof( WindowHook ) ) == FALSE );
        ATLASSERT( ::IsBadWritePtr( pHook, sizeof( WindowHook ) ) == FALSE );
    }
#endif

    return pHook;
}

void WindowHookManager::AttachHook( WindowHook::Observer *pObserver )
{
    WindowHook  *pHook = GetHook();

    if( pHook )
    {
        pHook->m_pObserver = pObserver;
    }
}

void WindowHookManager::ReleaseHook()
{
    if( m_tlsIndex == 0 )
    {
        return;
    }

    WindowHook *pHook = reinterpret_cast< WindowHook * >( ::TlsGetValue( m_tlsIndex ) );

    if( pHook )
    {
    #ifdef _DEBUG
        ATLASSERT( ::IsBadReadPtr( pHook, sizeof( WindowHook ) ) == FALSE );
        ATLASSERT( ::IsBadWritePtr( pHook, sizeof( WindowHook ) ) == FALSE );
    #endif

        delete  pHook;
        ::TlsSetValue( m_tlsIndex, NULL );
    }
}

WindowHookManager & WindowHookManager::GetInstance()
{
    static WindowHookManager    instance;
    return instance;
}

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