我有一个 VB.NET 应用程序,它使用 SetParent API 将 Worddocument 放入我的应用程序的 GroupBox 控件中。
Public Class myForm
Dim mwrdApp As Microsoft.Office.Interop.Word.Application
Dim mwrdDoc As Microsoft.Office.Interop.Word.Document
Dim mwrdHwnd As Integer
Dim sTemp As String
Public Structure RECT 'for GetWindowRect API
Dim Left As Integer
Dim Top As Integer
Dim Right As Integer
Dim Bottom As Integer
End Structure
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Declare Function GetWindowRect Lib "user32" (ByVal Hwnd As Integer, ByRef lpRect As RECT) As Integer
Declare Function MoveWindow Lib "user32" (ByVal Hwnd As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Integer) As Integer
Declare Function SetParent Lib "user32" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
Private Sub myForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
mwrdApp = New Microsoft.Office.Interop.Word.Application
mwrdDoc = mwrdApp.Documents.Add
sTemp = mwrdDoc.ActiveWindow.Caption 'save document-caption
mwrdDoc.ActiveWindow.Caption = "besuretofindthisinstance" 'set detectable caption
mwrdHwnd = FindWindow("OpusApp", mwrdDoc.ActiveWindow.Caption & " - " & mwrdApp.Caption) 'find Word window handle
mwrdDoc.ActiveWindow.Caption = sTemp 'restore original caption
mwrdApp.Visible = True
mwrdApp.ScreenUpdating = True
mwrdDoc.ActiveWindow.Visible = True
MsgBox("Worddocument-window before SetParent")
SetParent(mwrdHwnd, myGroupBox.Handle.ToInt32) 'put Word in myGroupBox
Dim myGroupBoxRect As RECT
GetWindowRect(myGroupBox.Handle.ToInt32, myGroupBoxRect) 'Get size of myGroupBox
MoveWindow(mwrdHwnd, 0, 0, myGroupBox.Right - myGroupBox.Left, myGroupBox.Bottom - myGroupBox.Top, True) 'Size the Word window to fit inside myGroupBox:
End Sub
End Class
在桌面上打开 Word 后,代码与消息框一起停止,然后 Wordwindow (Word 2013) 看起来完全正常。
然后 SetParent-API 将 Wordwindow 从桌面移动到 myForm 上的 myGroupBox。到目前为止,这在任何操作系统上都运行良好,但是我最近将我的应用程序切换到 Windows-8(在 MS Surface Pro 3 上),现在在 SetParent 之后,带框的 Wordwindows 显示增加的菜单和功能区。现在 Word 菜单和功能区控件中的所有标题和选项卡突然变大了;字体大小要大得多(顺便说一句:Word 本身运行正常,Worddocument 本身中的任何文本也不受影响)。
有人知道这是怎么发生的吗?之后可以以编程方式防止或纠正这种情况吗?