eightban's memo

残しておきたい記事をまとめてみました。このブログに書いてあるドキュメントやブログで配布しているファイルの使用によって発生するいかなる損害に対してもこのブログの管理者は責任を負いません。使用する場合は自己責任のもとに使用してください。

AutoIt

autoit で指定したボタンなどの画像を 監視してクリックする (複数画像対応 改良版)

更新日:

イメージ

機能

複数画像対応
画面から画像を登録
指定した画像が見つかるまで待機するしないの選択が可能
画像認識の前と後に処理を追加することが可能
画像認識した後にマウスカーソル移動もしくはクリックすることは可能

スクリプト

#includ
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include "_ImageSearch_UDF.au3"

Global Const $Ask_On_Found = 0
Global Const $Mouse_Move_On_Found = 0
Global Const $Mouse_Click_On_Found = 1
Global Const $Wait_mode = 1
Global Const $iSleep_Time=500
;Global Const $iSleep_Time=1500
Global $sCount = 0
Global $mCount = 8
Dim $_Image[$mCount]
For $iz=1 To $mCount Step 1
 $_Image[$iz-1] = @ScriptDir & "\butonimagej" & $iz & ".bmp"
Next
Global $_msg_0 =  "  image"
Global $_msg_1 =  "* image"

dim $_msg[$mCount*2]
For $iz=1 To $mCount*2 Step 1
 $_msg[$iz-1] = $_msg_0
Next
 $_msg[8] = $_msg_1

