我们的WinForms应用程序使用MS Word中进行交互,我们运行这个code时生成一个文件,我们要显示在Word中我们的应用程序的前面:
Our winforms application interacts with MS Word and we run this code when a document is generated and we want to show it in Word in front of our application:
[setup w as a Word interop object]
w.Visible = True
w.Activate()
在推出了为 XP上运行Office 2007的计算机这个工程的目的。
When rolled out to XP machines running Office 2007 this works as intended.
在 Win7的运行Office 2010的计算机我们的任务栏上的应用程序,并闪烁背后的文档加载。
On Win7 machines running Office 2010 the document loads behind our application and flashes on the taskbar.
任何想法?
我最近偶然发现了一个类似的问题。我的.NET程序调用COM应用程序,但是在Win7的它有时会没有显示在任务栏上,也不是桌面上的。我是不是真的能够追查这个原因,但我写了下面的函数来解决这个问题:
I stumbled upon a similar problem recently. My .NET program called a COM application, but on Win7 it would sometimes neither show up in taskbar nor on the desktop at all. I wasn't really able to track down the cause of this, but I wrote the following function to work around the issue:
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hwnd);
private static void BringAppToFront() {
foreach (var p in System.Diagnostics.Process.GetProcesses().Where(p => p.ProcessName == "COMInstanceName")) {
if (p.MainWindowHandle.ToInt32() != 0)
SetForegroundWindow(p.MainWindowHandle);
}
}