
The following example shows how to make a form stay on top of other forms. To try this example, paste the SetWindowPos Function declaration into the module level section of a BAS file. Place the remainder of the procedure in a BAS module also. To run the code, call the AlwaysOnTop sub from the form you want to stay on top as shown in the Usage section of the comments within the procedure.
Sub AlwaysOnTop (FrmID as Form, OnTop as Integer)
' ===========================================
' Requires the following declaration
' For VB3:
' Declare Function SetWindowPos Lib "user" (ByVal h%, ByVal hb%, ByVal x%, ByVal Y%, ByVal cx%, ByVal cy%, ByVal f%) As Integer
' For VB4:
' 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
' ===========================================
' Usage:
' AlwaysOnTop Me, -1 ' To make always on top
' AlwaysOnTop Me, -2 ' To make NOT always on top
' ===========================================
Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
If Ontop = -1 then
OnTop = SetWindowPos(FrmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
Else
OnTop = SetWindowPos(FrmID.hWnd,HWND_NOTOPMOST,0,0,0,0,FLAGS)
End If
End Sub