Global $_title_1 =  "autoimageclosej"

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode
$mainwindow = GUICreate($_title_1, 140,300,100,50)
;$mainwindow = GUICreate($_title_1, 140,300)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
$msg1="stopping"
$labelx=GUICtrlCreateLabel($msg1, 70, 25)
$buttonx = GUICtrlCreateButton("push", 10, 20,50, 25)
$button1 = GUICtrlCreateButton("set1", 10, 50,50, 25)
$button2 = GUICtrlCreateButton("set2", 10, 80,50, 25)
$button3 = GUICtrlCreateButton("set3", 10, 110,50, 25)
$button4 = GUICtrlCreateButton("set4", 10, 140,50, 25)
$button5 = GUICtrlCreateButton("set5", 10, 170,50, 25)
$button6 = GUICtrlCreateButton("set6", 10, 200,50, 25)
$button7 = GUICtrlCreateButton("set7", 10, 230,50, 25)
$button8 = GUICtrlCreateButton("set8", 10, 260,50, 25)
GUICtrlSetOnEvent($buttonx, "OKButtonx")
GUICtrlSetOnEvent($button1, "OKButton1")
GUICtrlSetOnEvent($button2, "OKButton2")
GUICtrlSetOnEvent($button3, "OKButton3")
GUICtrlSetOnEvent($button4, "OKButton4")
GUICtrlSetOnEvent($button5, "OKButton5")
GUICtrlSetOnEvent($button6, "OKButton6")
GUICtrlSetOnEvent($button7, "OKButton7")
GUICtrlSetOnEvent($button8, "OKButton8")
$Pic1 = GUICtrlCreatePic($_Image[0], 70, 50, 50, 25)
$Pic2 = GUICtrlCreatePic($_Image[1], 70, 80, 50, 25)
$Pic3 = GUICtrlCreatePic($_Image[2], 70, 110, 50, 25)
$Pic4 = GUICtrlCreatePic($_Image[3], 70, 140, 50, 25)
$Pic5 = GUICtrlCreatePic($_Image[4], 70, 170, 50, 25)
$Pic6 = GUICtrlCreatePic($_Image[5], 70, 200, 50, 25)
$Pic7 = GUICtrlCreatePic($_Image[6], 70, 230, 50, 25)
$Pic8 = GUICtrlCreatePic($_Image[7], 70, 260, 50, 25)
$label1=GUICtrlCreateLabel($_msg_0, 65,60)
$label2=GUICtrlCreateLabel($_msg_0, 65,90)
$label3=GUICtrlCreateLabel($_msg_0, 65,120)
$label4=GUICtrlCreateLabel($_msg_0, 65,150)
$label5=GUICtrlCreateLabel($_msg_0, 65,180)
$label6=GUICtrlCreateLabel($_msg_0, 65,210)
$label7=GUICtrlCreateLabel($_msg_0, 65,240)
$label8=GUICtrlCreateLabel($_msg_0, 65,270)
GUICtrlSetColor($label1, 0xff0000)
GUICtrlSetColor($label2, 0xff0000)
GUICtrlSetColor($label3, 0xff0000)
GUICtrlSetColor($label4, 0xff0000)
GUICtrlSetColor($label5, 0xff0000)
GUICtrlSetColor($label6, 0xff0000)
GUICtrlSetColor($label7, 0xff0000)
GUICtrlSetColor($label8, 0xff0000)
GUICtrlSetBkColor($label1, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetBkColor($label2, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetBkColor($label3, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetBkColor($label4, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetBkColor($label5, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetBkColor($label6, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetBkColor($label7, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetBkColor($label8, $GUI_BKCOLOR_TRANSPARENT)


GUISetState(@SW_SHOW)

WinWait($_title_1)
WinActivate($_title_1)
WinSetOnTop($_title_1, "", 1)


$ix=0
While 1
  if $msg1="stopping"  Then
Else

	GUICtrlSetData ($labelx, $msg1)
	$iy=mod($ix, $mCount)
    $_Image_x=$_Image[$iy]

	GUICtrlSetData ($label1, $_msg[$iy+8])
	GUICtrlSetData ($label2, $_msg[$iy+7])
	GUICtrlSetData ($label3, $_msg[$iy+6])
	GUICtrlSetData ($label4, $_msg[$iy+5])
	GUICtrlSetData ($label5, $_msg[$iy+4])
	GUICtrlSetData ($label6, $_msg[$iy+3])
	GUICtrlSetData ($label7, $_msg[$iy+2])
	GUICtrlSetData ($label8, $_msg[$iy+1])
    _osu($_Image_x)

  EndIf


	if $ix >  $mCount-1 Then $ix = 0

WEnd

Func OKButtonx()
;  MsgBox(0, "GUI Event", "You pressed OK!")
if $msg1="stopping"  Then
  $msg1="executing"

Else
  $msg1="stopping"
  	$ix+=1

EndIf
GUICtrlSetData ($labelx, $msg1)


EndFunc


Func OKButton1()
 _imageget($_Image[0],$Pic1)
EndFunc
Func OKButton2()
 _imageget($_Image[1],$Pic2)
EndFunc
Func OKButton3()
 _imageget($_Image[2],$Pic3)
EndFunc
Func OKButton4()
 _imageget($_Image[3],$Pic4)
EndFunc
Func OKButton5()
 _imageget($_Image[4],$Pic5)
EndFunc
Func OKButton6()
 _imageget($_Image[5],$Pic6)
EndFunc
Func OKButton7()
 _imageget($_Image[6],$Pic7)
EndFunc
Func OKButton8()
 _imageget($_Image[7],$Pic8)
EndFunc

Func _imageget($imgx="",$picx="")

MsgBox(64 + 262144, 'ImageSearch', 'At first, create a file bmp,' & @CRLF & 'photos that will search on the screen!')
_ImageSearch_Create_BMP($imgx)
ConsoleWrite(" Save image: " & $imgx & @CRLF )
Sleep(3000)

GUICtrlSetImage ($picx,$imgx)
EndFunc

Func CLOSEClicked()
 ; MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
Exit
EndFunc

Func _opt($ia=0)
Switch Int($ia+1)
	Case 0 To 3
	Case 4 To 5
;		Sleep(3000)
;		Send("{END}")
;		Sleep(1000)

	Case Else
EndSwitch
EndFunc
Func _opt2($ib=0)
Switch Int($ib+1)
	Case 0 To 3
	Case 4 To 5
;		Sleep(3000)
;		Send("!{LEFT}")
;		Sleep(1000)

	Case Else
		EndSwitch
EndFunc


;===========================
Func _osu($imgz="")
If FileExists($imgz) Then
		_opt($ix)

	$sCount += 1
;    Local $return = _ImageSearch($imgz,30)
    Local $return = _ImageSearch_Wait($imgz,100,30)

    If $return[0] = 1 Then
        ConsoleWrite('- [' & $sCount & '] Image found:' & " X=" & $return[1] & " Y=" & $return[2] & @CRLF)
        If $Mouse_Move_On_Found Then
			MouseMove($return[1], $return[2])
			Sleep($iSleep_Time)
		EndIf
        If $Mouse_Click_On_Found Then MouseClick("left", $return[1], $return[2])
        ToolTip('(' & $sCount & "] Image found:" &  " X=" & $return[1] & " Y=" & $return[2], 1, 1)
		_opt2($ix)
		$ix+=1

	Else
        If Not $Wait_mode Then	$ix+=1
    EndIf

    Sleep(2000)
Else

	$ix+=1

EndIf
EndFunc

説明

インストールなど

http://memo.eightban.com/autoit/autoit-_imagesearch_udf

$Mouse_Move_On_Found

画像が見つかった時にマウスカーソルを動かすかどうか

$Mouse_Click_On_Found

画像が見つかった時にクリックするかどうか

$Wait_mode

目的の画像が見つかるまで待ち続けるかどうか

プッシュボタンで次の画像を検索します

_opt

画像検索する前に行う処理

_opt2

画像検索後に行う処理

使い方

セットボタンで画像を登録してください
その後 PUSH ボタンで画像認識を開始してください。

その他

コピペした後は下にある文字は次のように変更してください
&#91; [
&amp; &
&lt; <
&gt; >

-AutoIt

Copyright© eightban's memo , 2024 All Rights Reserved Powered by STINGER.