如何设置Excel窗口的大小?

时间:2014-07-17 08:03:13

标签: c# wpf excel office-interop excel-interop

我有一个wpf窗口,应该在Excel窗口旁边打开。使用Interop.Excel打开并处理Excel。 在我的窗口中,我有一个方法,应该设置excel窗口的大小。

private void SetLayout()
        {
            Top = 0;
            Left = 0;
            Height = SystemParameters.WorkArea.Height;
            ((ConfiguratorWindowViewModel) DataContext).Manager.App.Height =
                SystemParameters.WorkArea.Height;
            ((ConfiguratorWindowViewModel) DataContext).Manager.App.Left = Width;
            ((ConfiguratorWindowViewModel) DataContext).Manager.App.Width = SystemParameters.WorkArea.Width - Width;
        }

在我的Viewmodel中,我有一个管理器,它返回打开的Excel应用程序(Office.Interop.Excel.Application)。 我可以设置我的wpf窗口的Height,Top,..它工作正常,但不能在Excel窗口中。 如何在我的wpf窗口旁边设置Excel窗口的大小,以便两者一起填满整个屏幕?

编辑:

我尝试了这个,但也没有用:

((ConfiguratorWindowViewModel) DataContext).Manager.App.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlNormal;
((ConfiguratorWindowViewModel) DataContext).Manager.App.ActiveWindow.Height = Screen.PrimaryScreen.Bounds.Height;
((ConfiguratorWindowViewModel) DataContext).Manager.App.ActiveWindow.Width = Screen.PrimaryScreen.Bounds.Width - Width;

两者都将高度设置为不同的值而不是正确的值,当我像.Height=800;那样对其进行硬编码时,它也无效。

不幸的是,我无法发布图片来向您展示它的外观和外观。

编辑:

问题与此问题不同(How do I change another program's window's size?)。我不想改变啊“外国”应用程序的大小。

1 个答案:

答案 0 :(得分:5)

假设您已将excel应用程序存储在名为App的变量中。然后你可以做这样的事情:

App.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlNormal;
App.ActiveWindow.Height = Screen.PrimaryScreen.Bounds.Height;
App.ActiveWindow.Width = Screen.PrimaryScreen.Bounds.Width;

屏幕类用于获取主屏幕的分辨率。

您可能还可以最大化窗口:

 App.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized