Link to home
Start Free TrialLog in
Avatar of vitanza
vitanza

asked on

form that's always-on-top

This is a pretty simple question:
My main form opens another small form. I want the smaller form to always be on top of the main form until the smaller one is closed out. In VB .NET, I think a form has an "AlwaysOnTop" property, but I'm working in VB6 right now and I can't figure out how to do it.
ASKER CERTIFIED SOLUTION
Avatar of SpineyNorman
SpineyNorman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GivenRandy
GivenRandy

If you only want your form to be at the top of other forms within your app, rather than being on top of all windows (including other apps):

frmTop.Show vbModeless, frmParent

This will bring the form above its parent (specified as the second parameter).

If a MessageBox appears below the TopMost window and you want it above it instead, then use the vbSystemModal constant in the MsgBox function's Style attribute (this forces the MessageBox to show on top of the AlwaysOnTop form).

'you need to use API functions:

'Win32API functions
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

'Win32API constants
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2

Private Sub Command1_Click()
    SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
End Sub

Private Sub Command2_Click()
    SetWindowPos Me.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
End Sub