1

I used the following VB6 code,

Private 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
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_NOMOVE = 2
Private Const SWP_NOSIZE = 1

Private Sub Form_Activate()
    Dim R As Long
    R = SetWindowPos(frmSlide.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Sub

and the form is set with these properties,

   MaxButton       =   False
   MinButton       =   False
   ShowInTaskbar   =   False
   StartUpPosition =   CenterScreen
   WindowState     =   Maximized

This is to make the form go to the background. It did not go to the background. The idea here is to make the form only go one window back. For example: if the Notepad program window is open. This program only need to be in the background of Notepad and not other program windows. Is this possible?

Codename K
  • 890
  • 4
  • 23
  • 52
  • 1
    Is `frmSlide` the same form being used by the Activate event? Does `frmSlide.hwnd` have a value before you call SetWindowPos – dbmitch May 07 '18 at 00:36
  • Yes, `frmSlide` is the same form used. What do you mean by _have a value_? – Codename K May 07 '18 at 07:52
  • 1
    if you put a debug stop on that line and hover over the `frmslide.hWnd` does it show 0 or a long integer? You can add a Watch on it or type in Debug.Print or use a MsgBox to troubleshoot and determine its value if it has one. – dbmitch May 07 '18 at 15:02
  • 1
    `frmslide.hWnd` has the value `1507988`. – Codename K May 08 '18 at 07:59
  • 1
    Are you trying to keep the form behind everything else - like a background desktop window? As soon as it is activated you deactivate it? – dbmitch May 08 '18 at 17:10
  • I am trying keep it similar to the old installers which had [full screen backgrounds](https://i.stack.imgur.com/27aiQ.png). One program will run the VB6 program and it needs to be in the background only to this program that run it. – Codename K May 08 '18 at 20:45
  • 1
    Okay - but that;s not a 3rd party app like Notepad that you're bringing to the front. That's a form in your same application. Or what you want is to put your installer window in the front. That should be easier than trying to deactivate the activate event of a form – dbmitch May 08 '18 at 21:30
  • The front window is not in the same VB6 program. The front program will run the VB6 program and the VB6 program needs to be in the back only to this front program. Is this possible? – Codename K May 09 '18 at 06:16

2 Answers2

1

I did some research based on Tarun Lalwani's information and this is what worked for me,

Add a Timer to the form and use this code,

Option Explicit

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

Public Function FindWindowHandle(Caption As String) As Long
  FindWindowHandle = FindWindow1(vbNullString, Caption)
End Function

Private Sub Form_Load()
    On Error Resume Next
    strTitle = "Untitled - Notepad"

    With Timer1
        .Interval = 2000
        .Enabled = True
    End With
End Sub

Private Sub Timer1_Timer()
    If FindWindowHandle(strTitle) <> 0 Then
        Timer1.Enabled = False
        parenthwnd = 0
        parenthwnd = FindWindow1(vbNullString, strTitle)
        Dim R As Long
        R = SetWindowLong(parenthwnd, GWL_HWNDPARENT, Me.hWnd)
    End If
End Sub

When the Notepad is openned it will be the parent to this form.

Warning: I have set the form properties to these,

   MaxButton       =   False
   MinButton       =   False
   ShowInTaskbar   =   False
   StartUpPosition =   CenterScreen
   WindowState     =   Maximized

If you set the same properties make sure you add a button or any other method to close the form. Otherwise the form will be on top and it may be difficult to close it.

Codename K
  • 890
  • 4
  • 23
  • 52
0

Yes, you can do this. What you need to do is find the window handle of your external program and then tell windows that your form should be treated as a parent

SetWindowLong(hwndChild, GWL_HWNDPARENT, hwndOwner)

PS: Credits to https://stackoverflow.com/a/834509/2830850

Also see the below SO Thread as well

Win32 window Owner vs window Parent?

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265