Results 1 to 12 of 12

Thread: Always On Top?

  1. #1
    NOMADMAN
    Guest

    Question Always On Top?

    How do I make a form stay on top (visible) on the screen while another window has the focus (the other windows bar is colored and my form is grayed out)...

    Also how would I get the windows id (hwnd, or something of that nature) from my form.

    Third and finally, how do I send text (or keypreeses if you can't do text) to the window with focus?

    ThankS!

    NOMAD

  2. #2
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Catalonia
    Posts
    397
    1-

    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)

    VB Code:
    1. 'this code makes the window stay on top
    2. rtn = SetWindowPos(OnTop.hwnd, -1, 0, 0, 0, 0, 3)
    3. 'window will not stay on top with this code
    4. rtn = SetWindowPos(OnTop.hwnd, -2, 0, 0, 0, 0, 3)
    Josep Mª

  3. #3
    Addicted Member hamins's Avatar
    Join Date
    Sep 2001
    Posts
    192
    To have the form appear on top always you'll have to use the "SetWindowPos" API. Here's the code : -



    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()

    SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE

    End Sub
    Knowledge is static .... understanding is Dynamic

  4. #4
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    I built that into a DLL so you can just use it without having to code anything. Just pass in the form name.
    ~Peter


  5. #5
    DaoK
    Guest
    You do no really need a DLL for 10 lines of code...

  6. #6
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Wink

    It's just a suggestion, and maybe he can find something else in the DLL useful.
    ~Peter


  7. #7
    Addicted Member
    Join Date
    Aug 2001
    Location
    UK
    Posts
    204
    How can i de-activate the always on top thing from hamins code?

    Sorry im still a newbie

  8. #8
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Catalonia
    Posts
    397
    Look at my post above.

    There is the code for cancelling always on top
    Josep Mª

  9. #9
    Addicted Member
    Join Date
    Aug 2001
    Location
    UK
    Posts
    204

    Cool

    O yeh thanks joijo

  10. #10
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228

    Talking Module

    Just plop this into a module! :-D
    VB Code:
    1. '====================================================
    2. '====This code was written by joijo and macai=======
    3. '====Use this code freely, but please leave the=======
    4. '====comments. And credit would be nice, too. *wink*====
    5. '====================================================
    6.  
    7. 'Declarations
    8. Declare Sub SetWindowPos Lib "user32" (ByVal hwnd As Long, _
    9. ByVal hWndInsertAfter As Long, _
    10. ByVal x As Long, ByVal y As Long, _
    11. ByVal cx As Long, _
    12. ByVal cy As Long, _
    13. ByVal wFlags As Long)
    14.  
    15. 'Actual Code
    16. Public Sub AlwaysOnTop(F As Form, Enabled As Boolean)
    17. If Enabled Then 'If the programmer wants the form on top.
    18.  SetWindowPos F.hwnd, -2, 0, 0, 0, 0, 3
    19. Else 'If the programmer does want the form on top.
    20.  SetWindowPos F.hwnd, -1, 0, 0, 0, 0, 3
    21. End If
    22. End Sub
    Me and jojo put that together a while back.
    Luke

  11. #11
    DaoK
    Guest
    '====================================================
    '====This code was written by joijo and macai=======
    '====Use this code freely, but please leave the=======
    '====comments. And credit would be nice, too. *wink*====
    '====================================================
    About vbworld.com example look like that... and how can you have credit on a Sub that you just do copy and paste

  12. #12
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228

    Wink DaoK

    DaoK, that was from an old thread. I just copied and pasted the
    module i have..
    Luke

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