autoit对文件的操作

本文介绍如何使用AutoIt进行窗口操作,包括等待活动窗口、获取窗口位置和大小、屏幕截图等。同时展示了一个GUI界面的创建过程,涉及控件如标签、按钮及输入框的应用,并演示了如何利用TreeView控件读取硬盘信息。

在此模式下,一些特殊的字符串可用来代替窗口标题参数,这样我们就可以使用下面的 属性 来识别一个窗口:

TITLE - 窗口标题
CLASS - 内部窗口类名
REGEXPTITLE -窗口标题使用 正则表达式 (如果正则表达式错误,将把 @error 设置为 2)
REGEXPCLASS -窗口类名使用 正则表达式 (如果正则表达式错误,将把 @error 设置为 2)
LAST - 使用上一次成功匹配的窗口,这样就不必三番四次地指定窗口标题和窗口文本了.
ACTIVE - 当前活动窗口
X(坐标) \ Y(坐标) \ W(宽度) \ H(高度) - 窗口坐标与大小
INSTANCE - 基于1开始的接口(instance,原翻译为枚举) 由程序自动分配的唯一标识.

 

 

"& _"   ----    可以作为换行连接使用的字符

 

 


#include <ScreenCapture.au3>

Opt("MustDeclareVars", 1)

Dim $path, $name,  $year, $month, $day, $hour, $min, $sec, $msec, $x, $width, $height
Dim $search, $file, $att
$path = "E:\AutoIt\Picture\"

WinWaitActive("[TITLE:AutoIt;CLASS:CabinetWClass]")
$x = WinGetPos("[TITLE:AutoIt;CLASS:CabinetWClass]")
;$size = WinGetClientSize("[TITLE:AutoIt;CLASS:CabinetWClass]")
$width = $x[0]+$x[2]
$height = $x[1]+$x[3]
FileDelete("E:\AutoIt\winsize.ini")
FileWrite("E:\AutoIt\winsize.ini","x=" & $x[0] & @CRLF & "y=" & $x[1] & @CRLF & "width=" & $width & @CRLF & "height=" & $height)
Sleep(2000)

If FileExists($path) Then
 
 ; 捕捉全屏
 ;_ScreenCapture_Capture($path & _getPicName() & ".jpg")
 ;Sleep(1000)
 ; 捕捉区域
 _ScreenCapture_Capture($path & _getPicName() & ".jpg", $x[0], $x[1], $width, $height)
Else
 DirCreate($path)
 
 ; 捕捉全屏
 _ScreenCapture_Capture($path & _getPicName() & ".jpg")
 Sleep(1000)
 ; 捕捉区域
 _ScreenCapture_Capture($path & _getPicName() & ".jpg", $x[0], $x[1], $width, $height)

EndIf

Func _getPicName()
 $year = @YEAR
 $month = @MON
 $day = @MDAY
 $hour = @HOUR
 $min = @MIN
 $sec = @SEC
 $msec = @MSEC

 $name = $year & $month & $day & "_" & $hour & $min & $sec & $msec
 Return $name
EndFunc


; 显示当前目录中所有文件的文件名
$search = FileFindFirstFile($path & "*.*") 

; 检查搜索是否成功
If $search = -1 Then
    MsgBox(0, "错误", "没有文件/目录 匹配搜索")
    Exit
Else
 
 While 1
  $file = FileFindNextFile($search)
  If @error Then ExitLoop
  
  $att = FileGetAttrib($path & $file)
  If StringInStr($att, "D") Then
   MsgBox(0,"showme",$file,3)
  EndIf
  
  MsgBox(4096, "文件:", $file,2)
 WEnd
 
EndIf


; 关闭搜索句柄
FileClose($search)

 

 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Local $font = "Comic Sans MS", $i, $width

GUICreate("结果显示",900,600,Default,Default,Default,Default)
GUISetBkColor(0xE0FFFF)

GUISetFont(12, 400, 4, $font)
GUISetState(@SW_SHOW)
;GUICtrlCreateLabel("underlined label", 10, 20)

GUICtrlCreateTab(10,20,880,550)
GUICtrlCreateTabItem("first")
 For $i = 1 To 6
  $width = $i * 30 + 50
  GUICtrlCreateLabel("label0", 30, $width, 50, 20)
  GUICtrlCreateButton("OK0", 20, $width, 50, 20)
  GUICtrlCreateInput("default", 80, $width, 70, 20)
 Next

GUICtrlCreateTabItem("")

While 1
 Local $msg
 $msg = GUIGetMsg()
 If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

 

 

 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>


;Opt('MustDeclareVars', 1)

Local $font = "Comic Sans MS", $i, $width

$Form1 = GUICreate("结果显示", 900, 600, -1, -1);创建一个 GUI 窗口
GUISetIcon(@SystemDir & "\mspaint.exe", 0);设置窗口图标

GUISetFont(12, 400, 4, $font)

;创建一个分组(Group)控件
$CreateGroup1 = GUICtrlCreateGroup("TreeView 控件例子", 10, 70, 880, 500)
GUICtrlSetResizing($CreateGroup1, 2 + 32 + 64 + 256);设置某个控件的大小调整方式

