*Moved to: http://fluentbytes.com/creating-an-vs2010-add-in-to-show-the-running-user-as-part-of-the-main-ide-window-title/

Today some of my fellow MVP’s asked the question if there is an option in Visual Studio to show under which user account the IDE is running.

the need this feature since they do a lot of demonstrations on using the IDE and then use the runas option, to show how you e.g. can work with multiple users on a piece of code using version control etc.

Unfortunately this is something not default possible with the VS IDE.

But after doing some visual Studio Integration work in the past I thought this should not be to hard to build myself.

On the first thought I would just try to create an Visual Studio Add-in and there try to obtain a reference to the main window and there set the caption. Unfortunately that would have been too easy :-D. It is not possible this way , since the property on the main window is read-only and when you try to set it you will get an error message.

But hey, you probably also remember the good old days where we had the Win32 API as our friend. In win32 you can set the windows caption of a window using a wind32 API call called SendMessage as long as you provide the correct parameters. So I thought to give that Idea a shot.

To make that work I did following:

On the Add In connect, get a reference to the Main Window. From the Main window, get the native windows handle (Hwnd)

Then use good old PInvoke to call an imported win32 API SendMessage and send a WM_SETTEXT message to the main window.

Now the last thing to fix, is that each and every time a window is opened or changed, the caption will disappear. To fix this, I just subscribed to two main events, WindowActivated and WindowCreated and there execute the same code again. this way the caption is always updated once a new window is opened or closed.

The following code does the trick:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, string lParam);
        public const UInt32 WM_SETTEXT = 0x000C;
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
   _applicationObject = (DTE2)application;
   _addInInstance = (AddIn)addInInst;
public void OnStartupComplete(ref Array custom)
    _applicationObject.Events.WindowEvents.WindowActivated += new _dispWindowEvents_WindowActivatedEventHandler(WindowEvents_WindowActivated);
    _applicationObject.Events.WindowEvents.WindowCreated += new _dispWindowEvents_WindowCreatedEventHandler(WindowEvents_WindowCreated);
    WindowEvents_WindowActivated(null, null);
void WindowEvents_WindowCreated(Window Window)
    WindowEvents_WindowActivated(null, null);

void WindowEvents_WindowActivated(Window GotFocus, Window LostFocus)
     DTE d = _applicationObject as DTE;
     IntPtr hWnd = new System.IntPtr(d.MainWindow.HWnd);
     string Caption = d.MainWindow.Caption + "*** " + System.Threading.Thread.CurrentPrincipal.Identity.Name + " ****";
     Connect.SendMessage(hWnd, WM_SETTEXT, new IntPtr(0), Caption);

As you can see, the following is the result when you activate the add-in:



Here you can find the sources as a downloadable Zip(http://blogs.infosupport.com/media/p/202504.aspx), I did not create an installer for it, just open the zip, compile it and activate the registered Add-in.

voila 🙂



Follow my new blog on http://fluentbytes.com