# aardio 范例: 找图代码生成器



```aardio
//找图代码生成器
import win.ui;
/*DSG{{*/
var winform = win.form(text="找图代码生成器";right=454;bottom=403)
winform.add(
btnSnap={cls="button";text="选择要查找的目标图像";left=146;top=324;right=450;bottom=400;color=14120960;anchors={left=1;right=1;bottom=1};font=LOGFONT(h=-14);note="在屏幕上框选要查找的目标图像，
目标图像越小，特征越明显，找图效果越好";z=3};
btnTest={cls="button";text="运行测试";left=325;top=7;right=418;bottom=34;anchors={top=1;right=1};z=5};
editCode={cls="edit";left=5;top=40;right=443;bottom=319;anchors={left=1;top=1;right=1;bottom=1};edge=1;hscroll=1;multiline=1;vscroll=1;z=2};
plusImage={cls="plus";left=7;top=337;right=106;bottom=385;anchors={left=1;bottom=1};repeat="scale";z=1};
static3={cls="static";text="自动找图代码：";left=8;top=14;right=244;bottom=37;anchors={left=1;top=1};transparent=1;z=4}
)
/*}}*/

//屏幕找图范例: https://www.aardio.com/zh-cn/docs/examples/Automation/ComputerVision/findImage.example.html

import mouse;
import soImage; 
import winex;
winform.btnSnap.oncommand = function(id,event){
 
	import gdip.snap;
	import mouse.screenArea;
	var screenArea = mouse.screenArea();
	screenArea.onSelectionChanged = function(rc){
		//尽量截取特征明显的图
		var bmp = gdip.snap(screenArea.hwnd,rc);
		var buf = bmp.saveToBuffer("*.jpg",80);
		winform.plusImage.background = buf;
		var strJpg = ..string.escape(buf);
		 
		owner.close();  
		
		var x,y = mouse.getPos();  
		winex.waitClose(screenArea.hwnd); 
		
		var hwndTarget = winex.fromPointReal( x,y,0x0004/*_CWP_SKIPTRANSPARENT*/ | 0x0001/*_CWP_SKIPINVISIBLE*/  );
  		var hwndRoot = win.getRoot(hwndTarget);
  		var pClass,pText = winform.validPattern(win.getClass(hwndTarget)) ,winform.validPattern(winex.getText(hwndTarget,50));
  		
  		var hwndPattern = string.format('var hwndParent = winex.find("%s","%s");',pClass,pText);
  		if( hwndRoot != hwndTarget ){ 
  			var pClass2,pText2 = winform.validPattern(win.getClass(hwndRoot)) ,winform.validPattern(winex.getText(hwndRoot,50));
  			hwndPattern = string.format('var hwndParent = winex.find("%s","%s");\r\nvar hwnd = winex.findEx(hwndParent,,"%s","%s");',pClass2,pText2,pClass,pText); 
  		}
  
		import string.template;
		var strCode = string.template();
		strCode.template = /***
//创建要查找的目标图像
import soImage;
var imgFind = soImage();
imgFind.setBytes('${jpg}');

//查找窗口
import winex;
${hwnd}

//窗口截图
var imgWindow = soImage();
imgWindow.captureWindow(hwnd);
 
//查找目标图像
var sm,x,y = imgFind.findImage( imgWindow );
 
//移动鼠标
import mouse; 
mouse.moveToWindow(0,0,hwnd);
mouse.moveToWindow(x,y,hwnd,8);
		***/
		 
		winform.editCode.text =  strCode.format(
			hwnd = hwndPattern;
			jpg = strJpg; 
		); 
	}
	screenArea.doModal(); 
}

winform.btnTest.oncommand = function(id,event){
	code = string.trim( winform.editCode.text );
	if(!#code){
		winform.msgboxErr("请先选择目标图像");
		return;
	}
	
	loadcodex(code);
}

winform.validPattern = function(str){
	if(!#str) return "";
		
	var mbs = string.match(str,"[\s\w]*:+[\s\w]*")
	if(#mbs)
		return mbs;
		
	str = string.replace(str,"(\p)","\\\1") 	
	str = string.replace(str,"\x+",function(str){
	 	if(..string.find(str,"\d") ) return "\x+"
	 	return str;
	} ) 	
    
    str = string.replace(str,"\d+","\\d+") 	
    return str;
}


winform.show();
win.loopMessage();
```

