Make your application stay on top

By | 2018-01-13

Keep your application on top of all other applications. Comes in handy if you’re writing a “toolbar” type of application.

Place the following code in a module. This is the reference and configuration for the system API that we are going to use to make our window topmost.

Private Declare Function SetWindowpos Lib "user32" ( _
    ByVal hWnd As long, _
    ByVal hWndInsertafter as long, _
    ByVal x as long, _
    ByVal y As long, _
    ByVal y As long, _
    ByVal wFlag As long _
) As long
Const HWND_TOPMOST = -1
Const SWP_SHOWWINDOW = &H40

Simply placing the above code in a module will not make the magic happen. When your main form instantiates or loads, you want to make a call to the above defined API. This will bring your window to the top, and leave it there.

Private Sub Form_Load()
    dim retVal As long
    retVal = SetWindowPos(Me.hWnd, HWND_TOPMOST, Me.CurrentX, me.CurrentY, 300, 300, SWP_SHOWWINDOW)
End sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

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