I'd like for my form to stay on top of all other windows - so if someone wants to run another program, my program will still be visible. how can i do this?
Printable View
I'd like for my form to stay on top of all other windows - so if someone wants to run another program, my program will still be visible. how can i do this?
This question has been asked many times. Do a search and I'm sure you'll find your answer.
MSDN help
This example forces a form to always remain on top. To try this example, create a form (not an MDI child form), and set its Caption
property to Always On Top. Create a menu for the form that contains the command Topmost (mnuTopmost). Create a new module using the Module command on the Insert menu. Paste the Declare statement into the Declarations section of the new module, being sure that the statement is on one line with no break or wordwrap. Then paste the Sub procedure into the Declarations section of the form, and press F5. (This example requires Microsoft Windows version 3.1.)
' Declaration of a Windows routine. This statement is for the module.
Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)
' Set some constant values (from WINAPI.TXT).
Const conHwndTopmost = -1
Const conHwndNoTopmost = -2
Const conSwpNoActivate = &H10
Const conSwpShowWindow = &H40
Private Sub mnuTopmost_Click ()
' Add or remove the check mark from the menu.
mnuTopmost.Checked = Not mnuTopmost.Checked
If mnuTopmost.Checked Then
' Turn on the TopMost attribute.
SetWindowPos hWnd, conHwndTopmost, 0, 0, 0, 0, conSwpNoActivate Or conSwpShowWindow
Else
' Turn off the TopMost attribute.
SetWindowPos hWnd, conHwndNoTopmost, 0, 0, 0, 0, conSwpNoActivate Or conSwpShowWindow
End If
End Sub
abilo: i keep getting an overflow error? any ideas as to why?
Private Declare Function SetWindowPos Lib "user32.dll" (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
Replace API declaration with this one and you will be there. Sorry about this error !
Or try this one.
Place this code on de form load event of form1
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Private Declare Sub 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)
Private Sub Form_Activate()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: [email protected]
'Set the window position to topmost
SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub
oh it's beautiful! thanks a lot!