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

Create a Topmost or Floating 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
Create a Topmost or Floating Dialog A floating (or Topmost) window is a window that remains constantly above all other windows, even when it is not active. The following routines can be used to force a form to remain visible (float) or restore the forms natural behaviour. Option Explicit '--------------Set Dialog to Top API--------------- 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 Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Const SW_NORMAL As Long = 1 Private Const SW_MAXIMIZE As Long = 3 Private Type POINTAPI x As Long y As Long End Type Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type WINDOWPLACEMENT Length As Long Flags As Long showCmd As Long MinPosition As POINTAPI MaxPosition As POINTAPI NormalPosition As RECT End Type 'Purpose : VBA Routine to permanently place a Dialog on top of all other dialogs (i.e. a floating dialog) 'Inputs : sDialogCaption The caption of the dialog to float 'Outputs : This enables the dialog to be shown above all other dialogs. 'Example : ' Call DialogToTopVBA(Me.Caption) 'Place code inside a userform 'Notes : 'Revisions : Sub DialogToTopVBA(sDialogCaption As String) Dim tWinPlace As WINDOWPLACEMENT, lDialogHwnd As Long Const HWND_TOPMOST As Long = -1 lDialogHwnd = GetDialogHwnd(sDialogCaption) If lDialogHwnd Then tWinPlace.Length = Len(tWinPlace) GetWindowPlacement lDialogHwnd, tWinPlace SetWindowPos lDialogHwnd, HWND_TOPMOST, tWinPlace.NormalPosition.Left, tWinPlace.NormalPosition.Top, 0, 0, SW_NORMAL End If End Sub 'Purpose : VB Routine to restore a Dialog's natural Z Order 'Inputs : sDialogCaption The dialog caption to restore to Z Order of. 'Outputs : N/A 'Example : ' Call DialogToNormalVBA(Me.Caption) 'Place code inside a userform 'Notes : This routine is only required after calling DialogToTop. 'Revisions : Sub DialogToNormalVBA(sDialogCaption As String) Dim tWinPlace As WINDOWPLACEMENT, lDialogHwnd As Long Const HWND_NOTOPMOST As Long = -2 lDialogHwnd = GetDialogHwnd(sDialogCaption) If lDialogHwnd Then tWinPlace.Length = Len(tWinPlace) GetWindowPlacement lDialogHwnd, tWinPlace SetWindowPos lDialogHwnd, HWND_NOTOPMOST, tWinPlace.NormalPosition.Left, tWinPlace.NormalPosition.Top, 0, 0, SW_NORMAL End If End Sub 'Purpose : Returns the Windows Handle of a Dialog based on its caption. ' Find windows scans down the Z Order of the dialogs currently displayed, ' before returning the handle of the matching dialog (use Spy ++ to find the class names). 'Inputs : DialogCaption as String 'Outputs : The Dialogs Window Handle 'Example : To Call in VBA use ' lHwnd = GetDialogHwnd(Me.Caption) 'Place inside a userform 'Notes : 'Revisions : Function GetDialogHwnd(ByVal sDialogCaption As String) As Long GetDialogHwnd = FindWindowA(vbNullString, sDialogCaption) End Function 'Purpose : VB Routine to permanently place a Dialog on top of all other dialogs (i.e. a floating dialog) 'Inputs : oForm The form to float (i.e. DialogToTopVB Me) 'Outputs : This enables the dialog to be shown floating above all other dialogs. 'Notes : 'Revisions : Sub DialogToTopVB(oForm As Form) Dim WinPlace As WINDOWPLACEMENT Const HWND_TOPMOST As Long = -1 WinPlace.Length = Len(WinPlace) GetWindowPlacement oForm.hwnd, WinPlace If oForm.WindowState = vbMaximized Then SetWindowPos oForm.hwnd, HWND_TOPMOST, WinPlace.NormalPosition.Left, WinPlace.NormalPosition.Top, 0, 0, SW_MAXIMIZE Else SetWindowPos oForm.hwnd, HWND_TOPMOST, WinPlace.NormalPosition.Left, WinPlace.NormalPosition.Top, 0, 0, SW_NORMAL End If End Sub 'Purpose : VB Routine to restore a Dialog's natural Z Order 'Inputs : oForm The form to float (i.e. DialogToNormalVB Me) 'Outputs : N/A 'Notes : This routine is only required after calling DialogToTop. 'Revisions : Sub DialogToNormalVB(oForm As Form) Dim WinPlace As WINDOWPLACEMENT Const HWND_NOTOPMOST As Long = -2 WinPlace.Length = Len(WinPlace) GetWindowPlacement oForm.hwnd, WinPlace If oForm.WindowState = vbMaximized Then SetWindowPos oForm.hwnd, HWND_NOTOPMOST, WinPlace.NormalPosition.Left, WinPlace.NormalPosition.Top, 0, 0, SW_MAXIMIZE Else SetWindowPos oForm.hwnd, HWND_NOTOPMOST, WinPlace.NormalPosition.Left, WinPlace.NormalPosition.Top, 0, 0, SW_NORMAL End If End Sub