Results 1 to 13 of 13

Thread: SetFocus API

  1. #1

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125

    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:
    1. 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
    <removed by admin>

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  3. #3

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    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>

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Try using the SetForegroundWindow API instead.
    VB Code:
    1. 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

  6. #6

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    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.
    <removed by admin>

  7. #7
    New Member
    Join Date
    Nov 2014
    Posts
    7

    Re: SetFocus API

    Quote Originally Posted by MidgetsBro View Post
    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:
    1. 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:
    1. Private Declare Function SetFocus2 Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long

  8. #8
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    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

  9. #9
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: SetFocus API

    Quote Originally Posted by Bonnie West View Post
    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)

  10. #10
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: SetFocus API

    Thanks Bonnie,
    Does it work on all versions of Windows (XP Vista W7 W8.1 W10) ?

    Regards,
    Rob

  11. #11
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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)

  12. #12

    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.

  13. #13
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: SetFocus API

    Quote Originally Posted by Julianne.Sauer View Post
    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
  •  



Click Here to Expand Forum to Full Width