/*  
 * 窗口探测器
 * 作者: lujjjh  
 * 备注: http://www.ecranesoft.com/bbs/showtopic-8494.aspx
 * -----------------------------
 * 修改记录
 * crane 2010/08/14
 * -----------------------------
 * $Id: winspy.aau 1126 2010-11-28 06:49:50Z crane.he $
*/

import win;
import winex;
import win.ui;
import win.ole.image;
import win.cur;//鼠标指针函数库
import mouse;
/*DSG{{*/
var winform = win.form(parent=...; right=409;text="winspy for AAuto";bottom=327 )
winform.add( 
txtHwnd={ dr=1;dl=1;bottom=98;dt=1;right=207;readonly=1;multiline=1;top=77;text="";transparent=1;left=101;z=17;edge=1;cls="edit" };
lbClass={ dr=1;dl=1;bottom=70;align="right";right=263;left=214;dt=1;top=55;transparent=1;text="类名:";z=10;cls="static" };
txtPid={ dr=1;dl=1;bottom=122;dt=1;right=377;readonly=1;multiline=1;top=101;text="";transparent=1;left=271;z=22;edge=1;cls="edit" };
lbStyleEx={ dr=1;dl=1;bottom=143;align="right";right=92;left=30;dt=1;top=128;transparent=1;text="扩展样式:";z=9;cls="static" };
txtText={ dr=1;dl=1;bottom=172;dt=1;right=376;readonly=1;multiline=1;top=149;text="";transparent=1;left=100;z=16;edge=1;cls="edit" };
chkForeground={ dr=1;dl=1;bottom=39;right=275;left=153;dt=1;top=24;z=26;text="自动捕获前台窗口";cls="checkbox" };
button5={ dr=1;dl=1;bottom=273;right=366;left=238;dt=1;top=249;font=LOGFONT( name="SimSun";h=-12 );z=5;text="窗口任务栏闪烁";cls="button" };
staticTip={ dr=1;dl=1;bottom=25;right=310;left=53;top=6;z=24;text="← 请用鼠标拖动左侧的图标放置于目标窗口上";transparent=1;cls="static" };
button6={ dr=1;dl=1;bottom=305;right=366;left=46;dt=1;top=281;font=LOGFONT( name="SimSun";h=-12 );z=6;text="单击按钮、反选选择框、选择单选框";cls="button" };
txtId={ dr=1;dl=1;bottom=98;dt=1;right=377;readonly=1;multiline=1;top=77;text="";transparent=1;left=271;z=21;edge=1;cls="edit" };
button={ dr=1;dl=1;bottom=204;right=296;left=161;dt=1;top=177;z=28;text="抓取密码( CALL )";cls="button" };
chkTopMost={ dr=1;dl=1;bottom=39;right=149;left=51;dt=1;top=24;z=25;text="保持窗口最前";cls="checkbox" };
button4={ dr=1;dl=1;bottom=273;right=230;left=142;dt=1;top=249;font=LOGFONT( name="SimSun";h=-12 );z=4;text="最小化窗口";cls="button" };
lbHwnd={ dr=1;dl=1;bottom=91;align="right";right=92;left=35;dt=1;top=79;transparent=1;text="窗口句柄:";z=7;cls="static" };
button2={ dr=1;dl=1;bottom=241;right=366;left=190;dt=1;top=217;font=LOGFONT( name="SimSun";h=-12 );z=2;text="DestroyWindow 关闭窗口";cls="button" };
button3={ dr=1;dl=1;bottom=273;right=134;left=46;dt=1;top=249;font=LOGFONT( name="SimSun";h=-12 );z=3;text="修改标题";cls="button" };
txtStyleEx={ dr=1;dl=1;bottom=144;dt=1;right=207;readonly=1;multiline=1;top=123;text="";transparent=1;left=101;z=19;edge=1;cls="edit" };
pic1={ dr=1;dl=1;bottom=39;right=41;left=9;dt=1;top=7;font=LOGFONT( name="SimSun";h=-12 );text="";transparent=1;notify=1;image=$"\res\1.gif";z=30;cls="picturebox" };
lbStyle={ dr=1;dl=1;bottom=123;align="right";right=92;left=32;dt=1;top=103;transparent=1;text="基本样式:";z=8;cls="static" };
txtStyle={ dr=1;dl=1;bottom=121;dt=1;right=207;readonly=1;multiline=1;top=100;text="";transparent=1;left=101;z=18;edge=1;cls="edit" };
lbParenHwnd={ dr=1;dl=1;bottom=70;align="right";right=92;left=24;dt=1;top=55;transparent=1;text="父窗口句柄:";z=12;cls="static" };
lbText={ dr=1;dl=1;bottom=166;align="right";right=91;left=42;dt=1;top=151;transparent=1;text="文本:";z=13;cls="static" };
txtClass={ dr=1;dl=1;bottom=75;dt=1;right=377;readonly=1;multiline=1;top=54;text="";transparent=1;left=271;z=20;edge=1;cls="edit" };
lbId={ dr=1;dl=1;bottom=95;align="right";right=262;left=213;dt=1;top=80;transparent=1;text="命令ID:";z=11;cls="static" };
lbPid={ dr=1;dl=1;bottom=121;align="right";right=262;left=213;dt=1;top=106;transparent=1;text="进程ID:";z=15;cls="static" };
txtParenHwnd={ dr=1;dl=1;notify=1;color=16711680;right=207;left=101;dt=1;cls="static";readonly=1;bottom=75;text="";multiline=1;top=54;font=LOGFONT( underline=1;name="Microsoft Sans Serif";out=3;family=34;clip=2;quality=1 );transparent=1;edge=1;z=29 };
txtTid={ dr=1;dl=1;bottom=146;dt=1;right=376;readonly=1;multiline=1;top=125;text="";transparent=1;left=270;z=23;edge=1;cls="edit" };
buttonPop={ dr=1;dl=1;bgcolor=14215660;color=0;right=348;left=321;dt=1;flat=1;cls="button";tabstop=1;bottom=200;align="";text="5";top=178;font=LOGFONT( name="Webdings";charset=2;out=3;family=18;clip=2;quality=1 );acceptfiles=1;transparent=1;z=27 };
lbTid={ dr=1;dl=1;bottom=143;align="right";right=262;left=213;dt=1;top=128;transparent=1;text="线程ID:";z=14;cls="static" };
buttonClose={ dr=1;dl=1;bottom=241;right=182;left=46;dt=1;top=217;font=LOGFONT( name="SimSun";h=-12 );z=1;text="发送消息关闭窗口";cls="button" }
)
/*}}*/

