-
August 29th, 2011, 05:10 AM
#1
SetForegroundWindow & Windows 7
Hey guys
I'm trying to use SetForegroundWindow to automatically get the focus on my App whenever the mouse is on top of it, that's working very well in the debug environment but not out of it.
When I try to launch the App outside of the debug environment, the App icon flashing on the taskbar (Windows 7) and I have to press on it to get the App in focus.
how can I avoid this and get it to work like in the debug environment ?
-
August 29th, 2011, 08:30 AM
#2
Re: SetForegroundWindow & Windows 7
Could you attach your project her, or show the problematic code. It is quite difficult to guess where your problem might be
-
August 29th, 2011, 11:04 AM
#3
Re: SetForegroundWindow & Windows 7
I hate it when that happens. Yeah post your code, so we can look for any additional problems.
Using the Flash API does not seem to work, however there are a couple of general solutions to this problem that should work.
If you simulate pressing the menu(alt) key before setforegroundwindow, this should avoid the flashing yellow task icon.
Code:
Const KEYEVENTF_KEYUP As Int32 = 2
Const VK_MENU As Int32 = 18
Public Declare Function apikeybd_event Lib "user32" Alias "keybd_event" (ByVal vKey As Int32, ByVal bScan As Int32, ByVal dwFlags As Int32, ByVal dwExtraInfo As Int32) As Boolean
There are a couple other API that relate to the setting of a foreground window. For example here is a more complete function with those API:
Code:
Const ASFW_ANY As Int32 = -1
Const LSFW_LOCK As Int32 = 1
Const LSFW_UNLOCK As Int32 = 2
Private Declare Function apiLockSetForegroundWindow Lib "user32" Alias "LockSetForegroundWindow" (ByVal uLockCode As Int32) As Boolean
Private Declare Function apiAllowSetForegroundWindow Lib "user32" Alias "AllowSetForegroundWindow" (ByVal dwProcessId As Int32) As Boolean
Public Function ForceForeground(ByVal hWnd As Int32) As Boolean
apiLockSetForegroundWindow(LSFW_UNLOCK) ''''''Unlock setforegroundwindow calls
apiAllowSetForegroundWindow(ASFW_ANY) ''''''''Allow setforeground window calls
KeyEvent(VK_MENU, False, True, -12) '''''''''''''''Lift menu key if pressed, and it also allows the foreground window to be set
ForceForeground = CBool(apiSetForegroundWindow(hWnd)) 'Set foreground window
apiLockSetForegroundWindow(LSFW_LOCK) ''''''''Lock other apps from using setforegroundwindow
End Function
Another solution that works in some situations uses the accessibility API AccessibleObjectFromWindow, and SELFLAG_TAKEFOCUS constant. This can be tricky, so try the first solution.
Const SELFLAG_TAKEFOCUS As Int32 = 1
Const SELFLAG_TAKESELECTION As Int32 = 2
Const OBJID_WINDOW As Int32 = &H0
Public Declare Function apiAccessibleObjectFromWindow Lib "oleacc" Alias "AccessibleObjectFromWindow" (ByVal Hwnd As Int32, ByVal dwId As Int32, ByRef riid As Guid, ByRef ppacc As Global.Accessibility.IAccessible) As Int32
Public Function FocusControl(ByVal cwnd As Int32) As Boolean
On Error Resume Next
Dim accObj As Global.Accessibility.IAccessible = Nothing
apiAccessibleObjectFromWindow(cwnd, OBJID_WINDOW, IID_IAcce, accObj)
accObj.accSelect(SELFLAG_TAKEFOCUS)
If accObj.accFocus Is accObj Then FocusControl = True
accObj = Nothing
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|