|
-
Sep 5th, 2010, 05:24 AM
#1
Thread Starter
New Member
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!
-
Sep 5th, 2010, 06:12 AM
#2
Re: UserForm Always on Top
 Originally Posted by mikepardon
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.
-
Sep 5th, 2010, 06:19 AM
#3
Thread Starter
New Member
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.
-
Sep 6th, 2010, 05:31 PM
#4
Thread Starter
New Member
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!
-
Sep 7th, 2010, 10:00 AM
#5
Addicted Member
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
-
Sep 11th, 2010, 12:01 PM
#6
Thread Starter
New Member
Re: UserForm Always on Top
 Originally Posted by His Nibbs
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|