winform.buttonPop.oncommand = function(id,event){
 
	var rc = ::RECT();
	
	if( winform.buttonPop.text != "6"){  
		::GetWindowRect( winform.hwnd,rc );
		::MoveWindow(winform.hwnd,rc.left,rc.top,rc.right - rc.left,rc.bottom - rc.top -110,1); 
		winform.buttonPop.text = "6" 
		winform.staticTip.top = 5;
		winform.staticTip.bottom = 50;
	}
	else{   
		::GetWindowRect( winform.hwnd,rc );
		::MoveWindow(winform.hwnd,rc.left,rc.top,rc.right - rc.left,rc.bottom - rc.top +110,1); 
		winform.buttonPop.text = "5"
	}
 	
	
}//endproc


 


//置顶窗口
winform.chkTopMost.oncommand = function(id,event){
	win.setTopmost(winform.hwnd,winform.chkTopMost.checked)  
}//endproc
 
 
//绘图API
CreatePen			= Gdi32.api("CreatePen", "pointer(int nStyle, int nWidth, int nColor)");
Rectangle			= Gdi32.api("Rectangle", "int(pointer hDC, int nLeft, int nTop, int nRight, int nBottom)");
SetROP2				= Gdi32.api("SetROP2", "int(pointer hDC, int nMode)"); 

//加载自定义的鼠标指针
var hCursor = win.cur.loadfile("\res\spy.cur") ;
 
//主窗体回调函数
winform.pic1.wndproc = function (hwndSpy, uMsg, wParam, lParam){
	select (uMsg){
		case 0x201/*_WM_LBUTTONDOWN*/{
			winform.pic1.image = $"\res\2.gif";
			
			winform.pic1.capture = true; //捕获全局鼠标消息
			win.cur.beginCur();//持续设定鼠标指针
		}
		case 0x202/*_WM_LBUTTONUP*/{ 
			winform.pic1.image = $"\res\1.gif";
			
			winform.pic1.capture = false; //释放鼠标
			win.cur.endCur(); //恢复正常鼠标指针
		} 
	}
}

//显示窗口信息
function showWindowInfo(hwnd){
	winform.txtParenHwnd.text = string.format("%X",win.getParent(hwnd) )
	winform.txtHwnd.text = string.format("%X",hwnd )
	winform.txtClass.text = win.getClass(hwnd) 
	winform.txtId.text = win.getId(hwnd);
	
	var str = win.getText(hwnd,50) ;
	if(str){
		winform.txtText.text = string.replace(str,"[\r\n]","")
	} 
	
	winform.txtStyle.text = tostring(::GetWindowLong(hwnd, 0xFFFFFFF0/*_GWL_STYLE*/) );
	winform.txtStyleEx.text = tostring(::GetWindowLong(hwnd, 0xFFFFFFEC/*_GWL_EXSTYLE*/) );
	
	winform.txtTid.text,winform.txtPid.text = win.getThreadProcessId(hwnd)
}

