none
Addin: How to know if already opened doucment got focus?

    Question

  • Hi experts,

    I'm very much newbie to VS Addins.

    Although, I subscribed to DocumentEvent.DocumentOpened. But additionally, I need to detect if already opened document got focus and I will read its contents then.

    How to get its focused state?

    Thanks

    Farrukh


    Best regards Farrukh

    Monday, April 30, 2012 7:24 PM

Answers

  • Hi Ed,

    Thanks for quick reply. I'm aware of this property actually (http://msdn.microsoft.com/en-US/library/envdte._dte.activedocument%28v=VS.80%29.aspx). But this doesn't provide the required solution

    Fortunately, after playing some sample code, I've got what I want. Its actually EnvDTE.WindowEvents.

    In VS IDE, every Code Document is also a Window. And it has the Focus event: WindowActivated. Here is my delegate to subscribe for this event:

    WinEvents.WindowActivated += new _dispWindowEvents_WindowActivatedEventHandler(WinEvents_WindowActivated);
    
    void WinEvents_WindowActivated(Window GotFocus, Window LostFocus)        
    {            
       Debug.WriteLine("GotFocus: " + GotFocus.Caption );            
       //throw new NotImplementedException();        
    }

    Best regards

    Farrukh


    Best regards Farrukh

    • Marked as answer by Farrukhw Tuesday, May 01, 2012 6:52 AM
    Tuesday, May 01, 2012 6:52 AM

All replies

  • Hi Farrukh,

    I suspect you're looking for the _DTE.ActiveDocument property. Also, if you have the Document interface, you can also use Document.Activate to set a given document as the active document.

    Sincerely,


    Ed Dore

    Tuesday, May 01, 2012 1:24 AM
    Moderator
  • Hi Ed,

    Thanks for quick reply. I'm aware of this property actually (http://msdn.microsoft.com/en-US/library/envdte._dte.activedocument%28v=VS.80%29.aspx). But this doesn't provide the required solution

    Fortunately, after playing some sample code, I've got what I want. Its actually EnvDTE.WindowEvents.

    In VS IDE, every Code Document is also a Window. And it has the Focus event: WindowActivated. Here is my delegate to subscribe for this event:

    WinEvents.WindowActivated += new _dispWindowEvents_WindowActivatedEventHandler(WinEvents_WindowActivated);
    
    void WinEvents_WindowActivated(Window GotFocus, Window LostFocus)        
    {            
       Debug.WriteLine("GotFocus: " + GotFocus.Caption );            
       //throw new NotImplementedException();        
    }

    Best regards

    Farrukh


    Best regards Farrukh

    • Marked as answer by Farrukhw Tuesday, May 01, 2012 6:52 AM
    Tuesday, May 01, 2012 6:52 AM