Results 1 to 22 of 22

Thread: [resoloved]Always on top?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Resolved [resoloved]Always on top?

    How to make a program always on top with a click on a command button?
    Last edited by n00b scripter; Jun 22nd, 2005 at 09:29 AM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Always on top?

    Here is a quick sample:
    VB Code:
    1. Option Explicit
    2.  
    3. Const HWND_TOPMOST = -1
    4. Const HWND_NOTOPMOST = -2
    5. Const SWP_NOSIZE = &H1
    6. Const SWP_NOMOVE = &H2
    7. Const SWP_NOACTIVATE = &H10
    8. Const SWP_SHOWWINDOW = &H40
    9.  
    10. Private Declare Sub SetWindowPos Lib "User32" _
    11.     (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, _
    12.     ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    13.  
    14. Private Sub Command1_Click()
    15.     SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW _
    16.         Or SWP_NOMOVE Or SWP_NOSIZE
    17. End Sub

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Always on top?

    VB Code:
    1. 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
    2.  
    3. Private Const HWND_TOPMOST = -1
    4. Private Const HWND_NOTOPMOST = -2
    5. Private Const SWP_NOMOVE = 2
    6. Private Const SWP_NOSIZE = 1
    7. Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
    8.  
    9. Private Sub Form_Load()
    10.     SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS
    11.     'To "turn off" always on top use
    12.     'SetWindowPos Me.Hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS
    13. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Always on top?

    ok i try

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Always on top?

    well how to make it not on top? i want to turn it on and off

  6. #6
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Always on top?

    use the constants provided:

    Private Const HWND_TOPMOST = -1
    Private Const HWND_NOTOPMOST = -2
    Private Const SWP_NOMOVE = 2
    Private Const SWP_NOSIZE = 1
    Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Always on top?

    I dont aunderstand how to bind that to a command button?

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Always on top?

    The same way the other const was used but don't forget to declare it first:
    VB Code:
    1. Private Sub Command2_Click()
    2.     SetWindowPos Me.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW _
    3.         Or SWP_NOMOVE Or SWP_NOSIZE
    4. End Sub

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Always on top?

    whit this script my whole application disappear

  10. #10
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Always on top?

    Quote Originally Posted by [LGS]Static
    VB Code:
    1. 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
    2.  
    3. Private Const HWND_TOPMOST = -1
    4. Private Const HWND_NOTOPMOST = -2
    5. Private Const SWP_NOMOVE = 2
    6. Private Const SWP_NOSIZE = 1
    7. Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
    8.  
    9. Private Sub Form_Load()
    10.     SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS
    11.     [B]'To "turn off" always on top use
    12.     'SetWindowPos Me.Hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS[/B]
    13. End Sub

    did you read this post?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Always on top?

    Yeah i did and now the turn on thing works but now it dissapears when i try the switch of button i have made it

    VB Code:
    1. Private Sub Command2_Click()
    2.     SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS
    3.  
    4. End Sub

  12. #12
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    Re: Always on top?

    Because of this right here:
    Quote Originally Posted by n00b scripter

    VB Code:
    1. Private Sub Command2_Click()
    2.     SetWindowPos Me.hwnd, [B]HWND_TOPMOST,[/B] 0, 0, 0, 0, FLAGS
    3.  
    4. End Sub
    Don't use HWND_TOPMOST again. That's a NoNo

    Base 2
    Fcnncu"Nqxgu"Lguug##

  13. #13
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Always on top?

    Heck knows what you're doing, though ...
    The following sample simply toggles between "regular" and "topmost" modes - all you need is ONE command button so just copy and paste this code:
    VB Code:
    1. Option Explicit
    2.  
    3. Const HWND_TOPMOST = -1
    4. Const HWND_NOTOPMOST = -2
    5. Const SWP_NOSIZE = &H1
    6. Const SWP_NOMOVE = &H2
    7. Const SWP_NOACTIVATE = &H10
    8. Const SWP_SHOWWINDOW = &H40
    9.  
    10. Private Declare Sub SetWindowPos Lib "User32" _
    11.     (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, _
    12.     ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    13.  
    14. Private Sub Command1_Click()
    15. '============================
    16. Static blnTopMost As Boolean
    17. Dim lngPos As Long
    18.  
    19.     blnTopMost = Not blnTopMost
    20.    
    21.     If blnTopMost Then
    22.         lngPos = HWND_TOPMOST
    23.         Command1.Caption = "Make Regular Window"
    24.     Else
    25.         lngPos = HWND_NOTOPMOST
    26.         Command1.Caption = "Make Topmost Window"
    27.     End If
    28.    
    29.     SetWindowPos Me.hWnd, lngPos, 0, 0, 0, 0, _
    30.                  SWP_NOACTIVATE Or SWP_SHOWWINDOW Or _
    31.                  SWP_NOMOVE Or SWP_NOSIZE
    32.  
    33. End Sub
    34.  
    35. Private Sub Form_Load()
    36.     Command1.Caption = "Make Topmost Window"
    37. End Sub

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Always on top?

    Whata... that script made the application fill out the screen!

  15. #15
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    Re: Always on top?

    Quote Originally Posted by n00b scripter
    Whata... that script made the application fill out the screen!
    If you're talking about RhinoBull's script, it worked just fine for me ?????

    Base 2
    Fcnncu"Nqxgu"Lguug##

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Always on top?

    hmm weird

  17. #17
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    Re: Always on top?

    Post your project maybe someone can see if there is something besides your code

    Base 2
    Fcnncu"Nqxgu"Lguug##

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Always on top?

    Ok here it is. I dont think you will understand whats it is for
    Attached Files Attached Files
    Last edited by n00b scripter; Jun 21st, 2005 at 09:17 PM.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Always on top?

    Can any one help me?

  20. #20
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Always on top?

    Your "problem" noob is setting form's windowstate to maximized, so simply comment it (as shown below):
    VB Code:
    1. Private Sub Command1_Click()
    2. 'InitializeRes
    3. '''[b]Me.WindowState = 2[/b]
    4.  
    5. ...

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Always on top?

    Ty so much for helping me and excuse me for my noobness

  22. #22
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Always on top?

    Not at all, are you kidding ... Enjoy it.

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