2009-04-27 9 views
6

Wer weiß, wie man sieht, ob das Excel-Fenster eines VSTO-Projekts aktiv/scharf ist?VSTO: Anwendungsfokus

Ich bin auf der Suche nach einem Äquivalent von System.Windows.Window.IsActive.

Antwort

7

Ich war auch damit frustriert. Verwenden Sie einen Dialog in der VSTO App? Wenn ja, haben, was ich getan ist ein Ereignis das Schließen eines Windows Form/Dialog fügen Sie die Office-Anwendung zu aktivieren, wie folgt (zB mit Word ist, so kann es Unterschiede in Excel sein):

//... VSTO Startup Event 
WindowsForm form = new WindowsForm(); 
form.FormClosed += new FormClosedEventHandler(form_FormClosed); 
form.Show(); 


void form_FormClosed(object sender, FormClosedEventArgs e) 
{ 
    this.Application.Activate();   
    this.Application.ActiveWindow.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateNormal; 

} 

I diese Linie immer haben festgestellt, liegt/true zurückgibt:

this.ActiveWindow.Active() 

Aber das funktioniert besser (global Bool Variable "AppActive" Spur des aktiven Fensters zu halten):

//... VSTO Startup Event  
this.Application.WindowDeactivate += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowDeactivateEventHandler(Application_WindowDeactivate); 
this.Application.WindowActivate += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowActivateEventHandler(Application_WindowActivate); 

    void Application_WindowActivate(Microsoft.Office.Interop.Word.Document Doc, Microsoft.Office.Interop.Word.Window Wn) 
    { 
     AppActive = true; 
    } 

    void Application_WindowDeactivate(Microsoft.Office.Interop.Word.Document Doc, Microsoft.Office.Interop.Word.Window Wn) 
    { 
     AppActive = false; 
    } 
+1

nette Lösung Mike, tolle Arbeit. Ich spüre zwar, dass ActiveWindow.Active -Eigenschaft sollte immer True, sonst ist es nicht das aktive Fenster ist es? –

2

this.ActiveWindow.Activate() ist die Methode, die das Fenster aktiviert.

this.ActiveWindow.Active ist die Eigenschaft, die den Status des Fensters angibt.