Keeping a Window OnTop
Ivan F Moala
Can Do
Visits here
Goto Guest book sign in page [Home]
Greatest place to be
Thanks for visiting my site lucky visitor:
This page was last updated on: April 25, 2006
Copyright © 2002. XcelFiles. All Rights Reserved Ivan F Moala
Tell a friend about this page



Keeping a Window on Top {always} is just a matter of setting the Windows position style with the appropriate API and Flags. Here is the code routine to Keep a Window on top.

APIS & Constants

'// Used to set the Windows Style
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


'// Positions
Private Const HWND_TOPMOST = -1

Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40

Usage:

SetWindowPos Windowhdl, _
   HWND_TOPMOST, _
   0, 0, 0, 0, _
   SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE

NB: Wndowhdl will need to be defined ie. you will need to get the handle of the Window that you will be working with. To do this there are various methods. Download the workbook to view one way of getting the Active Window handle.

What can I use this for:
Basically anytime you need to keep a Window eg another application Window, Userfrom etc and have this remain on Top even if it doesn't have the focus. Good example of this is to have a userform remain on Top. Also the NotePad utility.

Example for calculator applet
Here is an example of how to use this (see image below).
Have you ever needed to use the Windows Applet "Calculator" just do some quick calculations etc. Once you have got the result you then just copy it and paste it to your cell, BUT selecting the cell activates Excel and the calculator is no longer visible so to use it again you need to reactivate it. Using this code the calculator remains on TOP ALL the TIME.

Code notes:
Google
Search WWW Search My Site!

Download example WorkBook (Commented)