VSTO: Application Focus

你说的曾经没有我的故事 提交于 2020-01-02 03:32:07

问题


Anyone know of a way to see if the Excel window of a VSTO project is active/in focus?

I'm looking for an equivalent of System.Windows.Window.IsActive.


回答1:


I've been frustrated with this as well. Are you using a dialog in the VSTO app? If so, what I have done is add an event to the closing of a Windows Form/Dialog to activate the Office application as follows (example is 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 have found this line always lies/returns true:

this.ActiveWindow.Active()

But this works better (global bool variable "AppActive" to keep track of 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;
    }



回答2:


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

this.ActiveWindow.Active is the property that tell you the state of the window.



来源:https://stackoverflow.com/questions/795059/vsto-application-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!