|
-
Feb 15th, 2002, 08:44 PM
#1
Thread Starter
PowerPoster
SetFocus API
I am trying to use the SetFocus API to set focus to a program that I did not program, but it is telling me "Member already exists in an object module from which this object model derives" but I don't have SetFocus declared anywhere else. The only thing I can think of is where I use Me.SetFocus on MY form, but why can't I use the API? Why are the names not playing well together? I even tried changing the API declare to this:
VB Code:
Private Declare Function SetFocus Lib "user32" Alias "SetFocus2" (ByVal hwnd As Long) As Long
so that it has the Alias of SetFocus2, but it still gives me the same problem. Can anyone help?
Thanks
-Joey
-
Feb 15th, 2002, 11:32 PM
#2
Did you try putting the Declare in a module?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Feb 15th, 2002, 11:34 PM
#3
Thread Starter
PowerPoster
Nope. I've been putting all the declares on the form, cause there is only one form. I wanna keep the size down. Will a module add more to the size?
----------------
I just tried that and it didn't work. I get the error "Wrong number or arguments or invalid property assignment" but I am sending the right amount or arguments, just the hWnd of the window I want to setfocus to. I found the hWnd using FindWindow, but now it won't let me set focus to it.
Last edited by MidgetsBro; Feb 15th, 2002 at 11:37 PM.
<removed by admin>
-
Feb 15th, 2002, 11:48 PM
#4
Since SetFocus is part of the form you can just use an alias:
Private Declare Function PutFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
-
Feb 15th, 2002, 11:49 PM
#5
Try using the SetForegroundWindow API instead.
VB Code:
Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Feb 15th, 2002, 11:55 PM
#6
Thread Starter
PowerPoster
Thanks crptcblade. That's what I was wondering, if there was a different API declare that I could use. Now I know there is. I'll test it and see if it works.
-
Sep 24th, 2015, 10:36 AM
#7
New Member
Re: SetFocus API
 Originally Posted by MidgetsBro
I am trying to use the SetFocus API to set focus to a program that I did not program, but it is telling me "Member already exists in an object module from which this object model derives" but I don't have SetFocus declared anywhere else. The only thing I can think of is where I use Me.SetFocus on MY form, but why can't I use the API? Why are the names not playing well together? I even tried changing the API declare to this:
VB Code:
Private Declare Function SetFocus Lib "user32" Alias "SetFocus2" (ByVal hwnd As Long) As Long
so that it has the Alias of SetFocus2, but it still gives me the same problem. Can anyone help?
Thanks
-Joey
You should remember this rule. The alias in this type of declaration is actually the original name of the function, and the true alias should follow the Declare Function statement.
In other words, your declaration should be as:
VB Code:
Private Declare Function SetFocus2 Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
-
Nov 15th, 2015, 02:40 AM
#8
Re: SetFocus API
Karl Peterson had an article ("Force Your Way to the Foreground") on this, and a download on his web site, if you can find it.
I have not read the article recently, so I cannot recall if it is only forcing to the foreground, or whether it is also setting focus ?
If you can find his web site, you can browse his related articles and downloads, and home in on what you need.
I found his site -
http://test.epeterson.net/
There are download pages as well.
I am old, and my memory ain't great, but I recall that the solution varied as New versions of Windows were released.
So if you do get to grips with it, I would appreciate you summarizing it all, when you get a resolution.
Rob
PS there is a thread in the general VB6 section, where members are lamenting the death of some VB6 sites, including Karl's
http://www.vbforums.com/showthread.p...-PSC-CDs/page2
If you jump to the latest post, you will see me mention that The site I posted above, is fairly hollow, and the downloads are not available (But the OP of this thread, can read the descriptions for all the example projects)
I have asked any member who knows Karl, if he/she could get Karl's permission to share some of his projects (I have most of them)
Last edited by Bobbles; Nov 15th, 2015 at 02:59 AM.
Reason: I added an important PS
-
Nov 15th, 2015, 04:16 AM
#9
Re: SetFocus API
 Originally Posted by Bonnie West
The attached project demonstrates how to utilize various APIs to force a window to the foreground. Keep in mind though, that these techniques would probably annoy your users if your window forcibly stealed the focus from whatever they're currently doing. 
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Nov 15th, 2015, 05:13 AM
#10
Re: SetFocus API
Thanks Bonnie,
Does it work on all versions of Windows (XP Vista W7 W8.1 W10) ?
Regards,
Rob
-
Nov 15th, 2015, 05:55 AM
#11
Re: SetFocus API
I've only tested those APIs in XP, Vista and 7 but I believe at least one of them should also work in 8, 8.1 and 10.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jun 10th, 2020, 10:03 AM
#12
New Member
Re: SetFocus API
If you use win 10, you must use keyword PtrSafe before Function or Sub to run macro
Private Declare PtrSafe Function GetForegroundWindow Lib "user32" () As Long
Private Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
HwndExcel = GetForegroundWindow ()
Sendkeys Alt+Tab or you can use any API Function
HwndOtherProgram = GetForegroundWindow ()
SetForegroundWindow(HwndExcel) or SetForegroundWindow(HwndOtherProgram) Very good replacement for auto press Alt + Tab in win10 when Alt + Tab does not offer a fixed window like win 7 and earlier
---
Best Regards!
Hannah J. Parrish
Last edited by Shaggy Hiker; Jun 10th, 2020 at 10:30 AM.
Reason: Removed email to protect against bots.
-
Jun 10th, 2020, 02:40 PM
#13
Re: SetFocus API
 Originally Posted by Julianne.Sauer
If you use win 10, you must use keyword PtrSafe before Function or Sub to run macro
Private Declare PtrSafe Function GetForegroundWindow Lib "user32" () As Long
Private Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
HwndExcel = GetForegroundWindow ()
Sendkeys Alt+Tab or you can use any API Function
HwndOtherProgram = GetForegroundWindow ()
SetForegroundWindow(HwndExcel) or SetForegroundWindow(HwndOtherProgram) Very good replacement for auto press Alt + Tab in win10 when Alt + Tab does not offer a fixed window like win 7 and earlier
---
Best Regards!
Hannah J. Parrish
You might find this code useful:
https://github.com/PeterSwinkels/Window-Scanner.net
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
|