Solved

make a form stay on top

Posted on 2000-02-29
7
395 Views
Last Modified: 2010-05-02
how do i set a form so it is always on top when i switch to a different application?
0
Comment
Question by:berator
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Accepted Solution

by:
stefanx earned 25 total points
ID: 2568933
Just do a search in this category for "Always on Top". Itmust have been asked about 100 times before ;)
0
 
LVL 3

Expert Comment

by:Michelt
ID: 2568961
I don't remember where I got this, But I know it works. At some point in time I neded to do the same, and this is How I got it to work !

Place in MODULE

Global Const SWP_NOMOVE = 2Global Const SWP_NOSIZE = 1
Global Const HWND_TOPMOST = -1Global Const HWND_NOTOPMOST = -2
Global Const FLOAT = 1, SINK = 0
Public 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)

This is a sub within the module

Sub FloatWindow(X As Integer, action As Integer)

Dim wFlags As Integer, result As Integer
wFlags = SWP_NOMOVE Or SWP_NOSIZE

If action <> 0 Then    
    ' Float
    Call SetWindowPos(X, HWND_TOPMOST, 0, 0, 0, 0, wFlags)
Else                    
    ' Sink
    Call SetWindowPos(X, HWND_NOTOPMOST, 0, 0, 0, 0, wFlags)
End If

End Sub

Code to use -->

'Float code

Dim f as integer
f = Screen.ActiveForm.hWndCall
FloatWindow(f, FLOAT)

'sink code

Dim f as integer
f = Screen.ActiveForm.hWndCall FloatWindow(f, SINK)to sink.
0
 
LVL 22

Expert Comment

by:ture
ID: 2569306
berator,

Have you tried this?

STAY !!!

(It works with my dog, sometimes. Haven't tried it with forms.)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:vbWayne
ID: 2569372
'to make form stay topmost

'put this in a bas module
'
Public 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

Global Const SWP_NOMOVE = 2
Global Const SWP_NOSIZE = 1
Global Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Global Const HWND_TOPMOST = -1
Global Const HWND_NOTOPMOST = -2

'=============================================
'
'put this in events click or load or whatever
'or hardcopy
'To set Form1 as a TopMost form, do the following:

res& = SetWindowPos (Form1.hWnd, HWND_TOPMOST, _
0, 0, 0, 0, FLAGS)
'if res&=0, there is an error

'To turn off topmost (make the form act normal again):

res& = SetWindowPos (Form1.hWnd, HWND_NOTOPMOST, _
0, 0, 0, 0, FLAGS)
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2569531
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 SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const HWND_TOPMOST = -1

Private Sub Form_Load()
    Call SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)
End Sub
0
 
LVL 4

Expert Comment

by:vindevogel
ID: 2569625
Dump this in a UserControl ....

Option Explicit

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)

Private conValue As Boolean

Private Property Get Value() As Boolean
    Value = conValue
End Property

Public Property Let Value(newValue As Boolean)
    conValue = newValue
    PropertyChanged Value
   
    Call FormStayOnTop(UserControl.Parent.hWnd, newValue)
End Property

Private Sub UserControl_Initialize()
    UserControl_Resize
End Sub

Private Sub UserControl_InitProperties()
    Value = True
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    conValue = PropBag.ReadProperty("Value", True)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("Value", conValue, True)
End Sub

Private Sub UserControl_Resize()
    On Error Resume Next
   
    UserControl.Width = ImageInterface.Width
    UserControl.Height = ImageInterface.Height
End Sub

Private Sub FormStayOnTop(FormHandle As Long, OnOff As Boolean)
Dim wFlags As Integer

Const Swp_Nosize As Integer = &H1
Const Swp_Nomove As Integer = &H2
Const Swp_NoActivate As Integer = &H10
Const Swp_ShowWindow As Integer = &H40
Const Hwnd_TopMost As Integer = -1
Const Hwnd_NoTopMost As Integer = -2

    On Error Resume Next
   
    wFlags = Swp_Nomove Or Swp_Nosize Or Swp_ShowWindow Or Swp_NoActivate

    SetWindowPos FormHandle, IIf(OnOff, Hwnd_TopMost, Hwnd_NoTopMost), 0, 0, 0, 0, wFlags
End Sub



Next time, you just have to pick your "OnTop Control" from your toolbox. better than programming it over and over and over again.

You activate it with
Form.OpTop1.Value = True
0
 

Expert Comment

by:ramanan
ID: 2571345
---Put this in a Module

    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
   
    Global Const conHwndTopmost = -1
    Global Const conSwpNoActivate = &H10
    Global Const conSwpShowWindow = &H40


---Put this code in the form load

    SetWindowPos hwnd, conHwndTopmost, 100, 100, 400, 141, conSwpNoActivate Or conSwpShowWindow

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question