Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Forms Code Examples

Changing the parent handle-owner of a dialog

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
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