Excel作为WPF中的镶嵌框架已禁用ExcelWorksheet

时间:2018-08-23 13:07:38

标签: c# .net excel windows pinvoke

我找到了使用Windows的SetParent()函数在WPF中设置Excel实例的解决方案。

问题是,鼠标和键盘对工作表没有反应,但对工作簿却有反应。

完整样本Project Download here

我也尝试过使用WindowsFormsHost,但效果相同。

XAML

<Window x:Class="ExcelEditor.SimpleWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ExcelEditor"
    mc:Ignorable="d" Loaded="Window_Loaded" Closing="Window_Closing"
    Title="SimpleWindow" Height="450" Width="800">
<Grid x:Name="LayoutRoot">

</Grid>

C#代码

    using System;
    using System.Windows;


namespace ExcelEditor
{
    /// <summary>
    /// Interaktionslogik für SimpleWindow.xaml
    /// </summary>
    public partial class SimpleWindow : Window
    {
        private Microsoft.Office.Interop.Excel.Application ExcelApplication;

        public SimpleWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(this); // <- testing only (no success)
            ExcelApplication = new Microsoft.Office.Interop.Excel.Application();
            ExcelApplication.DisplayAlerts = false;
            System.Windows.Interop.HwndSource hwnd = (System.Windows.Interop.HwndSource)System.Windows.Interop.HwndSource.FromVisual(this.LayoutRoot);
            var excelWnd = Microsoft.Win32.Interop.FindWindow("XLMAIN", null);
            GenerateTestData(ExcelApplication);
            var successOfParentSetup = Microsoft.Win32.Interop.SetParent(excelWnd, hwnd.Handle);
            var isMovedToLeftTop = Microsoft.Win32.Interop.MoveWindow(excelWnd, 10, 10, 800, 600, true);
            ExcelApplication.Visible = true;
        }

        private void GenerateTestData(Microsoft.Office.Interop.Excel.Application excelApplication)
        {
            var excelWorkbook = excelApplication.Workbooks.Add();
            var excelWorksheet = excelWorkbook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
            excelWorksheet.Cells[1, 1] = "Today";
            excelWorksheet.Cells[2, 1] = DateTime.Now.ToString();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (ExcelApplication != null)
            {
                ExcelApplication.ActiveWorkbook.Close();
                ExcelApplication.Visible = false;
                ExcelApplication.Quit();
                ExcelApplication = null;
            }
        }
    }
}

Api通话

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    public static extern bool SetWindowPos(
        IntPtr hWnd,               // handle to window
        IntPtr hWndInsertAfter,    // placement-order handle
        int X,                  // horizontal position
        int Y,                  // vertical position
        int cx,                 // width
        int cy,                 // height
        uint uFlags             // window-positioning options
    );

    [DllImport("user32.dll", EntryPoint = "MoveWindow")]
    public static extern bool MoveWindow(
        IntPtr hWnd,
        int X,
        int Y,
        int nWidth,
        int nHeight,
        bool bRepaint
    );
    [DllImport("USER32.DLL", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    //Sets a window to be a child window of another window
    [DllImport("USER32.DLL", SetLastError = true)]
    public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

问题是,如何在Excel中启用鼠标和键盘处理以允许编辑工作表?

Excel not working propert as parent

*在以下情况下进行了测试:Windows 10 x64,单屏(可选双屏),Office 365之外的Excel 2016 MSO 32位(16.0.10325.20082)

Microsoft Forum上也报告了此问题,以检查是否有解决方案。

在以下视频中,您将看到单击有时会如何选择单元格,有时却无法识别鼠标。

Excel

1 个答案:

答案 0 :(得分:1)

您需要先确认Excel已显示一次,然后才能调用 code: 104 arrayScanObj.count PRE: 1 arrayScanObj.count PRE: 2 arrayScanObj.count PRE: 3 。当然,它看起来很丑陋,因此您还需要以某种方式向最终用户隐藏Excel UI。例如:

SetParent