VSTO:应用重点 [英] VSTO: Application Focus
问题描述
任何人都知道的一种方式,看是否有VSTO项目的Excel窗口被激活/焦点?
Anyone know of a way to see if the Excel window of a VSTO project is active/in focus?
我在寻找的等效 System.Windows.Window.IsActive
。
推荐答案
我已经感到失望这一点。您是否使用了VSTO应用程序的对话?如果是这样,我所做的就是添加一个事件到Windows窗体/对话闭幕如下激活Office应用程序(例如与Word,所以有可能会在Excel差异):
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;
}
我发现这行永远掌握/返回true:
I have found this line always lies/returns true:
this.ActiveWindow.Active()
但是,这工作得更好(全局布尔变量AppActive跟踪活动窗口):
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;
}
这篇关于VSTO:应用重点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!