dcsimg
Results 1 to 6 of 6

Thread: UserForm Always on Top

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    UserForm Always on Top

    I was reading the topic
    http://www.vbforums.com/showthread.php?t=588169
    by RobDogg888 but I'm unable to get this code to work with my applications.

    I am often confused as to where to place this code. I placed:
    Code:
    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
    In my Module file and place the rest of the code in the actual Userform, is this correct or incorrect?

    I'm just trying to make a very basic program to help me at work but need it to be able to toggle always on top.

    Any help would be great!

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: UserForm Always on Top

    Quote Originally Posted by mikepardon View Post
    In my Module file and place the rest of the code in the actual Userform, is this correct or incorrect?
    Correct, however, the Private should be changed to Public when put in a module.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: UserForm Always on Top

    Thanks for the quick reply Hack, I now no longer get the cannot find function message which that has fixed. Issue I now have is that when I run the application the form doesn't actually load though I get no errors

    Code:
        Call FormatUserForm(Me.Caption)
        'CommandButton1.Visible = False
        mlHwnd = FindWindow("Counter", "frmCounter") 'Change to match your userforms caption
        Do While mlHwnd = 0
            mlHwnd = FindWindow("Counter", "frmCounter") '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
    This is what I have in my UserForm_Initialize() which I'm sure is wrong! lol.

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: UserForm Always on Top

    Sorry for the double post, but can anyone help me with this?
    Need to get this thing working but I'm honestly baffled!

  5. #5
    Addicted Member
    Join Date
    Jul 2009
    Posts
    208

    Re: UserForm Always on Top

    What is "Counter" and why the loop? Try this instead:
    Code:
    Private Sub UserForm_Initialize()
        AlwaysOnTop (Me.caption)
    End Sub
    
    Private Sub AlwaysOnTop(caption As String)
        Dim ret As Long
        Dim hWnd As Long 
        hWnd = FindWindow(vbNullString, caption)
        ret = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)    
    End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: UserForm Always on Top

    Quote Originally Posted by His Nibbs View Post
    What is "Counter" and why the loop? Try this instead:
    Code:
    Private Sub UserForm_Initialize()
        AlwaysOnTop (Me.caption)
    End Sub
    
    Private Sub AlwaysOnTop(caption As String)
        Dim ret As Long
        Dim hWnd As Long 
        hWnd = FindWindow(vbNullString, caption)
        ret = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)    
    End Sub
    Nibbs, thank you! You're a God :P.
    Been trying to get this working for ages and it now works. Thanks mate :P.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width