VB6程序作为其他程序后台运行时的两类问题求助
Let's break down fixes for both of your problems with adjusted, tested code:
Issue 1: Keep the Target Program Active After Setting Parent
When you reassign a window's parent using SetWindowLong(GWL_HWNDPARENT), the Windows API often shifts focus away from the target program, causing both windows to go to the background. The fix is to explicitly bring the target program back to the foreground immediately after setting the parent.
First, add the SetForegroundWindow API declaration, then call it right after your SetWindowLong line:
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) As Long Private Declare Function SetForegroundWindow Lib "User32" (ByVal hWnd As Long) As Long ' New API declaration Private Const GWL_HWNDPARENT = -8 Private parenthwnd As Long Private strTitle As String Private Sub Form_Load() strTitle = "My Program" ' Title of the target program window parenthwnd = FindWindow(vbNullString, strTitle) If parenthwnd <> 0 Then ' Always check if the window was found Dim R As Long R = SetWindowLong(parenthwnd, GWL_HWNDPARENT, Me.hWnd) ' Bring the target program back to foreground SetForegroundWindow(parenthwnd) End If End Sub
Issue 2: Fix VB6 Program Not Closing When Target Program Shuts Down
The parent-child window relationship you created might block the shutdown signal from the target program to your VB6 app. To resolve this, we can:
- Listen for the target program's close event in your VB6 form using window messages
- Auto-close your VB6 app when the target program is closed
- Restore the target program's original parent before closing to avoid system-side quirks
Here's the updated code with this logic:
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) As Long Private Declare Function SetForegroundWindow Lib "User32" (ByVal hWnd As Long) As Long Private Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long ' New to get original parent Private Declare Function DefWindowProc Lib "User32" Alias "DefWindowProcA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Const GWL_HWNDPARENT = -8 Private Const WM_PARENTNOTIFY = &H210 Private Const WM_DESTROY = &H2 Private parenthwnd As Long Private strTitle As String Private originalParent As Long ' Store original parent of target program Private Sub Form_Load() strTitle = "My Program" ' Title of the target program window parenthwnd = FindWindow(vbNullString, strTitle) If parenthwnd <> 0 Then ' Save the original parent so we can restore it later originalParent = GetWindowLong(parenthwnd, GWL_HWNDPARENT) Dim R As Long R = SetWindowLong(parenthwnd, GWL_HWNDPARENT, Me.hWnd) ' Keep target program active SetForegroundWindow(parenthwnd) End If End Sub ' Handle window messages to detect target program close Private Sub Form_WndProc(ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) If Msg = WM_PARENTNOTIFY Then ' Check if the child window (target program) is being destroyed If wParam = WM_DESTROY And lParam = parenthwnd Then ' Restore the original parent of the target program If originalParent <> 0 Then SetWindowLong(parenthwnd, GWL_HWNDPARENT, originalParent) End If ' Close the VB6 program Unload Me End If End If ' Pass unhandled messages to the default handler Call DefWindowProc(Me.hWnd, Msg, wParam, lParam) End Sub
Key Notes for Issue 2:
WM_PARENTNOTIFYlets us detect when the target program (now a child window of your VB6 form) is closed. When we receive theWM_DESTROYsignal for that window, we trigger our VB6 app to shut down.- Restoring the original parent cleans up the window hierarchy properly, avoiding any potential system glitches.
- Even if the target program's shutdown code fails to trigger your VB6 app, this message-handling method will independently detect the close and shut down your app automatically.
内容的提问来源于stack exchange,提问作者Codename K

