Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
2.96/5 (13 votes)
28 Sep 2008GPL31 min read 51.1K   366   14   12
Forward/backward navigation with the mouse thumb buttons for Visual Studio 2008 (C++).

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:

C#
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:

C#
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:

C#
_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)


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

Comments and Discussions

 
QuestionThank you! Pin
Ivan Ferrer29-Sep-17 6:06
Ivan Ferrer29-Sep-17 6:06 
GeneralMy vote of 5 Pin
Aldus@Monitor6-Aug-10 2:22
Aldus@Monitor6-Aug-10 2:22 
GeneralWindows 7 64-bit Pin
Benoit Chaperot15-Jul-10 0:24
Benoit Chaperot15-Jul-10 0:24 
GeneralBeen Looking For This For a Long Time !! Pin
Bill Gord16-Feb-10 5:39
professionalBill Gord16-Feb-10 5:39 
GeneralAuthors tip: Version for Visual Studio 2010 available Pin
Jochen Baier7-Feb-10 8:44
Jochen Baier7-Feb-10 8:44 
GeneralWorks in Visual Studio 2005, too Pin
scott sanders16-Dec-09 17:26
scott sanders16-Dec-09 17:26 
GeneralOk, this is a bit of a long shot... Pin
LongShot_0117-Jul-09 4:34
LongShot_0117-Jul-09 4:34 
GeneralRe: Ok, this is a bit of a long shot... Pin
Jochen Baier18-Jul-09 8:41
Jochen 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
GeneralAwesome Pin
dcrobbins12-Sep-08 3:34
dcrobbins12-Sep-08 3:34 
GeneralHelp Pin
vbMr'J23-Jul-08 8:55
vbMr'J23-Jul-08 8:55 
GeneralRe: Help Pin
Jochen Baier23-Jul-08 10:40
Jochen Baier23-Jul-08 10:40 
GeneralVery nice Pin
Micah.code.project5-Jun-08 5:51
Micah.code.project5-Jun-08 5:51 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.