1

如何在 vsto C# 中将焦点放在 Excel 应用程序对象上我一直在寻找它但没有任何成功

4

2 回答 2

1

试试这个代码

 Process[] processes = Process.GetProcessesByName("excel");
 foreach (Process p in processes)
 {
     if (p.MainWindowTitle.Contains(fileName.Substring(fileName.LastIndexOf("/") + 1)))
     {
         SetForegroundWindow(p.MainWindowHandle);
     }
 } 
于 2012-07-26T11:21:22.107 回答
0

我的 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 主窗口。

问候,约尔格

于 2014-05-02T09:02:01.677 回答