Solved

make a form stay on top

Posted on 2000-02-29
7
391 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now