//点击父窗口句柄时显示父窗口信息
winform.txtParenHwnd.oncommand = function(id,event){
    var hwnd = tonumber(  winform.txtParenHwnd.text,16)
    if(hwnd)
		showWindowInfo(hwnd);
	
}//endproc

import process
winform.button.oncommand = function(id,event){

	winform.button.disabled = true;
	
	var hwnd = tonumber(  winform.txtHwnd.text,16)
	var tid,pid = win.getThreadProcessId(hwnd)
	var prcs = process( pid )
	var getWindowText_call = prcs.remoteApi("int(int hwnd,string& lpString,int cch)" ,"User32.dll","GetWindowTextA" )
	nil,winform.txtText.text = getWindowText_call(hwnd,50,50);
	prcs.free();
	
	winform.button.disabled = false;
	
}//endproc


var hwndSpy;// 用来保存探测到的窗口句柄
tmid = winform.addtimer(
	600/*毫秒*/, 
	function(hwnd,msg,id,tick){
		// 定时执行代码
		// 如果鼠标指针是探测状态
		if ( win.cur.beginning ){

			hwndSpy = winex.fromPointReal( mouse.getPos() );
			var lpRect = ::RECT();
			re, lpRect = ::GetWindowRect(hwndSpy, lpRect);
			
			showWindowInfo(hwndSpy);
			
			if (hwndSpy){
				var hDC = ::GetWindowDC(0);
				/** 防止溢出 Start **/
				if (lpRect.left < 0)
					lpRect.left = 0;
				if (lpRect.top < 0)
					lpRect.top = 0;
				if (lpRect.right > ::GetSystemMetrics(0x0/*_SM_CXSCREEN*/))
					lpRect.right = ::GetSystemMetrics(0x0/*_SM_CXSCREEN*/);
				if (lpRect.bottom > ::GetSystemMetrics(0x1/*_SM_CYSCREEN*/))
					lpRect.bottom = ::GetSystemMetrics(0x1/*_SM_CYSCREEN*/);
				/** 防止溢出 End **/
				var hPen = CreatePen(0x0/*_PS_SOLID*/, 3, 0);
				var hOldPen = ::SelectObject(hDC, hPen);
				var oldROP2 = SetROP2(hDC, 0xA/*_R2_NOTXORPEN*/);
				Rectangle(hDC, lpRect.left, lpRect.top, lpRect.right, lpRect.bottom);
				sleep(300);
				Rectangle(hDC, lpRect.left, lpRect.top, lpRect.right, lpRect.bottom);
				SetROP2(hDC, oldROP2);
				::SelectObject(hDC, hOldPen);
				::DeleteObject(hPen);
				::ReleaseDC(hwndSpy, hDC);
			}
		}
		elseif( winform.chkForeground.checked ){
			showWindowInfo( win.getForeground() );
		}
	}
);



winform.buttonClose.oncommand = function(id,event){
	if (win.msgboxTest("是否用 SendMessage 方式？否则用 PostMessage", "标题"))
		::SendMessageInt(hwndSpy, 0x10/*_WM_CLOSE*/, 0, 0);
	else
		::PostMessage(hwndSpy, 0x12/*_WM_QUIT*/, 0, 0);
	
}//endproc

winform.button2.oncommand = function(id, event){
	User32.api("DestroyWindow", "int(int hwndSpy)")(hwndSpy);
}

winform.button3.oncommand = function(id, event){
	if (win.msgboxTest("是否用 SendMessage 方式？否则用 SetWindowText", "标题"))
		::SendMessage (hwndSpy, 0xC/*_WM_SETTEXT*/,  , "我是新的标题");
	else
		win.setText(hwndSpy, "我是新的标题");
}

winform.button4.oncommand = function(id, event){
	User32.api("CloseWindow", "int(int hwndSpy)")(hwndSpy);
}

winform.button5.oncommand = function(id, event){
	User32.api("FlashWindow", "int(int hwndSpy, int bInvert)")(
		hwndSpy, win.msgboxTest("是否仅闪烁一次？否则持续闪烁") ? 1 : 0);
}

winform.button6.oncommand = function(id, event){
	::SendMessageInt(hwndSpy, 0xf5/*_BM_CLICK*/, 0, 0);
}


winform.show(true);
win.loopMessage(); 
return winform;
