Visual Basic FAQ

SetWindowPos: Create a Topmost Window
     
Posted:   Thursday December 26, 1996
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations
     
Related:   SetWindowPos: Create a Topmost Window
SetWindowLong: Create a Floating Window
Pure VB: Simulating Owned Windows for MDI Children
 Prerequisites
None.

Here's another popular request ... a quick API call that will a VB form appear 'topmost' over all other windows, even when another application gains focus.

Note: While in the VB design environment, a window set to topmost may, depending on the project form or other application activated, appear to loose its topmost status. This is by design in Win32, and the application should function as anticipated once the application is compiled into an exe. Once the application has been compiled it will perform as desired. 16-bit developers do not have this concern.

 BAS Module Code
None.

 Form Code
Add the following to any form to make topmost:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Constants for topmost.
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE

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


Private Sub Form_Load()
   
  'typically called in the form load
   Call SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)

End Sub
 Comments
A topmost window is one which is always overtop all other windows, including those windows in other applications. An example might be an application-launching toolbar which you want to always be 'on top' of any activated window in any application.

Because topmost windows act independently of any parent window which created them (using the toolbar example), a topmost window will not become minimized if the parent window is minimized. If your intention is to design a window that is only topmost in relation to your parent application, and not the entire screen, as the above code would set, see the FAQ Code for SetWindowLong: Create a Floating Window. To remove the topmost attribute from a form, call SetWindowPos again, passing HWND_NOTOPMOST as the second parameter of the API.

Note: While in the VB design environment, a window set to topmost may, depending on the project form or other application activated, appear to loose its topmost status. This is by design in Win32, and the application should function as anticipated once the application is compiled into an exe.


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter