Results 1 to 5 of 5

Thread: [RESOLVED] always on top

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    14

    Resolved [RESOLVED] always on top

    Hi,

    How can I for a form to always stay on top of the other windows ? I tried some code I found on the net such as this link from MSDN .

    But I always have the problem that VBA don't recognize the .hwnd properties of the form... Where is my problem ?

    Thanks!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: always on top

    Use the FindWindow API to get the UserForms handle.
    VB Code:
    1. Option Explicit
    2. 'Written By VB/Office Guru™
    3. Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
    4. ByVal lpWindowName As String) As Long
    5.  
    6. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
    7. ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    8.  
    9. Private Const HWND_TOPMOST = -1
    10. Private Const HWND_NOTOPMOST = -2
    11. Private Const SWP_NOMOVE = &H2
    12. Private Const SWP_NOSIZE = &H1
    13.  
    14. Private Sub UserForm_Initialize()
    15.     Dim lHwnd As Long
    16.     lHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
    17.     Do While lHwnd = 0
    18.         lHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
    19.         DoEvents
    20.     Loop
    21.     SetWindowPos lHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
    22. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    14

    Re: always on top

    I've changed "userform1" with the name (is it right ? Or I need the caption ?) of my form, but what is "ThunderDFrame" ?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: always on top

    "ThunderDFrame" is the class name of the userform. That doesnt change.

    UserForm1 is the forms Caption as described in my coment. Change that to whatever your forms caption says exactly.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    14

    Re: always on top

    Great!
    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