by Dennis Wrenn (2 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating:
(2 Votes)
This code keeps a form on top of all other windows.
API DeclarationsPrivate 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 Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_FLAGS = SWP_NOMOVE Or SWP_NOSIZE
'code:
Private Sub FormOnTop(frm As Form, blnOnTop As Boolean)
Dim lPos As Long
Select Case blnOnTop
Case True
lPos = HWND_TOPMOST
Case False
lPos = HWND_NOTOPMOST
End Select
Call SetWindowPos(frm.hwnd, lPos, 0, 0, 0, 0, SWP_FLAGS)
End Sub
'usage:
Private Sub Form_Load()
'makes a form on top
Call FormOnTop(Me, True)
End Sub
Private Sub Command1_Click()
'makes a form not always on top anymore..
Call FormOnTop(Me, False)
End Sub