Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event when a Document Window is focus in Visual Studio

I have an extensibility project in Visual Studio and I need to use the event triggered when I change from one window to another in the visual studio editor, my problem:

I created a Tool Window that display some diagram, that diagram depend of an editable file, when I save the editable file my tool window updates the information, but when there is more than one editable file opened and I switch between them I want that the tool window updates the information as well. So:

I want to get the event triggered when I switch between windows, file or documents in Visual Studio so I can use it to execute the update code of my tool window. Is there something I can do about it?

I just read this question here but I didn't find a solution in there: Are there any document window focus events?

like image 787
Jack1987 Avatar asked Mar 12 '15 03:03

Jack1987


1 Answers

You can subscribe to the EnvDTE.WindowEvents.WindowActivated event:

using EnvDTE;
using Microsoft.VisualStudio.Shell;

private class MyClass
{
    private DTE dte;

    public MyClass()
    {
        dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
        dte.Events.WindowEvents.WindowActivated += OnWindowActivated;
    }

    private void OnWindowActivated(Window gotFocus, Window lostFocus)
    {
        throw new NotImplementedException();
    }
}

See for example the 1. Display document path of the active window in the status bar sample code.

like image 175
Sergey Vlasov Avatar answered Nov 12 '22 15:11

Sergey Vlasov