|
-
Sep 2nd, 2006, 11:17 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Sep 2nd, 2006, 11:20 AM
#2
Hyperactive Member
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.
-
Sep 2nd, 2006, 11:26 AM
#3
Thread Starter
Hyperactive Member
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]
-
Sep 2nd, 2006, 11:29 AM
#4
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
-
Sep 2nd, 2006, 11:38 AM
#5
Thread Starter
Hyperactive Member
Re: Current window active?
ok thnx Gavio.
i found this:
VB Code:
Private Declare Function GetForegroundWindow Lib "user32" () As Long
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
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Form_Activate()
'KPD-Team 1998
'URL: [url]http://www.allapi.net/[/url]
Dim Ret As Long
Do
'Get the handle of the foreground window
Ret = GetForegroundWindow()
'Get the foreground window's device context
Ret = GetDC(Ret)
'draw an ellipse
Ellipse Ret, 0, 0, 200, 200
DoEvents
Loop
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?
-
Sep 2nd, 2006, 10:01 PM
#6
Hyperactive Member
Re: Current window active?
VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Sub Form_Load()
dim myGame as long
dim currentFocus as long
currentFocus = GetForegroundWindow()
myGame = FindWindow(yourGamesClassName,yourGamesCaption)
if currentFocus=myGame then
''Your game is in focus
end if
End Sub
-
Sep 3rd, 2006, 05:37 AM
#7
Thread Starter
Hyperactive Member
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:
if currentFocus=Pinball.exe then
?
-
Sep 3rd, 2006, 05:50 AM
#8
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:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Sub Timer1_Timer()
Dim myGame As Long
Dim currentFocus As Long
currentFocus = GetForegroundWindow()
'myGame = FindWindow("Enter the Class name", "Enter the caption of the Game")
'if u don't know anyone of them, then put vbNullString, but must have one to search
myGame = FindWindow(vbNullString, "3D Pinball for Windows - Space Cadet")
If currentFocus = myGame Then
Me.Caption = "Your Game has focus"
Else
Me.Caption = "Lost focus"
End If
End Sub
-
Sep 3rd, 2006, 06:52 AM
#9
Hyperactive Member
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
-
Sep 3rd, 2006, 09:46 AM
#10
Thread Starter
Hyperactive Member
Re: Current window active?
Aah thnx all.
Little question:
VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Sub Timer1_Timer()
Dim myGame As Long
Dim currentFocus As Long
currentFocus = GetForegroundWindow()
'myGame = FindWindow("Enter the Class name", "Enter the caption of the Game")
'if u don't know anyone of them, then put vbNullString, but must have one to search
myGame = FindWindow(vbNullString, "3D Pinball for Windows - Space Cadet")
If currentFocus = myGame Then
Me.Caption = "Your Game has focus"
Else
Me.Caption = "Lost focus"
End If
End Sub
If I put this in a timer, then it would check everytime if the game is active? Am I right?
-
Sep 4th, 2006, 02:07 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|