Results 1 to 6 of 6

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

Threaded View

  1. #1

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

    [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

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