profezzional
profezzional

Reputation: 33

VB6 Don't Auto-Activate Child Form on Form-Resize Windows 10

A window in this VB6 legacy system that I'm supporting hosts a Word 2007 instance as a child window. On Windows 7, the parent window can be resized without any issues: the window doesn't update until the mouse is released after resizing. On Windows 10, however, the window updates dynamically while being resized.

The issue I'm encountering in Windows 10 is that the child Word window is getting activated/focused upon the first update: you can only drag to resize the parent window by a couple pixels at a time, before the child window gets focused and the resize event on the parent window is canceled (the cursor is still on the resize icon, but continued dragging has no effect). Maximizing, minimizing, and restoring the parent window all work normally. Once the child Word window is closed (through the file menu in Word), the parent window can be resized normally, because there's no child window to activate/focus. The same automatic-child-window-activation-after-parent-window-resizing occurs in Windows 7, but because the resize event doesn't fire until after the parent window has actually updated, it's not an issue there.

My conundrum is that I don't see anything in the code that suggests why the child window is getting automatically activated/focused, unless that's just default Windows behavior. In either case, I'm pretty sure I need a way to make that not happen.

All that this code is explicitly doing (primarily the ResizeControls() sub; the rest is here mostly for context) is resizing/positioning the Word window to correspond to the new size of the container in the parent window, which is consistent with the behavior in Windows 7.

From what I can tell, I don't believe that GetWindow() actually activates the window it gets a handle to, but if it does, then that's likely the cause of the issue, in which case I need to be able to get a handle to the window without activating it.

PDFView.frm:

Begin VB.Form frmPDFView
    Caption         =   "Untitled"
    ClientHeight    =   8655
    ClientLeft      =   1320
    ClientTop       =   1665
    ClientWidth     =   9270
    ' ...

    Begin VB.PictureBox picContainer 
        BackColor       =   &H00FFFFFF&
        Height          =   4215
        Left            =   1080
        ScaleHeight     =   4155
        ScaleWidth      =   4995
        TabIndex        =   0
        Top             =   120
        Width           =   5055
    End
End

Private Sub ResizeControls()
On Error Resume Next

    Dim pWndChild As Long
    Dim r As RECT
    Dim rtn As Long

    picContainer.Left = 100
    picContainer.Height = Me.Height - 1300
    picContainer.Width = Me.Width - 350
    picContainer.Top = 300

    pWndChild = GetWindow(picContainer.hWnd, GW_CHILD)
    rtn = GetLastError

    If (pWndChild) Then
        rtn = GetClientRect(picContainer.hWnd, r)
        rtn = SetWindowPos(pWndChild, 0, 0, 0, r.Right - r.Left, r.Bottom - r.Top, SWP_NOZORDER Or SWP_NOMOVE)
    Else
        rtn = GetLastError
    End If

End Sub

Private Sub Form_Resize()
On Error GoTo ERROR_HANDLER

    Call ResizeControls

    Exit Sub

ERROR_HANDLER:
    Err.Clear
    Resume Next

End Sub

Upvotes: 2

Views: 256

Answers (1)

profezzional
profezzional

Reputation: 33

Turns out I'm blind and/or didn't read the documentation thoroughly enough for all the Windows functions used in the code. Found the solution the next day and forgot to come back and answer this, but as confirmed by @wqw's comment, the issue was with SetWindowPos(). The SWP_NOACTIVATE flag needed to be passed in to SetWindowPos() to prevent activation of the target window (in my case, the child Word window).

Upvotes: 1

Related Questions