gpt4 book ai didi

c# - Microsoft Word 自动化/互操作 Win32 API 问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:19:42 37 4
gpt4 key购买 nike

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

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

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

我不想重新发布错误代码,而是希望重新开始,因此欢迎已经取得类似成就的人提出任何建议。

最佳答案

您可以使用 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;

关于c# - Microsoft Word 自动化/互操作 Win32 API 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11431724/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com