BolnoYGaD
BolnoYGaD

Reputation: 31

AutoIT WinWaitActive doesn't uderstands that window is active

I want to get pixel color from a game and react. So I have this sript:

#include <Color.au3>

Local $pause = False;

$WinName = "Game" ; Let's say there is a game with this name =)
$hwnd = WinGetHandle($WinName) ; Checked handle with powershell and Au3Info, handle is correct

local $res

Opt("PixelCoordMode", 2);

HotKeySet("{F10}", "exitNow");
HotKeySet("{F11}", "Pause");

While 1
   ;WinWaitActive($hwnd) ; The script stops here if it is uncommented no matter if window is active or not
    if ( $pause ) Then
      $res = GetPos();
      ConsoleWrite ( StringFormat ("Result color: %s %s", $res[0], $res[1] ) )
      Sleep (1000)
    EndIf
WEnd

Func exitNow()
    Exit
EndFunc

Func Pause()
    $pause = Not $pause
EndFunc

Func GetPos()
    local $var = Hex(PixelGetColor(5, 5, $hwnd)) ; Only works in windowed mode, FullScreen return some random colors like 00FFFFFF or 00AAAAAA
    $var = StringTrimLeft($var,2) ; Removing first 2 numbers they r always 00
    local $var1 = Hex(PixelGetColor(15, 5, $hwnd)) ; Only works in windowed mode, FullScreen return some random colors like 00FFFFFF or 00AAAAAA
    $var1 = StringTrimLeft($var1,2) ; Removing first 2 numbers they r always 00
    local $result[2] = [ $var, $var1 ]
    return $result
EndFunc

Main script should before window is active and only then should try to GetPixelColor but that never happens, no matter what I do, i've tried WindowActivate still no result.

a) - What am I doing wrong ? Or may be there is another way to check if window is currently active ?

So at the moment I start script disabled, and when I activate window manually I press F11 to enable.

b) - PixelGetColor only works if game is running in Windowed mode, if it's a fullscreen mode result is unpredictable. Is there a way to make PixelGetColor work in fullscreen.

I've tried to run game x32, x64, DX9 and DX11 in different combinations Fullscreen result is just wrong.

ADDED:

Now While looks like this and that works :) Thanks to Xenobeologist!!!

While 1
    $hwnd = WinGetHandle('[Active]');
    If ( $pause ) Then
        If WinGetTitle($hwnd) <> $WinName Then
            Pause()
            ContinueLoop
        EndIf
        $res = GetPos();
        ConsoleWrite ( StringFormat ("Result color: %s %s", $res[0], $res[1] ) )
        Sleep (1000)
    Else
        If WinGetTitle($hwnd) = $WinName Then
            Pause()
            ContinueLoop
        EndIf
    EndIf
WEnd

a) is now solved

b) is still not solved. One more thing, topic of this question doesn't say anything bout this question, should add info bout this into the topic or it's better to start a new thread? a) was my main question, it would be prefect to solve b) as well but I can live without it. As far as I understood it's much more complicated. What do you think?

ADDED2:

As far as I understand problem b) can be solved by using more complex code. Here http://www.autohotkey.com/board/topic/63664-solved-imagesearch-failure-wdirectx-fullscreen-gamewin7/ was discussed pretty same problem. It's AHK and ImageSearch function, but im pretty sure that the reason I get wrong colours in fullscreen is the same. I don't want to complicate code that much and PrintScreen is too slow so I'll use windowed mode and wont be bothering with b).

Upvotes: 0

Views: 2684

Answers (1)

Xenobiologist
Xenobiologist

Reputation: 2151

Does this help?

#include <MsgBoxConstants.au3>

$handle = WinGetHandle('[Active]')

ConsoleWrite('!Title   : ' & WinGetTitle($handle) & @CRLF)
ConsoleWrite('!Process : ' & WinGetProcess($handle) & @CRLF)
ConsoleWrite('!Text    : ' & WinGetText($handle) & @CRLF)

Sleep(Random(10, 3000, 1))

If WinActive($handle) Then MsgBox($MB_SYSTEMMODAL, "", "WinActive" & @CRLF & "Scite ")

Upvotes: 2

Related Questions