Q1. 我怎样才能DEBUG我的脚本?
A1. 这个问题有无数个答案,不过最有效的还是从SciTE4AutoIt3开始,大多数人都使用这个软件来编写脚本。在debug方面SciTE有下面几条优势:
Syntax会即时高亮不符合语法的语句,这会让用户更容易发现脚本里的错误
内建在工具菜单里的Syntax可以一次检测脚本里的全部错误
内置的代码清理程序可以让代码变得更整齐、更具有可读性,它同时也能修正错误拼写的函数和变量
A2. 你也能通过添加下面的代码来在任何一台电脑上debug你的脚本:
Func dbg($msg)
DllCall("kernel32.dll", "none", "OutputDebugString", "str", $msg)
EndFunc
然后,你可以在需要debug的地方加上下面的代码:
dbg("The of Variable 1 at this time is " & $var1
这个方法对用户来说更加透明,同时也只对DebugView from SysInternals之类的程序可见。这个方法在那些没有安装SciTE的机器上更具有优势。
Q2.我怎样才能打开那些非exe格式的文件[.txt, .msi, .pdf, .jpg 之类]? [或] 我怎样才能用默认的浏览器打开网页?
A1. 这也就是为什么我们创建ShellExecute函数.下面有一个例子:
ShellExecute("C:\autoits\test.au3", "", "", "edit", @SW_MAXIMIZE)
你也能像这样打开一个网址:
ShellExecute("http://www.autoit.com/forum")
如果文件的右键菜单里有打印选项,你就可以这样用AutoIt打印文件:
ShellExecute("C:\boot.ini", "", "", "print")
如果你希望暂停脚本直到程序结束,你可以使用ShellExecuteWait函数,它们的运行参数是相同的.
Q3. 我怎样才能让脚本只运行一个进程?
A1. 你可以使用_Singleton函数来阻止脚本的副本运行,下面有一个实例:
#i nclude <Misc.au3>
_Singleton("TheNameOfMy")
这样如果脚本检测到自己已经启动就会立即退出,如果你只是想简单地知道脚本是否已经运行,你可以使用下面的代码:
#i nclude <Misc.au3>
If _Singleton("MyName", 1) Then
; We know the is already running. Let the user know.
MsgBox(0, " Name", "This is already running. Using multiple copies of this at the same breaks the [(UltimaCoder)] License!")
Exit
Endif
Q4. 我怎样才能让脚本作为系统服务启动?
这也是一个有多个答案的问题
A1.如果你只想在自己的电脑上安装服务,最简单的方法是使用Pirmasoft RunAsSvc.这个程序可以方便地添加/删除系统服务.
A2.如果你想让服务能在任何电脑上都能安装,你可以使用SRVANY.EXE和ServiceControl.au3,像这样安装服务:
#i nclude "ServiceControl.au3"
$servicename = "MyServiceName"
_CreateService("", $servicename, "My AutoIt ", "C:\Path_to_srvany.exe", "LocalSystem", "", 0x110)
RegWrite("HKLM\SYSTEM\CurrentControlSet\Services\" & $servicename & "\Parameters", "Application", "REG_SZ", @FullPath)
或者使用下面的代码删除服务: #i nclude "ServiceControl.au3"
$servicename = "MyServiceName"
_DeleteService("", $servicename)
Q5. 我怎样启动/停止服务?
A1.有两个函数集能帮助你控制服务:
SumTingWong制作的ServiceControl.au3 ,包含的函数有:
_StartService()
_StopService()
_ServiceExists()
_ServiceRunning()
_CreateService()
_DeleteService()
CatchFish制作的_NTServices.au3,包含的函数有:
_ServiceStart()
_ServiceStop()
_ServiceStatus()
_ServicePause()
Q6. 我怎样在复制文件时显示进度条?
A1.函数集ShellFileOperation.au3能完成这个操作:
Q7. 我怎样让快捷键只在自己的GUI起作用?
A1. 在更好的方法出现之前,最简单的方法是使用下面的代码:
#i nclude <GuiConstants.au3>
HotKeySet("{ENTER}", "catchguikey")
$gui = GuiCreate("Hotkey Test")
GuiCtrlCreateLabel("Press Enter", 0, 0)
GuiSetState()
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend
Func catchguikey()
Local $opt = Opt("WinTitleMatchMode", 4)
If WinGetHandle("active") = $gui Then
If @HotKeyPressed = "{ENTER}" Then
;Do something here
ToolTip("Key Pressed")
Sleep(1000)
ToolTip("")
EndIf
Else
HotKeySet(@HotKeyPressed)
Send(@HotKeyPressed)
HotKeySet(@HotKeyPressed, "catchguikey")
EndIf
Opt("WinTitleMatchMode", $opt)
EndFunc
Q8. 我怎样检测键盘是否按下了指定的键?
A1. 你可以使用_IsPressed()函数来检测按键. 你可以在帮助文件里找到这个函数:User Defined s -> Misc Management -> _IsPressed. 下面的例子会显示如何在K键按下时单击鼠标左键:
#i nclude <Misc.au3>
$pressed = 0
While 1
If _IsPressed("4B") Then
If Not $pressed Then
ToolTip("K Key being held down")
MouseDown("left")
$pressed = 1
EndIf
Else
If $pressed Then
ToolTip("")
MouseUp("left")
$pressed = 0
EndIf
EndIf
Sleep(250)
WEnd
Q9. 我怎样在远程计算机上运行脚本?
A1. 这个问题的答案由你在局域网的经验决定,如果目标系统是Windows 2000或Windows XP而且你拥有管理员权限,你就可以使用下面的两个程序:
SysInternals的PsExec
BeyondLogic的BeyondExec
这两个程序都允许在远程计算机上运行任何程序,甚至可以把你的脚本复制到目标系统上.不过Windows XP Home Edition 上不能运行这个两个程序.
Q10. 我怎样制作一个拥有可选参数的自定义函数?
A1. 你可以通过在声明函数时给参数指定一个默认值来做到. 下面是一个例子:
Func testme($param1, $param2 = "nothing", $param3 = 5)
MsgBox(0, "", "Parameter one is required. The of Parameter 1 is " & $param1 & @CRLF & "Parameter 2 is optional. The of Parameter 2 is " & $param2 & @CRLF & "Parameter 3 is optional. The of Parameter 3 is " & $param3)
EndFunc
如果调用testme()时只使用了一个参数[比如testme("test")]就会输出:
Parameter one is required. The of Parameter 1 is test
Parameter 2 is optional. The of Parameter 2 is nothing
Parameter 3 is optional. The of Parameter 3 is 5
不过,如果调用函数时使用了超过2个参数,比如testme("test", "something"), 就会输出:
Parameter one is required. The of Parameter 1 is test
Parameter 2 is optional. The of Parameter 2 is something
Parameter 3 is optional. The of Parameter 3 is 5
Q11. 我怎样让系统启动时自动运行脚本?
A1.你可以使用下面的语句来做到:
RegWrite("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "MyProgramName", "REG_SZ", @FullPath)
或者:
FileCreateShortcut(@FullPath, @StartupCommonDir & "\MyProgramName.lnk")
Q12. 我怎样让脚本删除自己?
A1. 下面的代码可以删除一个正在运行的脚本.
Func _SelfDelete($iDelay = 0)
Local $sCmdFile
FileDelete(@TempDir & "\scratch.bat")
$sCmdFile = 'ping -n ' & $iDelay & '127.0.0.1 > nul' & @CRLF _
& ':loop' & @CRLF _
& 'del "' & @FullPath & '"' & @CRLF _
& 'if exist "' & @FullPath & '" goto loop' & @CRLF _
& 'del ' & @TempDir & '\scratch.bat'
FileWrite(@TempDir & "\scratch.bat", $sCmdFile)
Run(@TempDir & "\scratch.bat", @TempDir, @SW_HIDE)
EndFunc
Q13. 我怎样在GUI里建立一个可点击的超链接?
Q14. 我怎样修改屏幕分辨率/刷新频率/颜色深度?
Q15. 在多显示器情况下我怎样得到屏幕分辨率?
A1. 下面的代码可以得到屏幕的总分辨率:
Global Const $SM_VIRTUALWIDTH = 78
Global Const $SM_VIRTUALHEIGHT = 79
$VirtualDesktopWidth = DLLCall("user32.dll", "int", "GetSystemMetrics", "int", $SM_VIRTUALWIDTH)
$VirtualDesktopWidth = $VirtualDesktopWidth[0]
$VirtualDesktopHeight = DLLCall("user32.dll", "int", "GetSystemMetrics", "int", $SM_VIRTUALHEIGHT)
$VirtualDesktopHeight = $VirtualDesktopHeight[0]
Q16. 我怎样注册一个文件类型[或者] 我怎样才能让我的程序变为一个文件类型的默认打开方式?
A1. 文件注册对那些新手来说的确有些困难. 第一件要做的事就是要让你的脚本能接受命令行调用. 下面是一段示例代码:
;$cmdline[0] is the number of parameters passed
If $cmdline[0] <> 0 Then
$filename = $cmdline[1]
; Do something with the file here
MsgBox(0, "UXYFixer", 'The file name passed to the command line is "' & $filename & '"')
Else
; We did not get any command line parameters.
; If this is a command line only program, you would want to
; alert the user that the command line parameters were incorrect.
; If this is a GUI program (like a notepad program), you would
; want to simply continue from here without opening a file.
MsgBox(0, "UXYFixer", 'Command line parameters incorrect.' & @CRLF & 'Command line usage: "' & @Name & '" "file to process"')
EndIf
然后你的脚本就能接受文件了, 你可以开始注册一个文件类型. 为了避免错误,我对此专门制作了一个UDF—— FileRegister.au3
下面的代码演示了如何用这个UDF来注册/反注册一个文件类型:
#i nclude "FileRegister.au3"
;==============================================================================================
;
; Deion: FileRegister($ext, $cmd, $verb[, $def[, $icon = ""[, $desc = ""]]])
; Registers a file type in Explorer
; Parameter(s): $ext - File Extension without period eg. "zip"
; $cmd - Program path with arguments eg. '"C:\test\testprog.exe" "%1"'
; (%1 is 1st argument, %2 is 2nd, etc.)
; $verb - Name of action to perform on file
; eg. "Open with ProgramName" or "Extract Files"
; $def - Action is the default action for this filetype
; (1 for true 0 for false)
; If the file is not already associated, this will be the default.
; $icon - Default icon for filetype including resource # if needed
; eg. "C:\test\testprog.exe,0" or "C:\test\filetype.ico"
; $desc - File Deion eg. "Zip File" or "ProgramName Document"
;
;===============================================================================================
FileRegister("uxy", '"' & @FullPath & '" "%1"', "Open in UXYFixer", 1, @FullPath & ',0', "UXYFixer Document")
;===============================================================================
;
; Deion: FileUnRegister($ext, $verb)
; UnRegisters a verb for a file type in Explorer
; Parameter(s): $ext - File Extension without period eg. "zip"
; $verb - Name of file action to remove
; eg. "Open with ProgramName" or "Extract Files"
;
;===============================================================================
FileUnRegister("uxy", "Open in UXYFixer")
Q17. 为什么点击我的下拉框(GUICtrlCreateCombo)时不出现一个下拉列表?
A1. 在使用GUICtrlCreateCombo前你先要确认height参数是你想要的下拉列表的高度,Windows XP会自动选择一个高度,但其他版本的Windows并不能这样做.
$combo = GUICtrlCreateCombo("",10,10,200,20)
应修改为:
$combo = GUICtrlCreateCombo("",10,10,200,200)
Q18. 为什么我的帖子没有人回答?
A1. 你是否对你的问题做了得体的描述? 如果你的标题或者对问题的描述含糊不清, 其他人只会忽略掉你的问题而不是回答它. 那些标题像 "帮助我", "我有了麻烦", "问题", "帮我看看代码的问题", "这段代码不能工作" 的帖子并不会引起其他人的注意. 那些有经验的用户(他们通常能解决你的问题) 经常会跳过类似的帖子. 一个规范的标题应该像这样:"使用WinWaitClose时出现的问题",或者 "陷入了死循环".
A2. 你是否贴出了你的代码? 如果你不贴出你的问题代码你就不可能得到帮助. 在发代码之前先把不相关的代码去掉.也许当然去掉不相关的代码时你已经看到问题所在的地方..也许会发现.原来是一个那样简单的问题.
A3. 请让你的文字变得易于阅读,适当的标点很重要,同时也不要给文字加上颜色.另外最好不要使用繁体字,繁体字并不能让你显得更有文化,相反而会影响他人的阅读.
另外很多人喜欢用些比较吓人的标题,比如"比如高手进来看一下"."版主进来解决一下"."高难度的问题".问题常常因为我不是高手或者版主而没有回答.或者发现"高难度"的问题原来是很菜滴.不是每个人都喜欢扮帅滴.....
还有,在提问建议多搜索一下.
Q19. 为什么杀毒软件报告我的脚本被感染?
A1. AutoIt并没有在你的系统里安装病毒,如果你的程序被报毒的话(前提是你自己不怀恶意) 那么这就是一次误报. 杀毒软件会在编译过后的AutoIt脚本里发现一些标记,并以此认为你的脚本被感染病毒.之所以会出现这样的情况有下面的两点原因:












