Visual Basic Programming Code Examples
Visual Basic > Forms Code Examples
Changing the parent handle-owner of a dialog
Changing the parent handle-owner of a dialog
In certain circumstances (eg. showing out of process dialogs) you may want to change the parent handle of a dialog. Another example might be if you where writing an application which uses Excel. In this case you may not want the user to be able to access your copy of Excel. If the user double clicks a Excel workbook in Explorer it may automatically load in your instance of Excel. The following code changes the parent handle of Excel and effectively creates a private copy of Excel inside notepad. Note, this technique should only really be used by programmers who understand the implications of such changes (it effects the dialogs behaviour with respect to repainting and resizing, which can be ignored for hidden instances of Excel).
Option Explicit
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
'Purpose : Sets the Parent Hwnd to lNewParentHwnd
'Inputs : lNewParentHwnd The handle of the parent form.
' lFormHwnd The handle of the form to change the Parent Hwnd.
' sAppTitle If do not have form handle then pass in the title of the
' application to set the new Parent handle.
'Outputs : The original Parent handle of the form
'Notes :
'Revisions :
Function SetParentHwnd(Optional lNewParentHwnd As Long, Optional lFormHwnd As Long, Optional sAppTitle As String, Optional bSetToDesktop As Boolean) As Long
If lFormHwnd = 0 Then
'Find Hwnd
lFormHwnd = DialogHwnd(sAppTitle)
End If
If lFormHwnd Then
'Return current parent
'Note: May return 0 if the form is running from the desktop
SetParentHwnd = GetParent(lFormHwnd)
'Set the New Parent
If bSetToDesktop Then
'Get desktop Hwnd
lNewParentHwnd = GetDesktopWindow
If lNewParentHwnd Then
'Set parent as Desktop
SetParent lFormHwnd, lNewParentHwnd
End If
Else
'Set parent to specified hwnd
SetParent lFormHwnd, lNewParentHwnd
End If
End If
End Function
'Returns the Windows Handle of a Dialog
Function DialogHwnd(ByVal DialogCaption As String) As Long
DialogCaption = DialogCaption & vbNullChar
On Error Resume Next
DialogHwnd = FindWindowA(vbNullString, DialogCaption)
On Error GoTo 0
End Function
'Demonstration routine
'Shows you how to put Excel in a notepad
Sub Test()
Dim ExApp As Object, lOldHwnd As Long
On Error Resume Next
'Create instance of Excel
Set ExApp = CreateObject("Excel.Application")
'Open Notepad
Shell "Notepad", vbMaximizedFocus
DoEvents
'Change Excels Parent window handle
'Note: As you will quickly see, Excel isn't happy runing in notepad
lOldHwnd = SetParentHwnd(DialogHwnd("Untitled - Notepad"), , ExApp.Caption)
ExApp.Visible = True
MsgBox "Excel in Notepad!", vbExclamation + vbSystemModal
'Restore Excel's Parent to the desktop
SetParentHwnd 0, , ExApp.Caption, True
MsgBox "Excel's back to normal!", vbExclamation + vbSystemModal
'Quit Excel
ExApp.Quit
Set ExApp = Nothing
End Sub