11-10-2022, 02:15 AM
So this is what I've came up with, and it works...
Now, the problem that I'm having is that this works -- but it only works *ONCE*. Start it up, click off to a different window, it jerks focus back to itself as expected. Click to a different window again, and... it detects it's not active, but it won't jerk focus back to itself like it's supposed to.
Any of you windows API gurus out there see something obvious that I'm missing here? (@Spriggsy) Why's this work once and then decide, "Nah! I don't wanna do that again??" Any ideas at all on this one would be appreciated.
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"
junk%& = SetForegroundWindow(hWnd)
Else
Print "Active"
End If
_Limit 30
Loop Until _KeyDown(27)
Now, the problem that I'm having is that this works -- but it only works *ONCE*. Start it up, click off to a different window, it jerks focus back to itself as expected. Click to a different window again, and... it detects it's not active, but it won't jerk focus back to itself like it's supposed to.
Any of you windows API gurus out there see something obvious that I'm missing here? (@Spriggsy) Why's this work once and then decide, "Nah! I don't wanna do that again??" Any ideas at all on this one would be appreciated.