VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Stay on Top (Fixed)

by VB Beginner (7 Submissions)
Category: Miscellaneous
Compatability: VB Script
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

I'm not sure sure if it's just my computer but, for some reason, my VB programs never stay on top of other windows all the time when I use the SetWindowPos API. This program still uses the SetWindowPos API but I added some stuff to make it work properly.

Rate Stay on Top (Fixed)

Option Explicit
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
Private Const HWND_TOPMOST = -1 'bring to top and stay there
Private Const SWP_NOMOVE = &H2 'don't move window
Private Const SWP_NOSIZE = &H1 'don't size window
Private Sub Form_Load()
Call SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Sub
Private Sub Timer1_Timer()
'If I remove the "If GetActiveWindow = 0 then" portion, then the form will also go on top of context menus, also
If GetActiveWindow = 0 Then
Call SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End If
End Sub

Download this snippet    Add to My Saved Code

Stay on Top (Fixed) Comments

No comments have been posted about Stay on Top (Fixed). Why not be the first to post a comment about Stay on Top (Fixed).

Post your comment

Subject:
Message:
0/1000 characters