尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

按键精灵找多图的代码实现|按下标顺序找到一组图中的任意一张,找到后鼠标移动到坐标位置,并返回boolean“True“,找不到则返回“False“

按键精灵找多图的代码实现|按下标顺序找到一组图中的任意一张,找到后鼠标移动到坐标位置,并返回boolean“True“,找不到则返回“False“ 百度了一下答案还行但是满足不了自己的需求所以索性自己造个轮子吧也让有缘人节省一些时间以下是查到的接下来上代码Function countSubStr(string1,string2) 查找string2在string1中出现的次数 start 1 length 1 countSubStr 0 While start len(string1) 1 str Left(string1, length) If Instr(start, str, string2)0 Then countSubStr countSubStr 1 End If start start 1 length length 1 Wend End Function Function locationPic(x1,y1,x2,y2,picName) 查找图片找到后鼠标移动到坐标 查找区域为左上坐标x1,y1到右下坐标x2,y2 返回booleanTrue,否则返回False,true-1,false0 默认附件路径 FindPic x1, y1, x2, y2, Attachment:\ picName .bmp, 0.8, tempX, tempY If tempX 0 And tempY 0 Then MoveTo tempX, tempY locationPic True Else locationPic False End If End Function Function findMultiPics(x1,y1,x2,y2,pics) 找多图,结合循环和判断语句可以有更多玩法 返回值类型为boolean,true-1,false0 查找区域为左上坐标x1,y1到右下坐标x2,y2 pics可以是单图名称也可以用 | 符号连接的多个图片名 找多图时按照下标顺序查找找到后返回True不再继续查找 默认附件路径在函数locationPic()中设置 separation | picArr split(pics, separation) cntSS countSubStr(pics, separation) If cntSS 0 Then findMultiPics locationPic(x1, y1, x2, y2, pics) Else picCnt 0 While picCnt cntSS findMultiPics locationPic(x1, y1, x2, y2, picArr(picCnt)) If findMultiPics True Then Goto ExitWhile End If picCnt picCnt 1 Wend Rem ExitWhile End If End Function Function loopFindMultiPics(x1,y1,x2,y2,pics) 增强版找多图,找不到目标图片就一直找直到找到为止 返回值类型为boolean,true-1,false0 查找区域为左上坐标x1,y1到右下坐标x2,y2 pics可以是单图名称也可以用 | 符号连接的多个图片名 找多图时按照下标顺序查找找到后返回True不再继续查找 默认附件路径在函数locationPic()中设置 separation | picArr split(pics, separation) cntSS countSubStr(pics, separation) Do If cntSS 0 Then loopFindMultiPics locationPic(x1, y1, x2, y2, pics) If loopFindMultiPics True Then Exit Do End If Else picCnt0 While picCnt cntSS loopFindMultiPics locationPic(x1, y1, x2, y2, picArr(picCnt)) If loopFindMultiPics True Then Exit Do End If picCnt picCnt 1 Wend End If Loop End Function 想要返回偏移坐标可以调用tempX和tempYlog: 这里是2023年6月9日,更新一下代码,前面写的代码虽然能用,但是太ugly,已经转行做程序员半年了,所以尝试更新一下,嘿嘿Option Explicit Function count_sub_str(str1, str2) 计算字符串 str2 在字符串 str1 中的出现次数并将结果存储在 cnt 变量中 Dim cnt Dim start_pos cnt 0 start_pos 1 Do While InStr(start_pos, str1, str2, vbTextCompare) 0 cnt cnt 1 start_pos InStr(start_pos, str1, str2, vbTextCompare) 1 Loop count_sub_str cnt End Function Function locate_pic(pos_x1, pos_y1, pos_x2, pos_y2, pic_name, ByRef found_x, ByRef found_y) 查找图片找到后鼠标移动到坐标 查找区域为左上坐标 pos_x1, pos_y1 到右下坐标 pos_x2, pos_y2 返回 Boolean True 和 found_x、found_y 的值True-1False0 默认附件路径 Dim ret ret False FindPic pos_x1, pos_y1, pos_x2, pos_y2, Attachment: pic_name .bmp, 0.8, found_x, found_y If found_x 0 And found_y 0 Then MoveTo found_x, found_y ret True Else ret False End If locate_pic ret End Function Function find_and_move_to_pic(pos_x1, pos_y1, pos_x2, pos_y2, pic_names, ByRef found_x, ByRef found_y) 在指定区域查找指定图片找到后移动鼠标至图片位置 pic_names 可以是单个图片名称也可以用 | 符号连接的多个图片名 默认附件路径在函数 location_pic() 中设置 Dim found Dim separation Dim pic_arr found False 定义返回值 separation | pic_arr Split(pic_names, separation) Dim cnt_ss cnt_ss count_sub_str(pic_names, separation) Do If cnt_ss 0 Then found locate_pic(pos_x1, pos_y1, pos_x2, pos_y2, pic_names, found_x, found_y) If found True Then MsgBox 找到目标图片 使用 found_x 和 found_y 进行操作 Exit Do End If Else Dim pic_cnt pic_cnt 0 While pic_cnt cnt_ss found locate_pic(pos_x1, pos_y1, pos_x2, pos_y2, pic_arr(pic_cnt), found_x, found_y) If found True Then MsgBox 找到目标图片 使用 found_x 和 found_y 进行操作 Exit Do End If pic_cnt pic_cnt 1 Wend End If Loop If found False Then MsgBox 未找到目标图片 End If find_and_move_to_pic found End Function Function loop_find_and_move_to_pic(pos_x1, pos_y1, pos_x2, pos_y2, pic_names) 在指定区域查找多张指定图片找到后移动鼠标至图片位置 pic_names 可以是单个图片名称也可以用 | 符号连接的多个图片名 默认附件路径在函数 location_pic() 中设置 Dim found Dim found_x, found_y Dim looptimes Dim MAX_TIMES found False looptimes 0 MAX_TIMES 1000 手动指定一下最大循环次数,防止死循环 Do found find_and_move_to_pic(pos_x1, pos_y1, pos_x2, pos_y2, pic_names, found_x, found_y) If found True And looptimes MAX_TIMES Then MsgBox 找到目标图片 使用 found_x 和 found_y 进行操作 Exit Do Else looptimes looptimes 1 End If Loop If found False Then MsgBox 未找到目标图片 End If loop_find_and_move_to_pic found End Function今天是26.08.13AI真是太好用了终于可以满足我奇奇怪怪的探索欲了Option Explicit 1. 业务逻辑调用区 (主程序) Dim _g_full_screen, _g_center_area _g_full_screen Array(0, 0, 1920, 1080) _g_center_area Array(400, 300, 1200, 800) Dim _finder, _x, _y Set _finder New pic_finder 查找附件中的 start 或 confirm 图片 (不需要加 .bmp 后缀) If _finder.set_region(_g_full_screen).set_options(0.8, 10, 200).find(start|confirm, _x, _y) Then TracePrint 找到目标坐标为: _x , _y Else TracePrint 未找到目标图片 End If Set _finder Nothing 2. 类定义区 (pic_finder) Class pic_finder Private _M_x1, _M_y1, _M_x2, _M_y2 Private _M_sim, _M_max_retry, _M_delay_ms Private Sub Class_Initialize() Me.reset() End Sub Private Sub Class_Terminate() RAII teardown End Sub Public Function reset() _M_x1 0 : _M_y1 0 _M_x2 1920 : _M_y2 1080 _M_sim 0.8 : _M_max_retry 10 : _M_delay_ms 200 Set reset Me End Function Public Function set_region(_bounds) _M_x1 _bounds(0) : _M_y1 _bounds(1) _M_x2 _bounds(2) : _M_y2 _bounds(3) Set set_region Me End Function Public Function set_options(_sim, _retry, _delay) _M_sim _sim : _M_max_retry _retry : _M_delay_ms _delay Set set_options Me End Function Private Function _M_locate_impl(_name, ByRef _out_x, ByRef _out_y) _out_x -1 : _out_y -1 按键精灵 9 标准找图接口 FindPic _M_x1, _M_y1, _M_x2, _M_y2, Attachment:\ _name .bmp, _M_sim, _out_x, _out_y If _out_x 0 And _out_y 0 Then MoveTo _out_x, _out_y _M_locate_impl True Else _M_locate_impl False End If End Function Public Function find(_names, ByRef _out_x, ByRef _out_y) Dim _vec, _item, _iter _vec Split(_names, |) find False For _iter 1 To _M_max_retry For Each _item In _vec If _M_locate_impl(Trim(_item), _out_x, _out_y) Then find True Exit Function End If Next Delay _M_delay_ms Next End Function End Class
返回列表