Skip to content

VB 6 code to keep your program always on top of other programs

  • This topic has 0 replies, 1 voice, and was last updated 1 year ago by Admin.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #171164
    Admin
    Administrator

    VB 6 code to keep your program always on top of other programs I tried this on Windows 11 and it works.
    So first we add this to a module of the program:

    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

    Now to call it either via a command or formload add this:

    SetTopMostWindow Form1.hwnd, True 

    Now to stop your program from being always on top use this:

    SetTopMostWindow Form1.hwnd, False

    I’ve attached the example.

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.