Results 1 to 11 of 11

Thread: [Resolved]Current window active?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    [Resolved]Current window active?

    Hi there, I have a (difficult) question:

    I use sendkeys for enter cheats in a game.
    And use hotkeys witch work ine very window.
    Now I want to make this:

    If i am in my game, so game-screen is active, and I hit my hotkey that it checks if my game, for exmaple. mygame.exe is the active window.

    So is my game.exe the active window then Sendkeys else msgbox "Start up your game"

    Maybe its easy,maybe not,

    I thank u
    Last edited by Account; Sep 9th, 2006 at 05:06 AM.

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    417

    Re: Current window active?

    Have you had a look at GetActiveWindow that wll return the HWND so you can then check that to see if your in your game.
    When your dreams come true.
    On error resume pulling hair out.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    Re: Current window active?

    hmm, I saw someting like this:

    http://www.vbforums.com/showthread.p...etActiveWindow

    The guy there says:
    GetActiveWindow can only return the handle of the current window belonging to the calling thread. Since the game belongs to another process you can't use that function, use the GetForegroundWindow API function instead.
    Secnd thing, thnx:P

    Third thing, where to find the GetForegroundWindow API function?

    [Sorry for my lazy question]

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Current window active?

    Check at AllAPI.net. You can download API Guide. There are almost all APIs with all descriptions on how to use and what each API does

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    Re: Current window active?

    ok thnx Gavio.

    i found this:

    VB Code:
    1. Private Declare Function GetForegroundWindow Lib "user32" () As Long
    2. Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    3. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    4. Private Sub Form_Activate()
    5.     'KPD-Team 1998
    6.     'URL: [url]http://www.allapi.net/[/url]
    7.     'E-Mail: [email][email protected][/email]
    8.     Dim Ret As Long
    9.     Do
    10.         'Get the handle of the foreground window
    11.         Ret = GetForegroundWindow()
    12.         'Get the foreground window's device context
    13.         Ret = GetDC(Ret)
    14.         'draw an ellipse
    15.         Ellipse Ret, 0, 0, 200, 200
    16.         DoEvents
    17.     Loop
    18. End Sub

    But how to make then, if I hit Command1_Click that it will check if mygame.exe is the current active/foreground window?

  6. #6
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Melbourne, Australia
    Posts
    415

    Re: Current window active?

    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    2. Private Declare Function GetForegroundWindow Lib "user32" () As Long
    3. Private Sub Form_Load()
    4. dim myGame as long
    5. dim currentFocus as long
    6.     currentFocus = GetForegroundWindow()
    7.     myGame = FindWindow(yourGamesClassName,yourGamesCaption)
    8.     if currentFocus=myGame then
    9.         ''Your game is in focus
    10.     end if
    11. End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    Re: Current window active?

    Thank you for the reaction.
    I think I get it.

    But the question is, how to set which 'current' foreground window is my game?
    So for example:

    i want my program only works on Pinball.exe

    is it then:

    VB Code:
    1. if currentFocus=Pinball.exe then

    ?

  8. #8
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Current window active?

    This needs a timer with interval 1. It will change the caption of the program, depending on whether pinball has focus or not.

    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    2. Private Declare Function GetForegroundWindow Lib "user32" () As Long
    3. Private Sub Timer1_Timer()
    4. Dim myGame As Long
    5. Dim currentFocus As Long
    6.     currentFocus = GetForegroundWindow()
    7.     'myGame = FindWindow("Enter the Class name", "Enter the caption of the Game")
    8.     'if u don't know anyone of them, then put vbNullString, but must have one to search
    9.     myGame = FindWindow(vbNullString, "3D Pinball for Windows - Space Cadet")
    10.     If currentFocus = myGame Then
    11.         Me.Caption = "Your Game has focus"
    12.     Else
    13.         Me.Caption = "Lost focus"
    14.     End If
    15. End Sub

  9. #9
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Melbourne, Australia
    Posts
    415

    Re: Current window active?

    To find the class and caption names of a specific window, use Spy++

    Start>All Progs>MVS 6.0>Tools>Spy++

    If your application is not fullscreen, press Alt-F3, then drag the crosshair over the application.

    Then, use FindWindow(Class,Caption), using the class and caption returned in the window finder.
    Note, both class and caption have to be within " "

    If it is full screen, check the name of the window in your toolbar, then manually search through the list until you come to the caption.

    The list shows [Handle][Caption][Class], so you can fill in FindWindow that way

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    Re: Current window active?

    Aah thnx all.

    Little question:

    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    2. Private Declare Function GetForegroundWindow Lib "user32" () As Long
    3. Private Sub Timer1_Timer()
    4. Dim myGame As Long
    5. Dim currentFocus As Long
    6.     currentFocus = GetForegroundWindow()
    7.     'myGame = FindWindow("Enter the Class name", "Enter the caption of the Game")
    8.     'if u don't know anyone of them, then put vbNullString, but must have one to search
    9.     myGame = FindWindow(vbNullString, "3D Pinball for Windows - Space Cadet")
    10.     If currentFocus = myGame Then
    11.         Me.Caption = "Your Game has focus"
    12.     Else
    13.         Me.Caption = "Lost focus"
    14.     End If
    15. End Sub

    If I put this in a timer, then it would check everytime if the game is active? Am I right?

  11. #11
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Current window active?

    Yes. If you add a timer and set its interval to a value then it will check if pinball is active, and change the caption according.

    For your program, you could set the hotkey if the game has focus, and remove the hotkey if it doesn't, that way you make sure that pressing a certain key won't send the keys by accident.

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