SetParent API under Windows-8 changes font size of application menu and ribbon

284 views Asked by At

I'm have a VB.NET application using the SetParent API to put a Worddocument inside a GroupBox-control of my application.

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

After opening Word on the desktop the code is halted with the messagebox, then the Wordwindow (Word 2013) is looking totally normal.

Then the SetParent-API moves the Wordwindow from the desktop into myGroupBox on myForm. This has been working well with any operating system until now, however I recently switched my application to Windows-8 (on a MS Surface Pro 3) and now after the SetParent the framed Wordwindows shows increased menus and ribbon. Now all titles and tabs in the Word-menu and the ribbon controls have increased sizes all of a sudden; the font size is much larger (BTW: Word itself is functioning correctly and also any text in the Worddocument itself is not affected).

Has anybody an idea how this can happen? Can this programmatically be prevented or otherwise corrected afterwards?

1

There are 1 answers

1
Eugene Astafiev On

What you are trying to achieve is not supported oficically. You can embed your application into Word (but not the reverse) developing an add-in with a task pane. Consider using any third-party components for displaying Word documents on Windows Forms.

Also take a look at the following similar threads: