#cs
编写本脚本收获:
对有”教训“的要特别注意
1.学习了对ListView的相关操作,如调整ListView标题宽度要用到#Include <GuiListView.au3>,之后才能使用_GUICtrlListView_SetColumnWidth,
2.学习了Combox的相关操作;
#cs
编写本脚本收获:
对有”教训“的要特别注意
1.学习了对ListView的相关操作,如调整ListView标题宽度要用到#Include <GuiListView.au3>,之后才能使用_GUICtrlListView_SetColumnWidth,
2.学习了Combox的相关操作;
#ce
#NoTrayIcon;脚本运行时任务栏不显示图标
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>;调整ListView标题宽度要用到的,注意_GUICtrlListView_SetColumnWidth($ListView1, 2, 250)
#include <GUIListBox.au3>
#Region ### START Koda GUI section ### Form=
$MainWindow = GUICreate("启动项设置管理", 430, 400, 325, 153)
GUISetBkColor(0x00E0FFFF)
;--------$Group1是管理已有启动项
;$Group1 = GUICtrlCreateGroup("启动项项目", 7, 8, 417, 209, -1, $WS_EX_TRANSPARENT)
$Group1 = GUICtrlCreateGroup("启动项项目", 7, 8, 417, 209)
GUICtrlSetFont(-1,9,400,0,"Comic Sans MS");800为全中指字体权值(weight),值越大显示字越大;0是属性,就是字是斜体字或下划线字
GUICtrlSetColor(-1,0x808080)
$ListView1 = GUICtrlCreateListView("位置 | 名称 | 路径 ", 11, 24, 407, 161);Group1下的列表框-$ListView1
_GUICtrlListView_SetColumnWidth($ListView1, 2, 250) ;设置ListView标题宽度 0-第一竖列 1-第二竖列 以此类推[/au3]
$Status = GUICtrlCreateLabel(" ", 12, 192, 400, 17,$SS_SUNKEN);$Group1下用于显示已有启动项相关信息
GUICtrlSetColor(-1,0xff0000)
GUICtrlCreateGroup("", -99, -99, 1, 1);$Group1组结束
;--------$Group2是进行添加启动项管理
$Group2 = GUICtrlCreateGroup("添加启动项", 7, 260, 417, 110);$Group2是管理启动项添加的组
GUICtrlSetFont(-1,9,400,0,"Comic Sans MS")
GUICtrlSetColor(-1,0x808080)
$Label1 = GUICtrlCreateLabel("创建位置", 26, 285, 88, 17);$Group2里的标签控件
$Label2 = GUICtrlCreateLabel("启动项名称", 26, 317, 64, 17);$Group2里的标签控件
$Label3 = GUICtrlCreateLabel("启动项路径", 26, 349, 64, 17);$Group2里的标签控件
$Choose = GUICtrlCreateCombo("MACHINE → RUN", 102, 281, 137, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL));$Group2里的组合列表框控件,与$Label1对应
GUICtrlSetData(-1,"USER → RUN") ;设置$Combo1控件数据由默认"MACHINE → RUN"更改为"USER → RUN"
$Name = GUICtrlCreateInput("Default-默认", 102, 314, 137, 21);$Group2里的组合列表框控件,与$Label2对应
GUICtrlSetTip(-1,"请选择创建启动项注册表所在位置")
GUICtrlSetTip(-1,"输入要建立的启动项名称");当鼠标移动到指定控件给予的文字提示
$Path = GUICtrlCreateInput("点击'浏览'按钮选择你需要添加的启动文件", 102, 345, 137, 21);$Group2里的组合列表框控件,与$Label3对应
GUICtrlSetTip(-1,"请选择你要添加启动项的完整路径及文件名")
$Browse = GUICtrlCreateButton("浏览", 264, 340, 89, 25);$Group2里的按钮控件,与$Input2对应
GUICtrlCreateGroup("", -99, -99, 1, 1);$Group2组结束
;--------以下按钮是进行具体操作
$Add = GUICtrlCreateButton("添加", 8, 225, 89, 33)
GUICtrlSetTip(-1,"将你选择的文件添加到你指定的注册表的相关启动位置")
$Delete = GUICtrlCreateButton("删除", 104, 225, 97, 33)
GUICtrlSetTip(-1,"将你选择的启动项删除")
$Refresh = GUICtrlCreateButton("刷新", 208, 225, 89, 33)
GUICtrlSetTip(-1,"刷新启动项")
$Tips = GUICtrlCreateLabel("提示:组策略和开始菜单启动目录也可设置开机启动。", 7, 378, 287, 16)
GUICtrlSetState($Tips, $GUI_DISABLE)
GUISetState(@SW_SHOW)
Read();该函数设置$StartUpStatus上的状态信息
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
GUIDelete($MainWindow)
Exit
Case $Add;对应添加按钮
$Data = GUICtrlRead($Choose);$Choose变量对应Machine和User下的RUN键
Select
Case GUICtrlRead($Path) = "" Or GUICtrlRead($Path) = "点击浏览按钮选择文件..."
MsgBox (0, "提示", "请输入正确的路径及文件名后再试!")
Case $Data = "MACHINE → RUN"
RegWrite("HKLM\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", GUICtrlRead($Name), "REG_SZ", GUICtrlRead($Path))
MsgBox(0, "提示", "设置成功,重启生效!")
Case $Data = "USER → RUN"
RegWrite("HKCU\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", GUICtrlRead($Name), "REG_SZ", GUICtrlRead($Path))
MsgBox(0, "提示", "设置成功,重启生效!")
EndSelect
Case $Delete;对应删除按钮
If GUICtrlRead($ListView1) = 0 Then;没选择相关ListView项,当然不能删除
MsgBox(0, "提示", "请选择您需要删除的项目再进行操作!")
ElseIf GUICtrlRead($ListView1) <> 0 Then
$answer = MsgBox(4, "提示", "删除后将不可恢复,只可手动添加,确认删除么?")
If $answer = 7 Then ;如果选择’取消‘按钮,执行Remain()函数
Remain()
Else ;否则执行删除操作
If StringLeft(GUICtrlRead(GUICtrlRead($ListView1)),4) = "User" Then
;stringleft()中的4是从字符串左返回指定数量的字符
;用一个GUICtrlRead()并不能读出User,只能读出ListView1的行标号,用两个才能读出整行的ListView1的内容
$result = StringTrimLeft(GUICtrlRead(GUICtrlRead($ListView1)), 9)
;结合UICtrlCreateListViewItem('User |'& $key & '|' & RegRead('HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run',$key),$ListView1)
;;$result 得到的结果是这行被截除了前9个字符的ListView1内容,比如"User |”后面的字符。这里的“User |”,
;应该是上面按照9个字符的格式录入到ListView中的
$x = StringInStr ($result, "|");检查某个字符串是否含有指定的子字符串.
;$x 返回“|”字符在$result中的位置
$y = StringLeft($result, $x - 1)
;$y 返回“|”字符左边的所有字符,这个字符串,应该为了得到该键值在注册表中的名称
RegDelete("HKCU\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", $y)
GUICtrlDelete(GUICtrlRead($ListView1))
;GUICtrlRead($ListView1)读取ListView1的行标号,再用GUICtrlDelete删除它
MsgBox(0, "提示", "删除成功,重启生效!")
Else;既然不是User,那么就是MACHINE!下面是针对HKLM下的RUN进行操作了
$result = StringTrimLeft(GUICtrlRead(GUICtrlRead($ListView1)), 9)
$x = StringInStr ($result, "|")
$y = StringLeft($result, $x - 1)
RegDelete("HKLM\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", $y)
GUICtrlDelete(GUICtrlRead($ListView1))
MsgBox(0, "提示", "删除成功,重启生效!")
EndIf
EndIf
EndIf
Case $Refresh;对应刷新按钮
ReMain()
Case $Browse;对应浏览按钮
$PathValue = FileOpenDialog("请选择需要开机启动的文件...", @ScriptDir, "可执行文件(*.exe;*.bat;*.vbs)")
GUICtrlSetData($Path, $PathValue)
EndSwitch
WEnd
Func Read() ;对Machine和User下的RUN信息到ListView中
$Sum = 0;初始信息为空
$i = 1;读取第1条启动项相关信息
While 1
$key=RegEnumVal('HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run',$i)
;枚举第1个启动项程序名称,与listview中的’名称‘字段对应
;RegEnumVal ( "键名", 序数 ) :读取指定值项的名称
If @error <> 0 then ExitLoop;如枚举失败则退出while循环
GUICtrlCreateListViewItem('User |'& $key & '|' & RegRead('HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run',$key),$ListView1)
;User对应’位置‘字段,就是你将启动项建在注册表什么地方了
;$key就是注册表值项’名称‘
;RegRead('HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run',$key 读取指定值项$key的具体值,对应的就是’路径‘
;GUICtrlCreateListViewItem ( "文本", listviewID ):在GUI上创建一个 ListView 项目控件.
$i=$i+1
$Sum = $Sum + 1
WEnd
$i = 1
While 1
$key=RegEnumVal('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run',$i)
If @error <> 0 then ExitLoop
GUICtrlCreateListViewItem('Machine |'& $key & '|' & RegRead('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run',$key),$ListView1)
$i=$i+1
$Sum = $Sum + 1
WEnd
GUICtrlSetData($Status, ' 读取启动项完毕! 共' & $Sum & '个项目...')
EndFunc
Func ReMain();对应‘刷新’按钮
_GUICtrlListView_DeleteAllItems($ListView1);删除所有ListView项
Read()
EndFunc