地球仪作用:AutoIt v3 FAQ By一点笔记
来源:百度文库 编辑:偶看新闻 时间:2024/05/05 19:00:11
AutoIt v3 FAQ By一点笔记
作为论坛里的一个资深潜水员,我注意到许多被初学者反复询问 的问题都很容易找到答案。这些问题大多不会被及时回答,原因就是提问者没有事先在论坛搜索答案,因此相同的答案往往会在不同的帖子里出现很多次。或者说, 作出这些回答已经令人厌烦、生气,因为大家之前已经看到同样的问题被问了无数遍。所以我在这里贴出许多被新手反复提及的问题的答案,我会给所有的问题编号 并列出一个方便查找的索引。
Q1. 我怎样才能DEBUG我的脚本?
A1. 这个问题有无数个答案,不过最有效的还是从SciTE4AutoIt3开始,大多数人都使用这个软件来编写脚本。在debug方面SciTE有下面几条 优势:
- Syntax会即时高亮不符合语法的语句,这会让用户更容易发现脚本里的错误
- 内建在工具菜单里的Syntax可以一次检测脚本里的全部错误
- 内置的代码清理程序可以让代码变得更整齐、更具有可读性,它同时也能修 正错误拼写的函数和变量
Func dbg($msg)
DllCall("kernel32.dll", "none", "OutputDebugString", "str", $msg)
EndFunc
然后,你可以在需要debug的地方加上下面的代码:
dbg("The value of Variable 1 at this time is " $var1这个方法对用户来说更加透明,同时也只对DebugView from SysInternals之类的程序可见。这个方法在那些没有安装SciTE的机器上更具有优势。
Q2.我怎样才能打开那些非exe格式的文件[.txt, .msi, .pdf, .jpg 之类]? [或] 我怎样才能用默认的浏览器打开网页?
A1. 这也就是为什么我们创建ShellExecute函数.下面有一个例子:
你也能像这样打开一个网址:
ShellExecute("C:\autoitscripts\test.au3", "", "", "edit", @SW_MAXIMIZE)
ShellExecute("http://www.autoitscript.com/forum")如果文件的右键菜单里有打印选项,你就可以这样用AutoIt打印文件:
ShellExecute("C:\boot.ini", "", "", "print")如果你希望暂停脚本直到程序结束,你可以使用ShellExecuteWait函数,它们的运行参数是相同的.
Q3. 我怎样才能让脚本只运行一个进程?
A1. 你可以使用_Singleton函数来阻止脚本的副本运行,下面有一个实例:
#include这样如果脚本检测到自己已经启动就会立即退出,如果你只是想简单地知道脚本是否已经运行,你可以使用下面的代码:
_Singleton("TheNameOfMyScript")
#includeQ4. 我怎样才能让脚本作为系统服务启动?
If _Singleton("MyScriptName", 1) Then
; We know the script is already running. Let the user know.
MsgBox(0, "Script Name", "This script is already running. Using multiple copies of this script at the same breaks the [(UltimaCoder)] License!")
Exit
Endif
这也是一个有多个答案的问题
A1.如果你只想在自己的电脑上安装服务,最简单的方法是使用Pirmasoft RunAsSvc.这个程序可以方便地添加/删除系统服务.
A2.如果你想让服务能在任何电脑上都能安装,你可以使用SRVANY.EXE和ServiceControl.au3,像这样安 装服务:
#include "ServiceControl.au3"或者使用下面的代码删除服务:
$servicename = "MyServiceName"
_CreateService("", $servicename, "My AutoIt Script", "C:\Path_to_srvany.exe", "LocalSystem", "", 0x110)
RegWrite("HKLM\SYSTEM\CurrentControlSet\Services\" $servicename "\Parameters", "Application", "REG_SZ", @ScriptFullPath)
#include "ServiceControl.au3"Q5. 我怎样启动/停止服务?
$servicename = "MyServiceName"
_DeleteService("", $servicename)
A1.有两个函数集能帮助你控制服务:
SumTingWong制作的ServiceControl.au3 ,包含的函数有:Q6. 我怎样在复制文件时显示进度条?
CatchFish 制作的_NTServices.au3,包含的函数 有:
- _StartService()
- _StopService()
- _ServiceExists()
- _ServiceRunning()
- _CreateService()
- _DeleteService()
- _ServiceStart()
- _ServiceStop()
- _ServiceStatus()
- _ServicePause()
A1.函数集ShellFileOperation.au3能 完成这个操作:
Q7. 我怎样让快捷键只在自己的GUI起作用?
A1. 在更好的方法出现之前,最简单的方法是使用下面的代码:
#includeQ8. 我怎样检测键盘是否按下了指定的键?
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
A1. 你可以使用_IsPressed()函数来检测按键. 你可以在帮助文件里找到这个函数:User Defined Functions -> Misc Management -> _IsPressed. 下面的例子会显示如何在K键按下时单击鼠标左键:
#IncludeQ9. 我怎样在远程计算机上运行脚本?
$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
A1. 这个问题的答案由你在局域网的经验决定,如果目标系统是Windows 2000或Windows XP而且你拥有管理员权限,你就可以使用下面的两个程序:
- SysInternals的PsExec
- BeyondLogic的BeyondExec
Q10. 我怎样制作一个拥有可选参数的自定义函数?
A1. 你可以通过在声明函数时给参数指定一个默认值来做到. 下面是一个例子:
Func testme($param1, $param2 = "nothing", $param3 = 5)如果调用testme()时只使用了一个参数[比如testme("test")]就会输出:
MsgBox(0, "", "Parameter one is required. The value of Parameter 1 is " $param1 @CRLF "Parameter 2 is optional. The value of Parameter 2 is " $param2 @CRLF "Parameter 3 is optional. The value of Parameter 3 is " $param3)
EndFunc
Parameter one is required. The value of Parameter 1 is test不过,如果调用函数时使用了超过2个参数,比如testme("test", "something"), 就会输出:
Parameter 2 is optional. The value of Parameter 2 is nothing
Parameter 3 is optional. The value of Parameter 3 is 5
Parameter one is required. The value of Parameter 1 is test
Parameter 2 is optional. The value of Parameter 2 is something
Parameter 3 is optional. The value of Parameter 3 is 5
Q11. 我怎样让系统启动时自动运行脚本?
A1.你可以使用下面的语句来做到:
RegWrite("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "MyProgramName", "REG_SZ", @ScriptFullPath)或者:
FileCreateShortcut(@ScriptFullPath, @StartupCommonDir "\MyProgramName.lnk")
Q12. 我怎样让脚本删除自己?
A1. 下面的代码可以删除一个正在运行的脚本.
Func _SelfDelete($iDelay = 0)Q13. 我怎样在GUI里建立一个可点击的超链接?
Local $sCmdFile
FileDelete(@TempDir "\scratch.bat")
$sCmdFile = 'ping -n ' $iDelay '127.0.0.1 > nul' @CRLF _
':loop' @CRLF _
'del "' @ScriptFullPath '"' @CRLF _
'if exist "' @ScriptFullPath '" goto loop' @CRLF _
'del ' @TempDir '\scratch.bat'
FileWrite(@TempDir "\scratch.bat", $sCmdFile)
Run(@TempDir "\scratch.bat", @TempDir, @SW_HIDE)
EndFunc
A1. Gafrost在这方面做了很大努力,并提供了一个UDF来帮助完成这个功能.
Q14. 我怎样修改屏幕分辨率/刷新频率/颜色深度?
A1. ChangeResolution.au3可以完 成这些设置.
Q15. 在多显示器情况下我怎样得到屏幕分辨率?
A1. 下面的代码可以得到屏幕的总分辨率:
Global Const $SM_VIRTUALWIDTH = 78Q16. 我怎样注册一个文件类型[或者] 我怎样才能让我的程序变为一个文件类型的默认打开方式?
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]
A1. 文件注册对那些新手来说的确有些困难. 第一件要做的事就是要让你的脚本能接受命令行调用. 下面是一段示例代码:
;$cmdline[0] is the number of parameters passed然后你的脚本就能接受文件了, 你可以开始注册一个文件类型. 为了避免错误,我对此专门制作了一个UDF—— FileRegister.au3
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: "' @ScriptName '" "file to process"')
EndIf
下面的代码演示了如何用这个UDF来注册/反注册一个文件类型:
#include "FileRegister.au3"Q17. 为什么点击我的下拉框(GUICtrlCreateCombo)时不出现一个下拉列表?
;==============================================================================================
;
; Description: 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 Description eg. "Zip File" or "ProgramName Document"
;
;===============================================================================================
FileRegister("uxy", '"' @ScriptFullPath '" "%1"', "Open in UXYFixer", 1, @ScriptFullPath ',0', "UXYFixer Document")
;===============================================================================
;
; Description: 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")
A1. 在使用GUICtrlCreateCombo前你先要确认height参数是你想要的下拉列表的高度,Windows XP会自动选择一个高度,但其他版本的Windows并不能这样 做.
$combo = GUICtrlCreateCombo("",10,10,200,20)Q18. 为什么我的帖子没有人回答?
应修改为:
$combo = GUICtrlCreateCombo("",10,10,200,200)
A1. 你是否对你的问题做了得体的描述? 如果你的标题或者对问题的描述含糊不清, 其他人只会忽略掉你的问题而不是回答它. 那些标题像 "帮助我", "我有了麻烦", "问题", "帮我看看代码的问题", "这段代码不能工作" 的帖子并不会引起其他人的注意. 那些有经验的用户(他们通常能解决你的问题) 经常会跳过类似的帖子. 一个规范的标题应该像这样:"使用WinWaitClose时出现的问题",或者 "陷入了死循环".
A2. 你是否贴出了你的代码? 如果你不贴出你的问题代码你就不可能得到帮助. 在发代码之前先把不相关的代码去掉.也许当然去掉不相关的代码时你已经看到问题所在的地方..也许会发现.原来是一个那样简单的问题.
A3. 请让你的文字变得易于阅读,适当的标点很重要,同时也不要给文字加上颜色.另外最好不要使用繁体字,繁体 字并不能让你显得更有文化,相反而会影响他人的阅读.
另外很多人喜欢用些比较吓人的标题,比如"比如高手进来看一下"."版主进来解决一下"."高难度的问 题".问题常常因为我不是高手或者版主而没有回答.或者发现"高难度"的问题原来是很菜滴.不是每个人都喜欢扮帅滴.....
还有,在提问建议多搜索一下.
Q18. 为什么杀毒软件报告我的脚本被感染?
A1. AutoIt并没有在你的系统里安装病毒,如果你的程序被报毒的话(前提是你自己不怀恶意) 那么这就是一次误报. 杀毒软件会在编译过后的AutoIt脚本里发现一些标记,并以此认为你的脚本被感染病毒.之所以会出现这样的情况有下面的两点原因:
- AutoIt使用UPX加壳. UPX是一种开源的程序压缩软件. 经常被一些病毒使用(让它们变得更小).
- 一些怀恶 意的写脚本的人让AutoIt脚本引擎被标识为了病毒.