VB6 - Issues with being in the background to another program window?

181 views Asked by At

I have created a VB6 program which runs in the background to another program. It means the program window will be in the back only to this other program. I am using this code for it,

Private Declare Function FindWindow1 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 Const GWL_HWNDPARENT = -8

Private parenthwnd As Long
Private strTitle As String

Private Sub Form_Load()
        strTitle = "My Program" 'Title of the program window
        parenthwnd = FindWindow1(vbNullString, strTitle)
        Dim R As Long
        R = SetWindowLong(parenthwnd, GWL_HWNDPARENT, Me.hWnd)
End Sub

The other program will run this VB6 program which sets it-self to the background to the other program window. It works. But there are two problems.

  1. When the VB6 program executes the code R = SetWindowLong(parenthwnd, GWL_HWNDPARENT, Me.hWnd), the other program along with VB6 program goes to the background. How to make other program active when it is run and the VB6 program is executed?
  2. When the other program is closed, it has the code to terminate the VB6 program. But this does not close the VB6 program. I think this may be due to running the code R = SetWindowLong(parenthwnd, GWL_HWNDPARENT, Me.hWnd). How to fix this?
1

There are 1 answers

0
Bill Hileman On

If I understand you question correctly (and I'm not sure I do) here's the code I use to send a form to the background or send the form to the top. Perhaps it's what you're looking for.

' Declares and constants for BringToFront and SendToBack

    Public Declare Function SetWindowPos Lib "user32" (ByVal _
        hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As _
        Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As _
        Long, ByVal wFlags As Long) As Long

    Public Const SWP_NOMOVE = &H2
    Public Const SWP_NOSIZE = &H1
    Public Const HWND_BOTTOM = 1
    Public Const HWND_TOP = 0

    Public Sub BringToFront(frm As Form)

       Dim flags As Long

       flags = SWP_NOSIZE Or SWP_NOMOVE
       SetWindowPos frm.hWnd, HWND_TOP, 0, 0, 0, 0, flags

    End Sub

    Public Sub SendToBack(frm As Form)

       Dim flags As Long

       flags = SWP_NOSIZE Or SWP_NOMOVE
       SetWindowPos frm.hWnd, HWND_BOTTOM, 0, 0, 0, 0, flags

    End Sub