VSTO: application focus

Does anyone know how to check if the Excel window of a VSTO project is really active / in focus?

I am looking for the equivalent of System.Windows.Window.IsActive .

+6
excel vsto
source share
2 answers

I was also upset by this. Do you use the dialog in the VSTO application? If so, then what I did adds an event to close the Windows / Dialog form to activate the Office application as follows (example with Word, so there may be differences in Excel):

 //... 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 found that this line always lies / returns true:

 this.ActiveWindow.Active() 

But this works better (global bool variable "AppActive" to track the active window):

 //... 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; } 
+7
source share

this.ActiveWindow.Activate() is the method that activates the window.

this.ActiveWindow.Active is a property indicating the state of the window.

+2
source share

All Articles