1
如何VSTO C# 我一直在尋找,但沒有任何成功將焦點設置Excel應用程序
如何VSTO C# 我一直在尋找,但沒有任何成功將焦點設置Excel應用程序
試試這個代碼
Process[] processes = Process.GetProcessesByName("excel");
foreach (Process p in processes)
{
if (p.MainWindowTitle.Contains(fileName.Substring(fileName.LastIndexOf("/") + 1)))
{
SetForegroundWindow(p.MainWindowHandle);
}
}
中的解決方案將焦點設置Excel應用程序對象我的VSTO的應用是:
[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(IntPtr hWnd);
/// <summary>
/// Gets the main excel window.
/// </summary>
/// <returns>An ArbitraryWindow that represents the main excel window.</returns>
public static ArbitraryWindow GetMainExcelWindow()
{
var excelHwnd_IntPtr = new IntPtr(Globals.ThisAddIn.Application.Hwnd);
var excelWindow = new ArbitraryWindow(excelHwnd_IntPtr);
return excelWindow;
}
/// <summary>
/// Activates the excel window.
/// </summary>
public static void ActivateExcelWindow()
{
var currentlyActiveForm = Form.ActiveForm;
//if (currentlyActiveForm != null && currentlyActiveForm.GetType() == typeof(FormProgress)) return;
var handle = GetMainExcelWindow().Handle;
BringWindowToTop(handle);
}
只需撥打 「ActivateExcelWindow()」,以激活Excel的主窗口。
問候, 約爾格
看看這個[SO接聽](http://stackoverflow.com/questions/795059/vsto-application-focus)幫助。並看看這個[鏈接](http://www.add-in-express.com/forum/read.php?FID=1&TID=3616) – 2012-07-25 05:07:06