Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 71,772 times

Related Categories

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Added suggestion

    Posted by ideo on 08 Apr 2005

    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...

  • just one problem

    Posted by ladoleste on 16 Mar 2005

    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 wor...

  • Works but

    Posted by Cici on 10 Dec 2004

    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...

  • The call

    Posted by Arebrok on 18 Aug 2004

    Example of call:

    Call MakeTopMost(Me.hwnd)

  • okay, how about this?

    Posted by jebrew on 25 Mar 2004

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