Click here to Skip to main content
Click here to Skip to main content

Forward/Backward navigation with the mouse thumb buttons for Visual Studio 2008 (C++)

By , 28 Sep 2008
 

Introduction

For most programs, users can use the "thumb-buttons" of the mouse to navigate forwards or backwards. This add-in will implement this feature in Visual Studio 2008 C++. (C# has this feature built-in).

Example usage

  1. Jump to a function definition (right click on the function name, "Go to definition").
  2. Jump back using the "backward button" of your mouse.

A look inside the code

The API for Visual Studio add-ins do not provide the possibility to get informed if a mouse button is pressed inside the Visual Studio window. It is necessary to use a Hook to achieve this:

private void InitHook()
{
  this.MouseProcDelegate = new HookProc(this.MouseProc);
  uint id = GetCurrentThreadId();
  hhook=SetWindowsHookEx(WH_MOUSE, 
        this.MouseProcDelegate, IntPtr.Zero, id);
}

The InitHook() function is called if Visual Studio is loading an add-in (on VS start). The hook is only attached to the thread whose the thread-ID is returned by GetCurrentThreadId(). The hook will receive the mouse events before Visual Studio does. Another possibility could be a ShellHook (WH_SHELL) (HSHELL_APPCOMMAND with the APPCOMMAND_BROWSER_BACKWARD and APPCOMMAND_BROWSER_FORWARD events). But, this does not work well (the event is not triggered under all circumstances).

The Hook will call the MouseProc function:

 private int MouseProc(int code, IntPtr wParam, ref MOUSEHOOKSTRUCTEX lParam)
    {
      try
      {
        if (code != HC_ACTION)
        {
          return CallNextHookEx(hhook, code, wParam, ref lParam);
        }

        if (wParam.ToInt32() == WM_XBUTTONUP)
        {
          switch (HiWord(lParam.mouseData))
          {
            case XBUTTON1:
              Debug.Write("mouse back button\n");
              _applicationObject.ExecuteCommand("View.NavigateBackward", "");
            return 1;

            case XBUTTON2:
              Debug.Write("mouse forward button\n");
              _applicationObject.ExecuteCommand("View.NavigateForward", "");
            return 1;
           }

        }

      }
      catch
      {

      }

      return CallNextHookEx(hhook, code, wParam, ref lParam);
    }

MOUSEHOOKSTRUCTEX is an updated version of MOUSEHOOKSTRUCT which supports the extra mouse buttons. If the wParam is WM_XBUTTONUP, one of the mouse thumb-buttons was released. You can find out which one was released using the hi-word of lParam.mouseData:

  • XBUTTON1: backward button
  • XBUTTON2: forward button

Finally, ExecuteCommand() is used to call the navigation functionality of Visual Studio:

_applicationObject.ExecuteCommand("View.NavigateBackward", "");
_applicationObject.ExecuteCommand("View.NavigateForward", "");

History

  • 0.2: Checks if the mouse button is pressed inside a C++ editor window.
  • 0.1: Initial version.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Jochen Baier
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberAldus@Monitor6 Aug '10 - 2:22 
Pros: Perfect functions. Works both with VS2005 and VS2008. Cons: Licence is GPL.
GeneralWindows 7 64-bitmemberBenoit Chaperot15 Jul '10 - 0:24 
Great code.
 
There seems to be a bug; probably in Windows 7 64-bit.
When executing the code lParam.mouseData often contains garbage.
One workaround solution was to stop/restart the addin in Visual Studio.
I have done the following changes to the addin to automatically unhook/rehook the MouseProc function if lParam.mouseData contains garbage.
This seems to work.
 
using System.Windows.Forms;
 
      private int MouseProc(int code, IntPtr wParam, ref MOUSEHOOKSTRUCTEX lParam)
      {
         try
         {
            if (code != HC_ACTION)
            {
               return CallNextHookEx(hhook, code, wParam, ref lParam);
            }
 
            if (wParam.ToInt32() == WM_XBUTTONUP && isEditorWindow(lParam.mstruct.WindowHandle) && isCPlusPlusText())
            {
               switch (HiWord(lParam.mouseData))
               {
                  case XBUTTON1:
                     Debug.Write("mouse back button\n");
                     _applicationObject.ExecuteCommand("View.NavigateBackward", "");
                  return 1;
 
                  case XBUTTON2:
                     Debug.Write("mouse forward button\n");
                     _applicationObject.ExecuteCommand("View.NavigateForward", "");
                  return 1;
              
                  default:
                     Debug.Write(string.Format("unknown button {0:x}.\n", lParam.mouseData));
                     MessageBox.Show(string.Format("MouseNavi: Problem with lParam.mouseData ({0:x}); attempting to rehook.", lParam.mouseData));
                     UnhookWindowsHookEx(hhook);
                     InitHook();
                     break;
               }
            }
 
         }
         catch
         {
 
         }
 
         return CallNextHookEx(hhook, code, wParam, ref lParam);
      }
GeneralBeen Looking For This For a Long Time !!memberBill Gord16 Feb '10 - 5:39 
Thanks for this !! Gets my '5'. Big Grin | :-D
GeneralAuthors tip: Version for Visual Studio 2010 availablememberJochen Baier7 Feb '10 - 8:44 
Forward/Backward code navigation with the mouse thumb buttons inside Visual Studio 2010 (C++, Visual Basic, F#)[^]
GeneralWorks in Visual Studio 2005, toomemberscott sanders16 Dec '09 - 17:26 
Fantastic add-in, just what I was wanting. Thanks !
Worth noting that it works in VS2005 as well if you change the <Version> in MouseNavi.AddIn to 8.0.
GeneralOk, this is a bit of a long shot...memberLongShot_0117 Jul '09 - 4:34 
But does anyone actually have the source code (headers and .cs or .cpp files)? I have an 8 button mouse and I'd like to modify the code to change some of my mouse bindings within VS 2008. Thanks!
GeneralRe: Ok, this is a bit of a long shot...memberJochen Baier18 Jul '09 - 8:41 
hi,
 
the linked zip file has the source code included. open the visual studio project file MouseNavi.sln with visual studio c# 2008. Read the README for install instruction
 
jochen
GeneralAwesomememberdcrobbins12 Sep '08 - 3:34 
just a note- in Vista, I had to turn UAC off to get it to work.
GeneralHelpmembervbMr'J23 Jul '08 - 8:55 
I wanted a webbrowser that use the thumbs buttons in my own exe. This example works with Visual Studio 2008, right? Can you make a Visual project with Thumbs buttons using webbrowser back od forward?
GeneralRe: HelpmemberMember 434649423 Jul '08 - 10:40 
hi,
 
can you explain more precisely what you want to do ?
 
regards
GeneralVery nicememberMicah.code.project5 Jun '08 - 5:51 
Compiles and installs flawlessly.
 
Beats the heck out of using control-key shortcuts!
 
Copy the dll and an xml file to "$My Documents$\Visual Studio 2008\Addins\"
Note: You may need to create the "Addins" dir.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 28 Sep 2008
Article Copyright 2008 by Jochen Baier
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid