Link to home
Start Free TrialLog in
Avatar of HATCHET
HATCHET

asked on

(50 pts) Making another window a child

I'd like to know the CORRECT way to make another application a child to your application.  I'm pretty sure you use the SetParent API, but I tried it and can't seem to get it to work right.

I'll award the 50 points to the first to correctly provide sample code on how to make a program like NOTEPAD a child to your application... making your application act like an MDI form and NOTEPAD like an MDI child form,

Thank You,

HATCHET
Avatar of y2kwacko
y2kwacko

make your form an MDI Parent then add this to your form

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Private Sub MDIForm_Load()
Dim NotePad As Long, st_parent As Long

NotePad& = FindWindow("Notepad", vbNullString)
st_parent& = SetParent(NotePad&, Me.hWnd)
End Sub

Enjoy,
Kevin
P.S. Make sure notepad is loaded before running your program
ASKER CERTIFIED SOLUTION
Avatar of asaflahv1
asaflahv1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HATCHET

ASKER

y2kwacko,

So you're saying make my program an MDI and then make the 3rd party software a "TRUE" child window in it?  Never thought of that.    =]   I'll give it a try and see what happens.

HATCHET
Avatar of HATCHET

ASKER

y2kwacko,

I did try your solution and it DID work... however, I hate working with MDI forms and its just not the best solution.  Sorry.

__________________________________________________________

asaflahv1,

I tried your code, but it would not work either.  Didn't give any errors, but just didn't work.  I did take your idea of using the PictureBox control as the parent object and used the following code that DID work.  Because you were close to the answer and gave me the idea for the answer, I'll give you the points.  Please answer the question.

__________________________________________________________


Option Explicit

' Function Declarations
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

' Variable Declarations
Private Handle As Long
Private OldParent As Long

Private Sub Form_Load()
 
  Dim MyAnswer As VbMsgBoxResult
  Dim Result As Long
 
FindWind:
 
  ' Check if NOTEPAD is running
  Handle = FindWindow("Notepad", vbNullString)
 
  ' If it's not, then prompt the user to start it and then continue.
  If Handle = 0 Then
    MyAnswer = MsgBox("Click OK when notepad is open.", vbOKCancel, " ")
    If MyAnswer = vbCancel Then
      Unload Me
      Exit Sub
    Else
      GoTo FindWind
    End If
 
  ' If it is running, continue
  Else
    ' Get the old parent so you can return it there later
    OldParent = GetParent(Handle)
    DoEvents
   
    ' Set the NOTEPAD's new parent to the PictureBox control
    Result = SetParent(Handle, Picture1.hwnd)
   
    ' Check if it worked, and if it did, move it to the top corner
    If Result = 0 Then
      MsgBox "Error setting the window as a child.", vbOKOnly + vbExclamation, " "
      Unload Me
      Exit Sub
    Else
      MoveWindow Handle, 0, 0, 500, 500, True
    End If
  End If
 
End Sub

Private Sub Form_Unload(Cancel As Integer)
 
  ' Set NOTEPAD's parent back to what it was before
  SetParent Handle, OldParent
 
End Sub
Avatar of HATCHET

ASKER

Adjusted points to 50