Results 1 to 8 of 8

Thread: I'm on top

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    9
    How can a form be kept on top regaurdless of what program or form is in focus?

    Thanks,
    JohnMB

  2. #2
    Guest
    Code:
    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 Const HWND_TOPMOST = -1
    Private Const HWND_NOTOPMOST = -2
    Private Const SWP_NOMOVE = &H2
    Private Const SWP_NOSIZE = &H1
    Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
    Private Sub Form_Load()
    
      OnTop Me, True
    
    End Sub
    Private Sub OnTop(frm As Form, TopMost As Boolean)
      
      Select Case TopMost
        Case True
          Call SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
        Case False
          Call SetWindowPos(Me.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)
      End Select
    
    End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    9

    Thumbs up

    Thanks, just what I was looking for.

  4. #4
    Guest
    Since the constants are short and readable, you can omit them, causing your code to reduce slightly.
    Code:
    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()
        SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, 3
    End Sub

  5. #5
    Guest
    thats good,
    but if you forgot to add comments, a few months later when you look at your code, you may not know what you were trying to do,
    thats why I always like to use constants.

  6. #6
    Guest
    I've remembered the code for this (and others) for ages and never forgot them. Using contants for values like -1, 3 etc. just doesn't suit me, however, when dealing with numbers like &H1FF01, it's a good idea to use them.

  7. #7
    Guest
    I remember the code for this(and also to drag a form without a title bar, and many other things)

    but some people may not.

    and I always like to add the constants, because then people will actually know which part of the code does what...

  8. #8
    Guest
    I usually use comments for that:
    Code:
    'Bring window to top
    SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, 3
    Again, this is just in my opinion.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width