Library code snippets

Always on top

A frequent request is how to make a window float on top of every other window. This is actually quite simple using the code below

Simply call MakeTopMost to make the window appear on top, and MakeNormal to return it to its normal behaviour

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, y, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE

Public Sub MakeNormal(hwnd As Long)
    SetWindowPos hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub
Public Sub MakeTopMost(hwnd As Long)
    SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub

Comments

  1. 08 Apr 2005 at 06:06

    The above code plus the following makes a very useful combination of floating the ENTIRE MS ACCESS WINDOW rather than just one of the MDI forms.


    Code:

    Option Explicit


    'API Declaration
    Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long



    sub RaiseAccess()


    Dim hwnd As Long


    'Find the MS ACCESS WINDOW
    hwnd = FindWindow("OMain", "Microsoft Access")
    If hwnd <> 0 Then MakeTopMost (hwnd)
    'Then use MakeNormal(FindWindow("OMain","Microsoft Access") to bring it back down


    end sub



  2. 16 Mar 2005 at 16:51

    I use a MDIform with some child forms. I wanted one of these child forms to stay on top of the others, but the code only works if the function is called from the MDIform, otherwise it just doesn't work. To solve the problem I had to set the MDIChild property of the form to false, but esthetically it didn't look good. If there is any way to get the code working with a child form it would fit perfectly to me.

  3. 10 Dec 2004 at 12:34

    I also want to be able to interact with my application while this application stays on top.  Anyone know how to do that?


    I.e. if I put this function into the form I want to stay on top of all forms.  If I use this function inside another application, it will not allow me to manipulate the screens of that application.


    Thanks

  4. 18 Aug 2004 at 01:31

    Example of call:


    Call MakeTopMost(Me.hwnd)


  5. 25 Mar 2004 at 17:52

    yeah, but can you do it in c in linux?

  6. 05 Nov 2003 at 04:20
    dropped this code into my form, cut and paste, and worked perfectly ... form stays on top of all other windows now ... even other forms in same app.
  7. 11 Aug 2003 at 03:52
    Here is an exemple of use :

    ' ---------------------------------------------------
    ' FORM frmMapSearch
    ' ---------------------------------------------------
    Private Sub Form_Activate()
     MakeTopMost Me.hwnd
    End Sub

    The result is that when the form is drawn screen the Sub MakeTopMost will be called and then it will act as a modal form
  8. 11 Aug 2003 at 03:49
    lol thanks that worked perfect
  9. 11 Aug 2003 at 03:47
    lol
  10. 11 Aug 2003 at 03:46
    where do i put that bit?
  11. 11 Aug 2003 at 03:41
    didnt make any difference
  12. 10 Aug 2003 at 21:31

    the path for win98 is c:\windows\sendto\
    a path that should also work for windows2k is
    c:\documents and settings\all users


  13. 20 Jul 2003 at 18:54
  14. 10 May 2003 at 02:23

    After adding this code, the form hides behind all other windows, doesn't show in the taskbar and won't close. -_-

  15. 31 Mar 2003 at 08:29

    Simple to do, but not sure what it has to do with always on top.


    Copy this code to a new form...


    Option Explicit


    Private Sub Form_Load()
       MsgBox Command$
       End
    End Sub


    Now just make your EXE and copy a shortcut to the "SendTo" directory.  Under Win2K its C:\Documents and Settings\Suicidal\SendTo


    Hope this helps.
    Steve.


    PS.  Cheers for the code on always on top - has been useful.

  16. 02 Dec 2002 at 01:27

    How can I add Command to my application in "send to" menu for any file type. I explain my problem here.


    If the user right clicks on the file, the "Send To" menu comes under windows. I want to add "Send to Dwijen's Application" text in that menu and then when the user clicks on that command from "send to" menu, the windows explorer should launch my program saying the filename which is sent to my application.

  17. 02 Dec 2002 at 00:40

    hi
    I am facing problem with this code.
    the form name is FrmTip which should always on top
    The Form1 has tow textbox control
    say text1 and text2 both have some tool tips
    in the gotfocus of the text box event i  calls the frmtip
    by the following code


    Private Sub Text1GotFocus()
       FrmTip.Label1.Caption = Me.ActiveControl.ToolTipText
       MakeTopMost FrmTip.hwnd
    End Sub
    Private Sub Text2
    GotFocus()
       FrmTip.Label1.Caption = Me.ActiveControl.ToolTipText
       MakeTopMost FrmTip.hwnd
    End Sub


    and also both the forms are Mdi Child forms
    Please help me
    if give frmtip.show command in the above event then the focus is in frmtip form only.
    i want to use frmtip form as a tips form.
    Please help as earyly as possible
    My email id is S_Muhilan@hotmail.com
    with regards
    S.Muhilan
    Mumbia-india.
    Waitting for ur reply


  18. 25 Nov 2002 at 13:33
    Hi

    I haven't try with mdi/mdi childs forms but if you want to get the handle of a form you can get it like this:

    m_lHwnd = MyForm.hwnd


    as the MyForm is your target form

    Hope this will help

    Cheers,

    Hervé
  19. 25 Nov 2002 at 13:28
    Sorry I missed ... I wanted to reply to the mssage posted by zoglchaim ...
    And I have no answer to your question yet.

    Cheers,

    Hervé
  20. 25 Nov 2002 at 13:26
    Sorry i missed and i reply your question to the message post by leokth ... check there

    Cheers,

    Hervé
  21. 25 Nov 2002 at 13:22
    Hi
    Here it is

    Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, y, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

    -> declaration of the API function SetWindowPos taken from the "user32" library

    Private Const HWND_TOPMOST = -1
    -> constant to specify the window will be on top

    Private Const HWND_NOTOPMOST = -2
    -> constant to specify the window won't be on top

    Private Const SWP_NOMOVE = &H2
    -> constant to specify that the parameters x and y passed to the SetWindowPos function have to be ignored (keep the current x and y of the form)

    Private Const SWP_NOSIZE = &H1
    -> constant to specify that the parameters cx and cy passed to the SetWindowPos function have to be ignored (keep the current width and height of the form)

    Private Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE
    -> constant designed to pre-compute the OR logical operation of the SWP_NOMOVE  and SWP_NOSIZE  constants (applying this constant will apply the two flags in the same time)


    Public Sub MakeTopMost(hwnd As Long)
    -> declaration of the MakeTopMost sub with the input parameter hwnd (handle window)

       SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
       -> call of SetWindowPos
       -> don't forget that the four 0 corresponding to the x, y, cx and cy parameters are ignored because of the TOPMOST_FLAGS

    End Sub

    It's the same for the other function, but the hWndInsertAfter which is set to HWND_NOTOPMOST

    I hope this will help you

    Cheers,

    Hervé
  22. 25 Nov 2002 at 13:04
    hi!
    basically you can't do it in asp as the code is executed on your web server and not on the client machine.
    Anyway, if you want to get a modal popup window you can add this code to the body tag of your popup :

    <body onblur="window.focus();">


    Cheers,

    Hervé
  23. 14 Nov 2002 at 00:59

    I have encounted a problem when I showing another form using "form1.show vbmodal".
    form1 cannot be shown.


    How to keep the main form always on top of other application? and the child forms can show on top the main form.
    Thanks!

  24. 12 Oct 2002 at 06:37
    Just a hint to people who use this...
    On Win 98 -> XP

    1)ALT-TAB
    2)Select any explorer app (or other, didn't bother to test)
    ---All you need to do is get to the quichlaunch bar!!
    3)Start-bar pops up (with Quicklaunch attached)
    4)Click Desktop short cut.

    Hay Presto everything minimized including your ontop! app.

    (Only tested on XP, but I assume it'll work on any desktop quichlaunch shortcut)


  25. 04 Sep 2002 at 11:35

    once i understood a bit of vb this was very easy to implement, i put the form on top state in the  form load and it worked, i suppose you could set the form on top and normal state in to a check box

  26. 26 Aug 2002 at 23:48
    works very nice
    but could you help me to understand want does every line of this code means and why is it there?
    thanks
    btw i am a begginer
  27. 04 Aug 2002 at 21:59

    Guys, can you please tell me if there is any code with the same functionality that works in ASP (Active Server Pages).

  28. 28 Jul 2002 at 09:40

    You have to put

    Code:

    MakeTopMost (hwnd)

    in your code, not just
    Code:

    MakeTopMost

    James, EXCELLENT ARTICLE! Helped me a lot!

  29. 01 Jul 2002 at 14:46

    Dude thats the point of this, that the window stays on top of other apps..


    If you want the window to just stay on top of windows in your app, use


    show formName,1


    to show it as a modal form

  30. 20 Jun 2002 at 21:41

    All this is very nice, but it comes with a very annoying side effect.  When the user
    Alt-tab's to another application, the "always-on-top" window stays annoyingly on
    top of the other application.


    VB doesn't even give you an event to tell you that you app is no longer on top.


    Got any ideas?

  31. 10 Jun 2002 at 09:13
    I agree with Rollershade, but this works fine, try it!:

    Call MakeTopMost(hwnd)

    Im not sure if the other function will work the same way, but this one works!
  32. 21 May 2002 at 16:22

    Basically you hwnd is a Handle (Pointer) to a window object, It being a long you must first create the pointer then send it.


    Good Luck!

  33. 25 Mar 2002 at 16:58

    i get argument not optional when i call this "Call MakeTopMost"


    i have the other code as per the example, but still makes no difference, is this because i am using an mdi form as well as this form, the form is not an mdi child tho!


    any ideas??

  34. 01 Jan 1999 at 00:00

    This thread is for discussions of Always on top.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback