How can you make an application/form always on top?
I'm using VB 6.0 Enterprise Ed.
Printable View
How can you make an application/form always on top?
I'm using VB 6.0 Enterprise Ed.
Code on the form:
Option Explicit
Private Sub Command1_Click()
Dim lR As Long
lR = SetTopMostWindow(Form1.hwnd, True)
End Sub
Private Sub Command2_Click()
Dim lR As Long
lR = SetTopMostWindow(Form1.hwnd, False)
End Sub
Code on a module:
Option Explicit
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" Alias "SetWindowPos" _
(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
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
This has always worked for me.
Code:
#If Win32 Then
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
#Else 'Win16
Private Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, _
ByVal hWndInsertAfter As Integer, ByVal X As Integer, _
ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, _
ByVal wFlags As Integer)
#End If
Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Private Sub Form_load()
#If Win32 Then
Dim lResult As Long
lResult = SetWindowPos(Me.hWnd, HWND_TOPMOST, _
0, 0, 0, 0, FLAGS)
#Else '16-bit API uses a Sub, Not a Function
SetWindowPos Me.hWnd, HWND_TOPMOST, _
0, 0, 0, 0, FLAGS
#End If
end sub
Hope this helps
thanks guys! that was fast.