;创建一个TreeView控件
$hTreeView = GUICtrlCreateTreeView(20, 90, 860, 440, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
GUICtrlSetResizing($hTreeView, 2 + 32 + 64 + 256);设置某个控件的大小调整方式

$menu_B = GUICtrlCreateContextMenu($hTreeView);创建上下文菜单(右键)
$menu_B1 = GUICtrlCreateMenuItem("读取方式(1)", $menu_B);创建一个菜单项目控件
$menu_B2 = GUICtrlCreateMenuItem("读取方式(2)", $menu_B);创建一个菜单项目控件
GUICtrlCreateMenuItem("", $menu_B);创建一个菜单项目控件
$menu_B3 = GUICtrlCreateMenuItem("读取方式(3)", $menu_B);创建一个菜单项目控件

GUISetState()

_DisksType()

While 1
 $msg = GUIGetMsg()
 Switch $msg
  Case $GUI_EVENT_CLOSE
   ExitLoop
  Case $menu_B1;读取(1)
   $TEXT = StringReplace(_GUICtrlTreeView_GetTree($hTreeView), _GUICtrlTreeView_GetText($hTreeView), "");读取方式1
   $location = StringInStr($TEXT, "|", 0, 2);检查某个字符串是否含有给定的子串
   If $location = 0 Then
    $item = GUICtrlRead($hTreeView);读取控件
    If $item = 0 Then
     MsgBox(262160, "错误", "没有选择")
    Else
     $TEXT = GUICtrlRead($item, 1)
     If $TEXT = "" Then
     Else
      MsgBox(266304, "提示", $TEXT)
     EndIf
    EndIf
   Else
    ;方法1
    $array = StringRegExp($TEXT, '\|(.+)\|(.+)', 3);检查字串是否符合给定的正则表达式
    For $i = 1 To UBound($array) - 1
     MsgBox(266304, "方法1", $array[0] & "\" & $array[1])
    Next
   EndIf
  Case $menu_B2;读取(2)
   $TEXT = _GUICtrlTreeView_GetTree($hTreeView);读取方式1
   $location = StringInStr($TEXT, "|", 0, 2);检查某个字符串是否含有给定的子串
   If $location = 0 Then
    $item = GUICtrlRead($hTreeView);读取控件
    If $item = 0 Then
     MsgBox(262160, "错误", "没有选择")
    Else
     $TEXT = GUICtrlRead($item, 1)
     If $TEXT = "" Then
     Else
      MsgBox(266304, "提示", $TEXT)
     EndIf
    EndIf
   Else
    ;方法2
    $array = StringRegExp($TEXT, '\|(.+)', 3);检查字串是否符合给定的正则表达式
    For $i = 0 To UBound($array) - 1
     $text_out = StringRegExpReplace($array[$i], '\|', '\\') ;基于正则表达式的文本替换
     MsgBox(266304, "方法2", $text_out)
    Next
   EndIf
  Case $menu_B3;读取(3)
   $item = GUICtrlRead($hTreeView);读取控件
   If $item = 0 Then
    MsgBox(262160, "错误", "没有选择")
   Else
    $TEXT = GUICtrlRead($item, 1)
    If $TEXT = "" Then
    Else
     MsgBox(266304, "提示", $TEXT)
    EndIf
   EndIf
 EndSwitch
WEnd

 


Func _DisksType();读取硬盘以及分区
 Dim $a[1]
 Local $x = 0
 $strComputer = "localhost"
 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
 $colDiskDrives = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive")
 For $objDrive In $colDiskDrives
  ReDim $a[UBound($a) + 1]
  $a[$x] = GUICtrlCreateTreeViewItem($objDrive.Caption, $hTreeView);创建一个 TreeView 控件项目
  GUICtrlSetImage($a[$x], "shell32.dll", 9);设置图标
  GUICtrlSetColor($a[$x], 0x0000ff);设置字体颜色
  GUICtrlSetState($a[$x], BitOR($GUI_EXPAND, $GUI_DEFBUTTON));设置字体效果
  $strDeviceID = StringReplace($objDrive.DeviceID, "\", "\\");替换字符串中的指定子串
  $colPartitions = $objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
    $strDeviceID & """} WHERE AssocClass = " & _
    "Win32_DiskDriveToDiskPartition")

  For $objPartition In $colPartitions
   $colLogicalDisks = $objWMIService.ExecQuery _
     ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
     $objPartition.DeviceID & """} WHERE AssocClass = " & _
     "Win32_LogicalDiskToPartition")

   For $objLogicalDisk In $colLogicalDisks
    $DriveTreeView = GUICtrlCreateTreeViewItem($objLogicalDisk.DeviceID, $a[$x]);创建一个 TreeView 控件项目
    $search = FileFindFirstFile($objLogicalDisk.DeviceID & "\*.*");搜索分区所有文件
    If $search = -1 Then;检查搜索是否成功
    Else
     While 1
      $file = FileFindNextFile($search)
      If @error Then ExitLoop
      GUICtrlCreateTreeViewItem($file, $DriveTreeView);创建一个 TreeView 控件项目
      GUICtrlSetImage(-1, "shell32.dll", 4);设置指定控件的位图或图标
     WEnd
    EndIf
    FileClose($search); 关闭搜索句柄
   Next
  Next
  $x += 1
 Next
EndFunc   ;==>_DisksType

 

 



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值