Library code snippets
Always on top
A frequent request is how to make a window float on top of every other window. This is actually quite simple using the code below
Simply call MakeTopMost to make the window appear on top, and MakeNormal to return it to its normal behaviour
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As
Long, ByVal hWndInsertAfter As Long, ByVal x As Long, y, 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 Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Public Sub MakeNormal(hwnd As Long)
SetWindowPos hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub
Public Sub MakeTopMost(hwnd As Long)
SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub
Related articles
Related discussion
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
-
Can you describe Above simple VB6 code?
by pramodmca09 (0 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
The above code plus the following makes a very useful combination of floating the ENTIRE MS ACCESS WINDOW rather than just one of the MDI forms.
Option Explicit
'API Declaration
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
sub RaiseAccess()
Dim hwnd As Long
'Find the MS ACCESS WINDOW
hwnd = FindWindow("OMain", "Microsoft Access")
If hwnd <> 0 Then MakeTopMost (hwnd)
'Then use MakeNormal(FindWindow("OMain","Microsoft Access") to bring it back down
end sub
I use a MDIform with some child forms. I wanted one of these child forms to stay on top of the others, but the code only works if the function is called from the MDIform, otherwise it just doesn't work. To solve the problem I had to set the MDIChild property of the form to false, but esthetically it didn't look good. If there is any way to get the code working with a child form it would fit perfectly to me.
I also want to be able to interact with my application while this application stays on top. Anyone know how to do that?
I.e. if I put this function into the form I want to stay on top of all forms. If I use this function inside another application, it will not allow me to manipulate the screens of that application.
Thanks
Example of call:
Call MakeTopMost(Me.hwnd)
yeah, but can you do it in c in linux?
' ---------------------------------------------------
' FORM frmMapSearch
' ---------------------------------------------------
Private Sub Form_Activate()
MakeTopMost Me.hwnd
End Sub
The result is that when the form is drawn screen the Sub MakeTopMost will be called and then it will act as a modal form
the path for win98 is c:\windows\sendto\
a path that should also work for windows2k is
c:\documents and settings\all users
After adding this code, the form hides behind all other windows, doesn't show in the taskbar and won't close. -_-
Simple to do, but not sure what it has to do with always on top.
Copy this code to a new form...
Option Explicit
Private Sub Form_Load()
MsgBox Command$
End
End Sub
Now just make your EXE and copy a shortcut to the "SendTo" directory. Under Win2K its C:\Documents and Settings\Suicidal\SendTo
Hope this helps.
Steve.
PS. Cheers for the code on always on top - has been useful.
How can I add Command to my application in "send to" menu for any file type. I explain my problem here.
If the user right clicks on the file, the "Send To" menu comes under windows. I want to add "Send to Dwijen's Application" text in that menu and then when the user clicks on that command from "send to" menu, the windows explorer should launch my program saying the filename which is sent to my application.
hi
I am facing problem with this code.
the form name is FrmTip which should always on top
The Form1 has tow textbox control
say text1 and text2 both have some tool tips
in the gotfocus of the text box event i calls the frmtip
by the following code
Private Sub Text1GotFocus()
FrmTip.Label1.Caption = Me.ActiveControl.ToolTipText
MakeTopMost FrmTip.hwnd
End Sub
Private Sub Text2GotFocus()
FrmTip.Label1.Caption = Me.ActiveControl.ToolTipText
MakeTopMost FrmTip.hwnd
End Sub
and also both the forms are Mdi Child forms
Please help me
if give frmtip.show command in the above event then the focus is in frmtip form only.
i want to use frmtip form as a tips form.
Please help as earyly as possible
My email id is S_Muhilan@hotmail.com
with regards
S.Muhilan
Mumbia-india.
Waitting for ur reply
I haven't try with mdi/mdi childs forms but if you want to get the handle of a form you can get it like this:
m_lHwnd = MyForm.hwnd
as the MyForm is your target form
Hope this will help
Cheers,
Hervé
And I have no answer to your question yet.
Cheers,
Hervé
Cheers,
Hervé
Here it is
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, y, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
-> declaration of the API function SetWindowPos taken from the "user32" library
Private Const HWND_TOPMOST = -1
-> constant to specify the window will be on top
Private Const HWND_NOTOPMOST = -2
-> constant to specify the window won't be on top
Private Const SWP_NOMOVE = &H2
-> constant to specify that the parameters x and y passed to the SetWindowPos function have to be ignored (keep the current x and y of the form)
Private Const SWP_NOSIZE = &H1
-> constant to specify that the parameters cx and cy passed to the SetWindowPos function have to be ignored (keep the current width and height of the form)
Private Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE
-> constant designed to pre-compute the OR logical operation of the SWP_NOMOVE and SWP_NOSIZE constants (applying this constant will apply the two flags in the same time)
Public Sub MakeTopMost(hwnd As Long)
-> declaration of the MakeTopMost sub with the input parameter hwnd (handle window)
SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
-> call of SetWindowPos
-> don't forget that the four 0 corresponding to the x, y, cx and cy parameters are ignored because of the TOPMOST_FLAGS
End Sub
It's the same for the other function, but the hWndInsertAfter which is set to HWND_NOTOPMOST
I hope this will help you
Cheers,
Hervé
basically you can't do it in asp as the code is executed on your web server and not on the client machine.
Anyway, if you want to get a modal popup window you can add this code to the body tag of your popup :
<body onblur="window.focus();">
Cheers,
Hervé
I have encounted a problem when I showing another form using "form1.show vbmodal".
form1 cannot be shown.
How to keep the main form always on top of other application? and the child forms can show on top the main form.
Thanks!
On Win 98 -> XP
1)ALT-TAB
2)Select any explorer app (or other, didn't bother to test)
---All you need to do is get to the quichlaunch bar!!
3)Start-bar pops up (with Quicklaunch attached)
4)Click Desktop short cut.
Hay Presto everything minimized including your ontop! app.
(Only tested on XP, but I assume it'll work on any desktop quichlaunch shortcut)
once i understood a bit of vb this was very easy to implement, i put the form on top state in the form load and it worked, i suppose you could set the form on top and normal state in to a check box
but could you help me to understand want does every line of this code means and why is it there?
thanks
btw i am a begginer
Guys, can you please tell me if there is any code with the same functionality that works in ASP (Active Server Pages).
You have to put
MakeTopMost (hwnd)
in your code, not just
MakeTopMost
James, EXCELLENT ARTICLE! Helped me a lot!
Dude thats the point of this, that the window stays on top of other apps..
If you want the window to just stay on top of windows in your app, use
show formName,1
to show it as a modal form
All this is very nice, but it comes with a very annoying side effect. When the user
Alt-tab's to another application, the "always-on-top" window stays annoyingly on
top of the other application.
VB doesn't even give you an event to tell you that you app is no longer on top.
Got any ideas?
Call MakeTopMost(hwnd)
Im not sure if the other function will work the same way, but this one works!
Basically you hwnd is a Handle (Pointer) to a window object, It being a long you must first create the pointer then send it.
Good Luck!
i get argument not optional when i call this "Call MakeTopMost"
i have the other code as per the example, but still makes no difference, is this because i am using an mdi form as well as this form, the form is not an mdi child tho!
any ideas??
This thread is for discussions of Always on top.