Visual Basic Programming Code Examples
Visual Basic > Forms Code Examples
How to make a form stay 'on top' of all other windows 32bit.
How to make a form stay 'on top' of all other windows 32bit.
'This version has been tested with VB4-32
'Here's a cool little API function for VB4 on Win 32s.
'It basically allows a window to "Always be on top"
This goes in the declarations section of a module
Global Const SWP_NOMOVE = 2
Global Const SWP_NOSIZE = 1
Global Const HWND_TOPMOST = -1
Global Const HWND_NOTOPMOST = -2
Global Const FLOAT = 1, SINK = 0
Public Declare Sub 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)
This is a sub within the module
Sub FloatWindow(X As Integer, action As Integer)
' When called by a form:
'
' If action <> 0 makes the form float (always on top)
' If action = 0 "unfloats" the window.
'
Dim wFlags As Integer, result As Integer
wFlags = SWP_NOMOVE Or SWP_NOSIZE
If action <> 0 Then ' Float
Call SetWindowPos(X, HWND_TOPMOST, 0, 0, 0, 0, wFlags)
Else ' Sink
Call SetWindowPos(X, HWND_NOTOPMOST, 0, 0, 0, 0, wFlags)
End If
End Sub
Then to call the procedure you use this code
Dim f as integer
f = Screen.ActiveForm.hWnd
Call FloatWindow(f, FLOAT)
to "float" the form, ie. allways ontop
or
Dim f as integer
f = Screen.ActiveForm.hWnd
Call FloatWindow(f, SINK)
to sink.
Alternatively instead of Screen.ActiveForm.hWnd you can specify a window, ie
Me.hWnd to float/sink the current form.