【问题标题】:Microsoft Word Automation / interop Win32 API issueMicrosoft Word 自动化/互操作 Win32 API 问题
【发布时间】:2012-07-11 11:31:38
【问题描述】:

我正在尝试控制 Word 的窗口位置。

我的 WinForms (C#) 应用程序占据屏幕的下半部分并启动 Word 实例。我需要这个实例占据屏幕的上半部分。

这无法通过互操作进行控制,因此我设法获取了 Word 实例的窗口句柄。然后我尝试以各种方式使用 SetWindowPos、SetWindowsLong 等,但没有任何可见的结果。

我不想重新发布有缺陷的代码,而是想要一个全新的开始,因此欢迎取得此类成就的人提出任何建议。

【问题讨论】:

  • 您可以将 word 嵌入为 activex 控件。看看这是否适合你。

标签: c# .net winapi interop ms-word


【解决方案1】:

您可以使用Application.Move 方法。或直接设置Application.TopApplication.Left 属性。你的代码可以是这样的:

private Word.Application WordApp = new Word.Application();

    ...

private void buttonClick(object sender, System.EventArgs e)
{
    if (this.openFileDialog.ShowDialog() == DialogResult.OK)
    {
        object fileName = openFileDialog.FileName;
        object visible = true;
        object missing = System.Reflection.Missing.Value;

        WordApp.Visible = true;
        Word.Document aDoc = 
                        WordApp.Documents.Open(ref fileName, ref missing, ref missing,
                                               ref missing, ref missing, ref missing,
                                               ref missing, ref missing, ref missing,
                                               ref missing, ref missing, ref visible);
        aDoc.Activate();
        WordApp.Top = 0;
    }
}

不要忘记将 Microsoft Word 对象库 添加到您的引用并使用相关的命名空间:

using Microsoft.Office.Interop.Word;

【讨论】:

  • 谢谢。我不知道 Move 和 Dimension 属性。会尝试一下并很快恢复。
  • @RaheelKhan:感谢关注
  • 不客气,感谢您的回答。我注意到,当我使用 Screen.PrimaryScreen.WorkingArea.Height 计算屏幕大小时,我必须减去大约 120 个像素才能使其垂直适合屏幕。有什么想法会导致这种情况吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多