Results 1 to 3 of 3

Thread: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?

  1. #1

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,711

    [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?

    Using two API calls we can obtain the UserForm window handle which is needed to pass as an argument to another API which changes the UserForms "Z Ordering" position of it.

    Add a UserForm to an Office application and a Command button to toggle the UserForm from Topmost to Not Topmost.

    Enjoy

    Code:
    Option Explicit
    'Written By RobDog888 - VB/Office Guru™
    'Add a Command Button so you can toggle the userform's topmost effect
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
                        ByVal lpClassName As String, _
                        ByVal lpWindowName As String) As Long
                        
    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 mlHwnd As Long
    
    Private Sub CommandButton1_Click()
        If CommandButton1.Caption = "Not Topmost" Then
            SetWindowPos mlHwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
            CommandButton1.Caption = "Topmost"
        Else
            SetWindowPos mlHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
            CommandButton1.Caption = "Not Topmost"
        End If
    End Sub
    
    Private Sub UserForm_Initialize()
        mlHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
        Do While mlHwnd = 0
            mlHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
            DoEvents
        Loop
        'Set topmost
        SetWindowPos mlHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
        CommandButton1.Caption = "Not Topmost"
    End Sub
    Last edited by RobDog888; Oct 17th, 2009 at 03:55 PM.
    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

  2. #2

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,711

    Re: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?

    If you make a small modification in the UserForm_Initialize event you can make your UserForm act like a normal Windows Form like it was independant from the main Excel window.

    Using the above code, add one more line of code as shown below...


    Code:
    Private Sub UserForm_Initialize()
        mlHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
        Do While mlHwnd = 0
            mlHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
            DoEvents
        Loop
        'Set topmost
        SetWindowPos mlHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
        CommandButton1.Caption = "Not Topmost"
        'Minimize Excel's main window if you want to present the UserForm as a standard Form not "associated" with Excel.
        Application.WindowState = xlMinimized
    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
    New Member
    Join Date
    Aug 2017
    Posts
    9

    Re: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?

    Hi RobDog888,

    awesome idea!!!
    I have a question because i want to do userform as TOP on everything, even if i am opening browser, it is possible ?

    Now when i am opening userform as top, net i am swithing on browser - the browser is higher than userform.

    Can you please help if you are here still or maybe a anyone else?
    Best,
    Jacek

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