import win.ui;
import win.ui.menu;
import win.ole.image;
/*DSG{{*/
var winform = win.form(parent=...; text="XP壁纸自动换";right=411;bottom=207 )
winform.add( 
txtDir={ bottom=68;text="";left=44;top=45;z=6;right=296;edge=1;cls="edit" };
static={ text="秒";bottom=123;right=170;left=122;top=103;transparent=1;z=3;cls="static" };
static2={ text="自动更换壁纸时间间隔(不小于5秒)";bottom=101;right=280;left=47;top=81;transparent=1;z=4;cls="static" };
static4={ text="支持jpg、png、gif、bmp等图片格式";bottom=41;right=279;left=46;top=21;transparent=1;z=9;cls="static" };
static3={ bottom=144;color=16711680;right=348;left=81;top=126;font=LOGFONT( underline=1;h=-12;name="Microsoft Sans Serif";out=3;family=34;clip=2;quality=1 );transparent=1;notify=1;text="本程序使用 快手(AAuto) 编写,点击查看AAuto源码";z=8;cls="static" };
btnBrowser={ bottom=69;text="选择图片...";left=297;top=42;z=7;right=380;cls="button" };
txtTime={ bottom=122;align="center";text="10";left=45;top=100;z=2;right=109;edge=1;cls="edit" };
chkSlideShow={ bottom=122;text="自动定时更换壁纸";left=172;top=104;z=5;right=315;cls="checkbox" };
btnChange={ bottom=185;text="手动切换壁纸";left=203;top=152;z=1;right=327;cls="button" }
)
/*}}*/

winform.static3.oncommand = function(id,event){
	import process;
	process.execute("https://svn.ecranesoft.com/svn/aauto/Example/图形图像/壁纸自动换");
}//endproc


import fsys;
import fsys.dlg;
winform.txtDir.text = io.fullpath("/桌面/");

var timages = {};
searchImageFile = function(dir,p){   
	fsys.enum( dir, 
		p, 
		function(dir,name,path){ 
			if(name)  
				..table.push( timages,path );  
		} 
	); 
}
searchImageFolder = function(){
	timages = {}
	if(io.exist(winform.txtDir.text) ){ 
		searchImageFile (  winform.txtDir.text ,  "*.jpg"  ) 
	   	searchImageFile (  winform.txtDir.text ,  "*.png"  ) 
	   	searchImageFile (  winform.txtDir.text ,  "*.bmp"  ) 
		searchImageFile (  winform.txtDir.text ,  "*.gif"  )
	}
	
}
searchImageFolder(); //启动就扫描一次

winform.btnBrowser.oncommand = function(id,event){
	//先停下来
	winform.chkSlideShow.checked = false;
	winform.chkSlideShow.oncommand(); 
	 
	winform.txtDir.text = fsys.dlg.opendir(winform.txtDir.text,winform.hwnd); 
	
	//如果选择了无效目录，禁用所有功能
	winform.chkSlideShow.disabled = !( io.exist(winform.txtDir.text) )
	winform.btnChange.disabled= !( io.exist(winform.txtDir.text) )
	
	scanfolder();
}//endproc
  
 //随机返回壁纸路径
randimage = function(){
  return timages[ math.random(1,#timages) ]  
}
 

winform.txtTime.oncommand = function(id,event){
	winform.chkSlideShow.checked = false
	winform.chkSlideShow.oncommand();//输入时取消定时器，以方便用户切换
	
}//endproc


var timeid;
winform.chkSlideShow.oncommand = function(id,event){
	if(timeid)
		winform.killtimer(timeid)//删除原来的定时器
	
	//用户取消了自动更壁纸
	if(!winform.chkSlideShow.checked)
		return;
		
	 
	var sec = tonumber(winform.txtTime.text);	
	assert( sec,"必须输入一个大于5的数值")
	
	if(sec<5)sec=5;
	
	timeid = winform.addtimer(sec * 1000/*毫秒*/,function(hwnd,msg,id,time){//定时执行代码
		 
		slide_wallpaper( randimage() );  
	});
	
} 

winform.popmenu = win.ui.popmenu(winform);//创建弹出菜单
winform.popmenu.add('设置',function(id){ winform.show(true) }) 
winform.popmenu.add('退出',function(id){ winform.close() })

import win.reg; 
import gdip;

//用API函数修改壁纸
SystemParametersInfo = ::User32.api("SystemParametersInfoA","int(int uAction,int uParam,string lpvParam,int fuWinIni)")
	
//更换壁纸 参数( 壁纸文件路径 )
slide_wallpaper   = function(path){ 

	var p = io.splitpath(path);//拆分路径
	if(string.lower(p.ext)!=".bmp"){ //不是bmp
		var newpath = p.dir + p.name + ".bmp";//在相同目录下生成bmp文件路径
		
		if(io.exist(newpath)==false){ //如果已经生成了bmp就略过下面
			var img  =gdip.image(path) //加载原图片，创建图片对象
			img.save(newpath) //保存为bmp格式
			img.dispose(); //释放图像
		}
		
		path = newpath; //更新路径
	}
	
	//在注册表中修改壁纸
	//reg = win.reg("HKEY_CURRENT_USER\Control Panel\Desktop")
	//bmp = reg.queryValue("WallPaper")   
	//reg.setSzValue("WallPaper",path)
	//reg.close();
	
	//用API函数修改壁纸
 	SystemParametersInfo(0x14/*_SPI_SETDESKWALLPAPER*/,0, path, 1 | 2 /*_SPIF_UPDATEINIFILE | _SPIF_SENDWININICHANGE*/);
}

 
winform.btnChange.oncommand = function(id,event){
	
	//随机生成壁纸路径 
	slide_wallpaper( randimage() ); 
 
}//endproc


import win.util.tray;//导入托盘图标支持
winform.wndproc = function(hwnd,message,wparam,lparam){

	select(message) {//判断消息类型
		case( 0x400+9981/*_WM_TRAYMESSAGE*/ ) { //托盘图标消息
		
			if( lparam = 0x205/*_WM_RBUTTONUP*/ ){
				import mouse;
				x,y = mouse.getPos();
				winform.popmenu.popup( x,y,true ) //弹出菜单
			}
			else if( lparam = 0x201/*_WM_LBUTTONDOWN*/ ){
				winform.btnChange.oncommand();
			}
		} 
		case( 0x112/*_WM_SYSCOMMAND*/ ){ //系统命令消息
			if( wparam == 0xF020/*_SC_MINIMIZE*/ ){ //用户点击了最小化按钮  
				tray = win.util.tray(winform) //创建托盘图标 
				tray.message = 0x400+9981;
				tray.pop("右键点击这里可以弹出菜单,
左键点击这里更换壁纸。","AAuto壁纸自动换" )
				winform.show(false); //隐藏窗口
				return true;//阻击默认消息传递，取消最小化过程
			} 
		}
	}
	
	//无返回值则继续调用默认回调函数
}//endproc

//启动就更换一次壁纸
winform.btnChange.oncommand()

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

//删除托盘图标
if(tray)
tray.delete()

return winform;
