Keeping your VB form always on top of all applications

How do you keep your VB form on top of all open aplications.  It doesn't have to have focus.  An example would be to have a word document open with focus and the VB form be on top of it)

Thank you
flipper1Asked:
Who is Participating?
 
RuchiConnect With a Mentor Commented:
the above code
0
 
RuchiCommented:
From the web:

'This sample will show you how to make your form to be Top Most (Like ICQ)
'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'Add 2 CommandButtons to your form (named Command1 and Command2).
'When you will press the first button the form will be top most.
'When you will press the second button the form will back to the regular state.
'Insert this code to the module :

Declare Function SetWindowPos Lib "user32" (ByVal h%, ByVal hb%, _
ByVal x%, ByVal Y%, ByVal cx%, ByVal cy%, ByVal F%) As Integer
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

'Insert the following code to your form:

Private Sub Command1_Click()
res = SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, flags)
End Sub

Private Sub Command2_Click()
res = SetWindowPos(Form1.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, flags)
End Sub
0
 
MarineCommented:
Someone just got easy points :-)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
RuchiCommented:
This one is better than the original code I gave.

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
 
RuchiCommented:
From the web, also:

"Let your form be right on top even though form does not have the focus. Other applications will still run normally, just with your form floating on top"


Option Explicit

Private Const HWND_BOTTOM = 1
Private Const HWND_NOTOPMOST = -2
Private Const HWND_TOP = 0
Private Const HWND_TOPMOST = -1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1

Private Declare Function GetActiveWindow Lib "user32" () As Long


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
Code:


'Pass the form's hwnd.  The first routine ensures the form will
'always be on top.  The second reverts back to the normal state

Public Sub MakeWindowAlwaysTop(hwnd As Long)

 SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, _
   SWP_NOMOVE + SWP_NOSIZE

End Sub

Public Sub MakeWindowNotTop(hwnd As Long)

  SetWindowPos hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, _
     SWP_NOMOVE + SWP_NOSIZE

End Sub

0
 
MarineCommented:
The code is practically the same you just tooks the steps out. But i don't know if he likes it since the form is locked you can't move it :-)
0
 
flipper1Author Commented:
Thank you
0
 
MarineCommented:
flipper this question worth a lot less then you have it for .
0
 
RuchiCommented:
How are you doing with this? Please let me know.

I agree with Marine. Should be 50 points!
0
 
flipper1Author Commented:
Ruchi I found a problem with this code.

When you press the botton to take away the always on top off.  It makes the window very small and if you dont have a title bar is disappears.  Please help
0
 
flipper1Author Commented:
oops I put this on the wrong question
0
 
flipper1Author Commented:
Here is my silly code
Don't laugh I'm new at this :)

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 Const HWND_NOTOPMOST = -2
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const HTCAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1

Private Sub Command2_Click()

  If Command2.Caption = "Always On Top" Then
      Command2.Caption = "Always On Top OFF"
      Else
       Command2.Caption = "Always On Top"
      End If
     
  If Command2.Caption = "Always On Top" Then
     
res = SetWindowPos(Me.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, flags)
Else
res = SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, flags)

End If
End Sub




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

End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    Call ReleaseCapture
    Call SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
End Sub

Private Sub Picture1_Click(Index As Integer)
Unload Me
End Sub

Private Sub Picture2_Click()
frmbuttons.WindowState = 1

End Sub

Private Sub Picture3_Click()
frmbuttons.WindowState = 0
End Sub
0
 
RuchiCommented:
Flipper1: Can you explain me what you want to do with your code?
0
 
flipper1Author Commented:
sorry I figured it out.

I just started with VB yesterday, but I'm sloooowly catching on :)
0
All Courses

From novice to tech pro — start learning today.