Results 1 to 7 of 7

Thread: Force window to stay on top of all others [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    525

    Question Force window to stay on top of all others [RESOLVED]

    I'd like for my form to stay on top of all other windows - so if someone wants to run another program, my program will still be visible. how can i do this?
    Last edited by jsun9; Oct 23rd, 2003 at 09:08 AM.

  2. #2

  3. #3
    Addicted Member Abilio's Avatar
    Join Date
    May 2003
    Location
    Aveiro - Portugal
    Posts
    222
    MSDN help
    This example forces a form to always remain on top. To try this example, create a form (not an MDI child form), and set its Caption
    property to Always On Top. Create a menu for the form that contains the command Topmost (mnuTopmost). Create a new module using the Module command on the Insert menu. Paste the Declare statement into the Declarations section of the new module, being sure that the statement is on one line with no break or wordwrap. Then paste the Sub procedure into the Declarations section of the form, and press F5. (This example requires Microsoft Windows version 3.1.)

    ' Declaration of a Windows routine. This statement is for the module.
    Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)

    ' Set some constant values (from WINAPI.TXT).
    Const conHwndTopmost = -1
    Const conHwndNoTopmost = -2
    Const conSwpNoActivate = &H10
    Const conSwpShowWindow = &H40

    Private Sub mnuTopmost_Click ()
    ' Add or remove the check mark from the menu.
    mnuTopmost.Checked = Not mnuTopmost.Checked
    If mnuTopmost.Checked Then

    ' Turn on the TopMost attribute.
    SetWindowPos hWnd, conHwndTopmost, 0, 0, 0, 0, conSwpNoActivate Or conSwpShowWindow
    Else
    ' Turn off the TopMost attribute.
    SetWindowPos hWnd, conHwndNoTopmost, 0, 0, 0, 0, conSwpNoActivate Or conSwpShowWindow
    End If
    End Sub

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    525
    abilo: i keep getting an overflow error? any ideas as to why?

  5. #5
    Addicted Member Abilio's Avatar
    Join Date
    May 2003
    Location
    Aveiro - Portugal
    Posts
    222
    Private Declare Function SetWindowPos Lib "user32.dll" (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

    Replace API declaration with this one and you will be there. Sorry about this error !

  6. #6
    Addicted Member Abilio's Avatar
    Join Date
    May 2003
    Location
    Aveiro - Portugal
    Posts
    222
    Or try this one.


    Place this code on de form load event of form1


    Const HWND_TOPMOST = -1
    Const HWND_NOTOPMOST = -2
    Const SWP_NOSIZE = &H1
    Const SWP_NOMOVE = &H2
    Const SWP_NOACTIVATE = &H10
    Const SWP_SHOWWINDOW = &H40
    Private Declare Sub 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)

    Private Sub Form_Activate()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    'Set the window position to topmost
    SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    End Sub

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    525
    oh it's beautiful! thanks a lot!

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