如何将Microsoft Word设置为VB6表单的所有者?

时间:2019-01-29 08:04:46

标签: ms-word vb6

我们正在创建Microsoft Word加载项。在此加载项中,有一个无模式的表单,我们也根据需要将其设置为“最热门”表单。当我们最小化Microsoft Word应用程序时,不会最小化无模式形式。如果我们能够将MS Word设置为此表单的所有者,则将解决此问题。请告诉我一种方法。

这是我当前用于加载表单的代码。

Dim Test As Long 

With frmSelectStyle
 .aaInitialize SelectStyleDlg:=Me
 .Show vbModeless
End With

Test = SetTopMostWindow(frmSelectStyle.hwnd, True)

我知道我们可以将所有者设置为如下形式。

frmSelectStyle.Show vbModeless , frmMain 

但就我而言,我需要将MS Word设置为所有者。请帮我。

1 个答案:

答案 0 :(得分:5)

关键是使用SetWindowLong Windows API函数。您可以将该函数包装在自己的“ SetOwner”函数中,以使其易于使用。 SetOwner接受两个Long Windows句柄:第一个用于无模式窗口,第二个用于Word Application主窗口(以下代码是最初在DevX.com上发布的代码的变体)。

我已经测试了下面的代码,如果将Word最小化,则无模式窗口会与Word一起最小化。

Option Explicit

Private hwndOriginal As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" _
    (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Const GWL_HWNDPARENT = (-8)
Function SetOwner(ByVal HwndtoUse, ByVal HwndofOwner) As Long

    SetOwner = SetWindowLong(HwndtoUse, GWL_HWNDPARENT, HwndofOwner)

End Function
Private Sub Form_Load()

   Dim hWndWord As Long

   ' start an instance of Microsoft Word
   Dim WordApp As Word.Application
   Set WordApp = CreateObject("Word.Application")

   ' make sure it's visible
   WordApp.Application.Visible = True

   ' use the FindWindow API to find a window class of "OpusApp" with the specified Word-application caption
   hWndWord = FindWindow("OpusApp", WordApp.Caption)

   hwndOriginal = SetOwner(Me.hWnd, hWndWord)

End Sub

Private Sub Form_Unload(Cancel As Integer)

   SetOwner Me.hWnd, hwndOriginal

End Sub