C# Excel VSTO加载项:无法在MessageBox.Show(“测试”)之后重新激活Excel;
我正在实现一个VSTO Excel加载项,其中显示一些模式对话框。这些对话框不会在windows任务栏中显示为自己的窗口。但在某些情况下,这些对话框不会从Excel顶部消失,并且无法使用任务栏返回C# Excel VSTO加载项:无法在MessageBox.Show(“测试”)之后重新激活Excel;,c#,excel,modal-dialog,vsto,office-interop,C#,Excel,Modal Dialog,Vsto,Office Interop,我正在实现一个VSTO Excel加载项,其中显示一些模式对话框。这些对话框不会在windows任务栏中显示为自己的窗口。但在某些情况下,这些对话框不会从Excel顶部消失,并且无法使用任务栏返回 整个故事(可通过Windows XP-7、Excel 2007-2010复制): 我打开Excel并创建两个或多个新工作簿 我确实显示了一个模式对话框,比如说通过“MessageBox.show”打开“记事本”并最大化其窗口 我尝试通过windows重新激活其中一个excel工作簿窗口 任务栏 我希
整个故事(可通过Windows XP-7、Excel 2007-2010复制):
代码示例:
namespace ExcelAddIn6
{
公共部分类ThisAddIn
{
私有void ThisAddIn_启动(对象发送方,System.EventArgs e)
{
NativeWindowExcelWindowThatheOwner=null;
尝试
{
//获取excel主窗口以设置所有者(但无论如何都没有帮助)
ExcelWindowThatheOwner=新的本机窗口();
ExcelWindowThatheOwner.AssignHandle(新的IntPtr(Globals.ThisAddIn.Application.Hwnd));
//再创建两个工作簿以具有多个。。。
Globals.ThisAddIn.Application.Workbooks.Add();
Globals.ThisAddIn.Application.Workbooks.Add();
Globals.ThisAddIn.Application.WindowState=Excel.XlWindowState.xlMaximized;
//显示模式对话框(此处:一个消息框,但是)
MessageBox.Show(所有者:ExcelWindow即所有者,
text:“我是一个模式MessageBox。\n现在将另一个应用程序带到前台,然后尝试通过windows任务栏将excel带回来…”);
//模态形式的问题保持不变
var myForm=新表单();
myForm.ShowInTaskbar=false;
myForm.ShowDialog(所有者的Excel窗口);
}
最后
{
//清理
如果(ExcelWindowThatheOwner!=null)ExcelWindowThatheOwner.ReleaseHandle();
}
}
私有void ThisAddIn_关闭(对象发送方,System.EventArgs e)
{
}
#区域VSTO生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InternalStartup()
{
this.Startup+=new System.EventHandler(ThisAddIn\u启动);
this.Shutdown+=new System.EventHandler(ThisAddIn\u Shutdown);
}
#端区
}
实际结果是什么?你只看到MessageBox了吗?什么也没有出现?你能编辑并修复复制的步骤吗(它们缺少换行符)?@etaiso:对不起。我编辑了这个(在圣诞节的时候很匆忙:)你能帮我解决我的问题吗?我想如果你在不同的线程上显示,就会发生这种情况。但是,我不知道如何在Excel的UI线程上运行。我怀疑标准MessageBox是否在主UI线程之外的其他线程上运行。或者我错了?
namespace ExcelAddIn6
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
NativeWindow excelWindowThatIsTheOwner = null;
try
{
//Get excel main window to set owner (however does not help anyway)
excelWindowThatIsTheOwner = new NativeWindow();
excelWindowThatIsTheOwner.AssignHandle(new IntPtr(Globals.ThisAddIn.Application.Hwnd));
//Create two more workbooks to have more than one...
Globals.ThisAddIn.Application.Workbooks.Add();
Globals.ThisAddIn.Application.Workbooks.Add();
Globals.ThisAddIn.Application.WindowState = Excel.XlWindowState.xlMaximized;
//Show modal dialog (here: a message box, but )
MessageBox.Show(owner: excelWindowThatIsTheOwner,
text: "I am a modal MessageBox.\nNow bring another application to the foreground and then try to bring excel back via the windows taskbar...");
//Problem stays the same for modal forms
var myForm = new Form();
myForm.ShowInTaskbar = false;
myForm.ShowDialog(excelWindowThatIsTheOwner);
}
finally
{
//Cleanup
if (excelWindowThatIsTheOwner != null) excelWindowThatIsTheOwner.ReleaseHandle();
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}