;用户自定义函数Func.....EndFunc应用
这是一个带水波效果另加运行一个“图像专家”小程序的窗口,
他共嵌入了三个文件:
一张名为“modern-header.bmP”的图片
一个水波效果的"waterctrl.dll"文件
还有一个小程序"图像专家.exe"
为了更好的说明我们在系统盘建立"modern"目录,把上面三个文件是放到这里运行。
代码如下:
#include <GUIConstantsEx.au3>
DirCreate(@HomeDrive & "\modern") ;系统盘建立"modern"目录
$Dir = @HomeDrive & "\modern\" ;"modern"目录路径
FileInstall("waterctrl.dll", $Dir & "\")
FileInstall("modern-header.bmP", $Dir & "\")
FileInstall("图像专家.exe", $Dir & "\") ;嵌入三个文件
$dll=DllOpen($Dir&"\waterctrl.dll")
$pic=DllCall("user32.dll","handle","LoadImageW","handle",0,"wstr",$Dir&"\modern-header.bmp","uint",0,"int",500,"int",54,"uint",0x2010)
$handle=GUICreate("AutoIt Script By Duvet",500,254,-1,-1) ;窗口
$Button1 = GUICtrlCreateButton("运行", 20, 100, 75, 22, 0)
$Button2 = GUICtrlCreateButton("退出", 200, 100, 75, 22, 0)
GUISetState(@SW_SHOW)
DllCall($dll,"bool","enablewater","HWND",$handle,"int",0,"int",0,"HWND",$pic[0],"int",3,"int",50)
DllCall($dll,"bool","setwaterparent","HWND",$handle)
DllCall($dll,"bool","waterblob","int",249,"int",27,"int",10,"int",500) ;加载效果
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Select
Case $nMsg = $GUI_EVENT_CLOSE ;按窗口X
Exit ;退出程序
Case $nMsg = $Button2 ;按退出按钮
Exit ;退出程序
Case $nMsg = $Button1 ;按运行按钮
RunWait($Dir & "\图像专家.exe") ;运行"图像专家.exe"
Exit ;关闭"图像专家.exe"时并退出主程序
EndSelect
WEnd
运行以上代码,不管是按窗口X退出程序还是按退出按钮以及运行"图像专家.exe"时并退出主程序,在系统盘下都残留"modern"
文件夹,并且里面还有我们刚刚释放的三个文件,为什么呢?原来是我们退出时没有删除以上文件。
对于dll文件的加载(如皮肤DLL等)必须先关闭DLL加载,才能删除掉dll文件,好!我们再改写一下。
While 1
$nMsg = GUIGetMsg()
Select
Case $nMsg = $GUI_EVENT_CLOSE ;按窗口X
Exit ;退出程序
Case $nMsg = $Button2 ;按退出按钮
DllCall($dll,"bool","flattenwater")
DllCall($dll,"bool","disablewater")
DllClose($dll)
GUIDelete() ;关闭DLL
FileDelete($Dir&"\waterctrl.dll")
FileDelete($Dir&"\modern-header.bmP")
FileDelete($Dir&"\图像专家.exe") ;删除三个文件,当然你可以直接删除目录
DirRemove(@HomeDrive & "\modern",1) ;删除目录
Exit ;退出程序
Case $nMsg = $Button1 ;按运行按钮
RunWait($Dir & "\图像专家.exe") ;运行"图像专家.exe"
Exit ;关闭"图像专家.exe"时并退出主程序
EndSelect
WEnd
改写之后我们点击按退出按钮时残留文件都被删除了,但新的问题又来了,当按窗口X退出程序时"modern"又没删除掉,
再看一下源码知道,点击按窗口X时是直接运行"Exit"(退出),并没有执行删除,那么是不是在"Case $nMsg = $GUI_EVENT_CLOSE"
下也要写以上删除命令呢?回答是肯定的,不仅这里要写而且在运行"图像专家.exe"后也要写,哈哈!是不是太麻烦了,
有没有简单的方法呢?这就是下面我们谈到的"用户自定义函数"。
Func ;自定义函数开始
.
.
.
EndFunc;自定义函数结束
首先要给自定义函数取个名字,我们就叫他"_del()"吧!把上面的删除命令放到自定义函数中,
Func _del()
DllCall($dll,"bool","flattenwater")
DllCall($dll,"bool","disablewater")
DllClose($dll)
GUIDelete() ;关闭DLL
FileDelete($Dir&"\waterctrl.dll")
FileDelete($Dir&"\modern-header.bmP")
FileDelete($Dir&"\图像专家.exe") ;删除三个文件,当然你可以直接删除目录
DirRemove(@HomeDrive & "\modern",1) ;删除目录
Exit ;退出程序
EndFunc
自定义函数写好了,怎样才能让他运行呢?别急!哪里需要就放哪里。
While 1
$nMsg = GUIGetMsg()
Select
Case $nMsg = $GUI_EVENT_CLOSE ;按窗口X
_del() ;运行自定义函数
Case $nMsg = $Button2 ;按退出按钮
_del() ;运行自定义函数
Case $nMsg = $Button1 ;按运行按钮
RunWait($Dir & "\图像专家.exe") ;运行"图像专家.exe"
_del() ;运行自定义函数
EndSelect
WEnd
Func _del()
DllCall($dll,"bool","flattenwater")
DllCall($dll,"bool","disablewater")
DllClose($dll)
GUIDelete() ;关闭DLL
FileDelete($Dir&"\waterctrl.dll")
FileDelete($Dir&"\modern-header.bmP")
FileDelete($Dir&"\图像专家.exe") ;删除三个文件,当然你可以直接删除目录
DirRemove(@HomeDrive & "\modern",1) ;删除目录
Exit ;退出程序
EndFunc
自定义函数可以简化程序编写,在一个程序中可以使用多个自定义函数,可以减少程序编写的繁琐性。
|