Solved

make a form stay on top

Posted on 2000-02-29
7
390 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

706 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

15 Experts available now in Live!

Get 1:1 Help Now