11-10-2022, 02:40 AM
Try this @Pete:
A mouseclick offscreen does a good job of solving the issue (it's a limitation on setforegroundwindow and how often it works and under what conditions). So does a press of the ALT key before the function call, but _SCREENPRINT doesn't seem to easily accept one of those, and I didn't want to toss in any more API calls than were necessary.
Code: (Select All)
'public domain
Const SWP_NOSIZE = &H0001 'ignores cx and cy size parameters
Const SWP_NOMOVE = &H0002 'ignores x and y position parameters
Const SWP_NOZORDER = &H0004 'keeps z order and ignores hWndInsertAfter parameter
Const SWP_NOREDRAW = &H0008 'does not redraw window changes
Const SWP_NOACTIVATE = &H0010 'does not activate window
Const SWP_FRAMECHANGED = &H0020
Const SWP_SHOWWINDOW = &H0040
Const SWP_HIDEWINDOW = &H0080
Const SWP_NOCOPYBITS = &H0100
Const SWP_NOOWNERZORDER = &H0200
Const SWP_NOSENDCHANGING = &H0400
Const SWP_DRAWFRAME = SWP_FRAMECHANGED
Const SWP_NOREPOSITION = SWP_NOOWNERZORDER
Const SWP_DEFERERASE = &H2000
Const SWP_ASYNCWINDOWPOS = &H4000
Const HWND_TOP = 0 'window at top of z order no focus
Const HWND_BOTTOM = 1 'window at bottom of z order no focus
Const HWND_TOPMOST = -1 'window above all others no focus unless active
Const HWND_NOTOPMOST = -2 'window below active no focus
Declare Dynamic Library "user32"
' Function FindWindowA%& (ByVal lpClassName%&, Byval lpWindowName%&)
Function SetWindowPos& (ByVal hWnd%&, Byval hWndInsertAfter%&, Byval X&, Byval Y&, Byval cx&, Byval cy&, Byval uFlags~&)
Function GetForegroundWindow%&
Function SetForegroundWindow%& (ByVal hwnd As _Offset) 'set foreground window process(focus)
End Declare
Declare Dynamic Library "kernel32"
Function GetLastError~& ()
End Declare
Dim hWnd As _Offset
_Title "This Window will always be on Top" 'any title
_Delay .5 'delay allows user to click focus on other windows
hWnd = _WindowHandle 'FindWindowA(0, _OFFSET(t))
If 0 = SetWindowPos(hWnd, HWND_TOPMOST, 200, 200, 0, 0, SWP_NOSIZE Or SWP_NOACTIVATE) Then
Print "SetWindowPos failed. 0x" + LCase$(Hex$(GetLastError))
End If
Do
Cls
x%& = GetForegroundWindow%& 'find currently focused process handle
Locate 3, 1
Print "Program handle:"; hWnd; "Focus handle:"; x%&
If x%& <> hWnd Then
Print "Not Active"
_ScreenClick -32000, -32000
junk%& = SetForegroundWindow(hWnd)
Else
Print "Active"
End If
_Limit 30
Loop Until _KeyDown(27)
A mouseclick offscreen does a good job of solving the issue (it's a limitation on setforegroundwindow and how often it works and under what conditions). So does a press of the ALT key before the function call, but _SCREENPRINT doesn't seem to easily accept one of those, and I didn't want to toss in any more API calls than were necessary.