Setting Form on Top of ALL (using API):   Return to List

Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const LB_ITEMFROMPOINT = &H1A9

Public Const WM_NCLBUTTONDOWN = &HA1
Public Const HTCAPTION = 2

Public 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

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function ReleaseCapture Lib "user32" () As Long

' Public Function (place it in a globals module) to set the window position as On Top or Not
Public Function SetWinPos(lHWnd As Long, MyOpt As Boolean) As Boolean
    Dim lWinPos As Long' A variable to hold the the value of API window position constant
    Dim l As Long
    If MyOpt = True Then
        lWinPos = HWND_TOPMOST
    Else
         lWinPos = HWND_NOTOPMOST
    End If
    ' Run the API SetWindowPos function
    If SetWindowPos(lHWnd, lWinPos, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE) Then
        ' If the function is greater than 0 (FALSE) then the operation was successful.  Return a True for to indicate such.
        SetWinPos = True' True or false can be returned from this function, but not absolutely necessary
    End If
End Function


' There's a checkbox on the form called "chkOnTop"
Private Sub chkOnTop_Click()
If chkOnTop.Value = True Then
    Call SetWinPos(Me.hWND, True)
Else
    Call SetWinPos(Me.hWND, False)
End If
End Sub



Note to Webmaster