How to Set Windows Form Always on Top of Other Applications in VB6

11:02 PM Unknown 1 Comments

The following code snippets allows to set Windows Form Always on Top of Other applications in Visual Basic 6.0.

Public Const SWP_NOMOVE = 2
Public Const SWP_NOSIZE = 1
Public Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2

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


The following SetTopMostWindow() function takes two parameters as follows:
hWnd: the window handle
Topmost: It is either True to set the on top or False if Not.
Public Function SetTopMostWindow(hWnd As Long, Topmost As Boolean) As Long

If Topmost = True Then 'Make the window topmost
SetTopMostWindow = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
Else
SetTopMostWindow = SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)
SetTopMostWindow = False
End If
End Function

The following Sub sets the form frmMini on Top of any other applications.
Private Sub Form_Load()
Dim lR As Long
lR = SetTopMostWindow(frmMini.hWnd, True)
End Sub

1 comment: