Click here to Skip to main content
15,896,207 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.
///////////////////////////////////////////////////////////////
//
// ExplorerListDlg.cpp
//
// Created: 17/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 "ExplorerListDlg.h"
#include "WindowHelper.h"
#include "OSUtils.h"
#include <algorithm>

namespace
{
    LPCTSTR CLASS_IE_FRAME = _T( "IEFrame" );

    BOOL __stdcall EnumWindowsProc( HWND hWnd, LPARAM lParam )
    {
        if( lParam == NULL )
        {
            return FALSE;
        }

        if( WindowHelper::GetClassName( hWnd ) == CLASS_IE_FRAME )
        {
        #ifdef _DEBUG
            ATLASSERT( ::IsBadReadPtr( 
                                reinterpret_cast< void * >( lParam ), 
                                sizeof( WindowList ) 
                                ) == FALSE );
            ATLASSERT( ::IsBadWritePtr( 
                                reinterpret_cast< void * >( lParam ), 
                                sizeof( WindowList ) 
                                ) == FALSE );
        #endif

            reinterpret_cast< WindowList * >( lParam )->push_back( hWnd );
        }

        return TRUE;
    }

    class InsertWindows : public std::unary_function< HWND, void >
    {
    public:
        InsertWindows( const CListViewCtrl &listCtrl ) :
            m_listCtrl( listCtrl )
        {
        }

        void operator()( HWND hWnd )
        {
            int len = ::GetWindowTextLength( hWnd ) + 1;
            std::vector< TCHAR >    buffer( len );

            ::GetWindowText( hWnd, &buffer[ 0 ], len );

            int item = m_listCtrl.InsertItem( 999999, &buffer[ 0 ] );
            m_listCtrl.SetItemData( item, reinterpret_cast< DWORD_PTR >( hWnd ) );
        }

    private:
        CListViewCtrl   m_listCtrl;
    };
}

BOOL ExplorerListDlg::Invoke( HWND hWndParent )
{
//
// modeless dialog as we don't get mouse button up messages
// if the cursor goes out of the client area with a modal dialog
// (even with SetCapture) ????
//
    static ExplorerListDlg  instance;

    if( instance.Create( hWndParent, NULL ) == NULL )
    {
        return FALSE;
    }

    instance.ShowWindow( SW_SHOW );

    return TRUE;
}

HWND ExplorerListDlg::Create( HWND hWndParent, LPARAM dwInitParam )
{
    m_windowList.clear();

    ::EnumDesktopWindows( 
                    NULL, 
                    EnumWindowsProc, 
                    reinterpret_cast< LPARAM >( &m_windowList ) 
                    );

    if( m_windowList.size() < 2 )
    {
        return NULL;
    }

    HWND    hWnd = CDialogImpl< ExplorerListDlg >::Create( hWndParent, dwInitParam );
    if( ::IsWindow( hWnd ) == FALSE )
    {
        return hWnd;
    }

//
// bring the window to the top
//
    ::SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );

//
// TODO: add code to size window according to width of text
// GetFont()
// GetDC() etc.
//
    m_listCtrl = GetDlgItem( IDC_LIST_EXPLORERS );
    m_listCtrl.InsertColumn( 0, _T( "" ), LVCFMT_LEFT, 350, -1 );

    std::for_each(
                m_windowList.begin(),
                m_windowList.end(),
                InsertWindows( m_listCtrl )
                );

    HWND    hWndFrame = ::GetAncestor( GetParent(), GA_ROOT );
    int     selected  = 0;

    for( int idx = m_listCtrl.GetItemCount(); --idx != -1; )
    {
        if( reinterpret_cast< HWND >( m_listCtrl.GetItemData( idx ) ) == hWndFrame )
        {
            selected = idx;
            break;
        }
    }

    m_listCtrl.SelectItem( selected );

    CenterWindow();
    SetCapture();

    m_mouseTracker.Advise( m_listCtrl, this );

    return hWnd;
}

bool ExplorerListDlg::OnMouseWheel( HWND hWnd, WORD, WORD delta, const POINT &pt )
{
    int item = m_listCtrl.GetSelectedIndex();

    if( short( delta ) < 0 )
    {
        if( ++item == m_listCtrl.GetItemCount() )
        {
            item = 0;
        }
    }
    else
    {
        if( --item == -1 )
        {
            item += m_listCtrl.GetItemCount();
        }
    }

    m_listCtrl.SelectItem( item );

    return true;
}

LRESULT ExplorerListDlg::OnRightButtonUp( UINT, WPARAM, LPARAM, BOOL & )
{
    ShowWindow( SW_HIDE );

    int     item = m_listCtrl.GetSelectedIndex();
    HWND    hWnd = reinterpret_cast< HWND >( m_listCtrl.GetItemData( item ) );

    if( ::IsIconic( hWnd ) )
    {
        WINDOWPLACEMENT pl;
        pl.length = sizeof( WINDOWPLACEMENT );

        ::GetWindowPlacement( hWnd, &pl );
        if( ( pl.flags & WPF_RESTORETOMAXIMIZED ) == WPF_RESTORETOMAXIMIZED )
        {
            ::ShowWindow( hWnd, SW_MAXIMIZE );
        }
        else
        {
            ::ShowWindow( hWnd, SW_RESTORE );
        }
    }

    ::SetFocus( hWnd );
    ::SetForegroundWindow( hWnd );

    m_mouseTracker.Unadvise();
    DestroyWindow();

    return 0;
}

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