windows z order (1 Viewer)

Jaye7

Registered User.
Local time
Today, 17:45
Joined
Aug 19, 2014
Messages
205
I have the following script which tells me what window is in the top of the order, does someone have a script that list the position for all windows.

Code:
Private Declare Function GetTopWindow Lib "user32.dll" (ByVal hWnd As Long) As Long Private Declare Function GetNextWindow Lib "user32.dll" Alias "GetWindow" (ByVal hWnd As Long, ByVal wFlag As Long) As Long Private Declare Function IsWindowVisible Lib "user32.dll" (ByVal hWnd As Long) As Long Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long  Private Sub Timer1_Timer()     Timer1.Enabled = False          Dim topHwnd As Long          topHwnd = GetTopWindow(topHwnd)     Do While topHwnd         If IsWindowVisible(topHwnd) Then             ' test for windows that can be at top but you don't care about             ' for example, the taskbar will probably be the top window most of the time             ' You may want to get the window class(es) of such windows & ignore the windows,             '   if they are of that class                          ' Do not Exit the loop if it is a window you don't care about             Exit Do         End If         topHwnd = GetNextWindow(topHwnd, 2&)     Loop     If topHwnd = 0& Then         MsgBox "No visible windows exist"     Else         Dim sCaption As String         Dim lLen As Long         lLen = GetWindowTextLength(topHwnd)         If lLen Then             sCaption = Space$(lLen)             GetWindowText topHwnd, sCaption, lLen + 1         Else             sCaption = "[Window has no caption]"         End If         MsgBox "Top window caption:" & vbNewLine & sCaption     End If  End Sub
 

Jaye7

Registered User.
Local time
Today, 17:45
Joined
Aug 19, 2014
Messages
205
Thanks JHB, a bit over my head though.
 

JHB

Have been here a while
Local time
Today, 09:45
Joined
Jun 17, 2012
Messages
7,732
Try the below code, create a new form, put a button into it, and place the code in on_click event, replace the "Private Sub Command0_Click()" with the name of yours.

Code:
Private Declare Function GetTopWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare Function GetNextWindow Lib "user32.dll" Alias "GetWindow" (ByVal hWnd As Long, ByVal wFlag As Long) As Long
Private Declare Function IsWindowVisible Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long

Private Sub Command0_Click()
  Dim sCaption As String
  Dim lLen As Long
  Dim topHwnd As Long
  
  topHwnd = GetTopWindow(topHwnd)
  Do While topHwnd
    If IsWindowVisible(topHwnd) Then
      lLen = GetWindowTextLength(topHwnd)
      If lLen Then
        sCaption = Space$(lLen)
        GetWindowText topHwnd, sCaption, lLen + 1
        MsgBox "Top window caption:" & vbNewLine & sCaption
      End If
    End If
    topHwnd = GetNextWindow(topHwnd, 2&)
  Loop
End Sub
 

Jaye7

Registered User.
Local time
Today, 17:45
Joined
Aug 19, 2014
Messages
205
Hi Jhb,

I had to change the code to PtrSafe and I get a message saying sub or function not defined, I have marked it in red

Code:
Private Sub Command0_Click()

 Dim sCaption As String
  Dim lLen As Long
  Dim topHwnd As LongPtr
  
  topHwnd = [COLOR=Red][B]GetTopWindow[/B][/COLOR](topHwnd)
  Do While topHwnd
    If [B][COLOR=Red]IsWindowVisible[/COLOR][/B](topHwnd) Then
      lLen = [B][COLOR=Red]GetWindowTextLength[/COLOR][/B](topHwnd)
      If lLen Then
        sCaption = Space$[COLOR=Red][B](lLen[/B][/COLOR])[B][COLOR=Red] ''''''''''''''''''''''''''''''''TYPE MISMATCH[/COLOR][/B]
        GetWindowText topHwnd, sCaption, lLen + 1
        MsgBox "Top window caption:" & vbNewLine & sCaption
      End If
    End If
    topHwnd = GetNextWindow(topHwnd, 2&)
  Loop
  
  End Sub
NEW Module code

Code:
Private Declare PtrSafe Function GetTopWindow Lib "user32.dll" (ByVal hWnd As LongPtr) As LongPtr
Private Declare PtrSafe Function GetNextWindow Lib "user32.dll" Alias "GetWindow" (ByVal hWnd As LongPtr, ByVal wFlag As LongPtr) As LongPtr
Private Declare PtrSafe Function IsWindowVisible Lib "user32.dll" (ByVal hWnd As LongPtr) As LongPtr
Private Declare PtrSafe Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As LongPtr
Private Declare PtrSafe Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As LongPtr) As LongPtr
 

JHB

Have been here a while
Local time
Today, 09:45
Joined
Jun 17, 2012
Messages
7,732
Try by removing the $.

Code:
sCaption = Space[COLOR=Red][COLOR=Black](lLen[/COLOR][/COLOR])
 

Jaye7

Registered User.
Local time
Today, 17:45
Joined
Aug 19, 2014
Messages
205
That works great, is there a way to then set a form to permanently be the top form, or should I post another thread for this.
 

Jaye7

Registered User.
Local time
Today, 17:45
Joined
Aug 19, 2014
Messages
205
That's OK, I found a script to lock the access form to the front.

Code:
‘In the form open event put
   
  Private Sub Form_Open(Cancel As Integer)
  Dim lR As Long
           lR = SetTopMostWindow(Forms!Form5.hwnd, True) ‘change Form5 to your access form
  End Sub
   
  ‘In a module put.
   
  Option Explicit
        Public Const SWP_NOMOVE = 2
        Public Const SWP_NOSIZE = 1
        Public Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
        Public Const HWND_TOPMOST = -1
        Public Const HWND_NOTOPMOST = -2
   
        Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos"  _
              (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
   
        Public Function SetTopMostWindow(hwnd As Long, Topmost As Boolean) _
           As Long
   
           If Topmost = True Then 'Make the window topmost
              SetTopMostWindow = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, _
                 0, FLAGS)
           Else
              SetTopMostWindow = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, _
                 0, 0,FLAGS)
              SetTopMostWindow = False
           End If
        End Function
 

Users who are viewing this thread

Top Bottom