Make A Form Stay On Top (16 bit and 32 bit)

Pass it the handle of the window you want to make topmost (or for which you wish to end that condition) and a true/ false flag to indicate whether it should be topmost.

'Declares for FormStayOnTop
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)
Sub FormStayOnTop (varForm as Form, OnTop%)
   Handle% = varForm.hwnd
   Const Swp_Nosize = &H1
   Const SWP_Nomove = &H2
   Const Swp_NoActivate = &H10
   Const Swp_ShowWindow = &H40
   Const Hwnd_TopMost = -1
   Const Hwnd_NoTopMost = -2
   wFlags = SWP_Nomove Or Swp_Nosize Or _
              Swp_ShowWindow Or Swp_NoActivate
   Select Case OnTop%
      Case True
         PosFlag = Hwnd_TopMost
      Case False
         PosFlag = Hwnd_NoTopMost
   End Select
   SetWindowPos Handle%, PosFlag, 0, 0, 0, 0, wFlags
End Sub
'Declares for FormStayOnTop 32 bit version
Declare Sub SetWindowPos Lib "User32" (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)
Sub FormStayOnTop (varForm as Form, OnTop%)
   Handle% = varForm.hwnd
   Const Swp_Nosize = &H1
   Const SWP_Nomove = &H2
   Const Swp_NoActivate = &H10
   Const Swp_ShowWindow = &H40
   Const Hwnd_TopMost = -1
   Const Hwnd_NoTopMost = -2
   wFlags = SWP_Nomove Or Swp_Nosize Or _
               Swp_ShowWindow Or Swp_NoActivate
   Select Case OnTop%
      Case True
         PosFlag = Hwnd_TopMost
      Case False
         PosFlag = Hwnd_NoTopMost
   End Select
   SetWindowPos Handle%, PosFlag, 0, 0, 0, 0, wFlags
End Sub

 

Tip Submitted By: Jeff Williams


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.