VB and VBA Users Source Code: Create a Topmost or Floating Dialog
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Create a Topmost or Floating Dialog
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, March 08, 2001
Hits:
904
Category:
Windows API
Article:
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. 'Author : Andrew Baker (VBusers.com) 'Date : 30/05/2000 '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 'Author : Andrew Baker (VBusers.com) 'Date : 30/05/2000 '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 'Author : Andrew Baker 'Date : 30/05/2000 '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. 'Author : Andrew Baker (VBUsers.com) 'Date : 30/05/2000 '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 'Author : Andrew Baker (VBUsers.com) 'Date : 30/05/2000 '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
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder