虽然autoit本身并不提供操作桌面的相关函数,不过利用已有的窗口函数我们仍可以对桌面进行简单的控制。
一、桌面句柄的取得
Windows的桌面实际上就是一个窗口,其标题就是"Program Manager",而桌面上的图标则是一个标准的SysListView32控件。因此,我们可以这样获取桌面窗口和控件的句柄:
CODE: AutoIt $handle = WinGetHandle("Program Manager") ;窗口句柄
$ctrl = ControlGetHandle("Program Manager","","SysListView321") ;控件句柄当然了,使用窗口函数时我们并不一定要用窗口句柄和控件句柄,不过在调用系统API时我们只能使用窗口句柄,而且这样会让代码更整洁一些。
二、对桌面窗口的控制
既然已经得到了桌面窗口的句柄,我们就不妨利用窗口函数对桌面简单操作一下:
CODE: AutoIt WinClose ($handle) ;显示关机对话框
Sleep(5000)
WinSetTrans ($handle,"",100) ;桌面图标透明(实际上没这么简单)
$pos = WinGetPos ($handle)
MsgBox(0,"DesktopWidth",$pos[2])
MsgBox(0,"DesktopHeight",$pos[3])
WinKill ($handle) ;重启桌面另外需要说明的是,只要用WinMove函数移动桌面,桌面图标就会消失。
三、对桌面图标的控制
因为桌面图标是一个ListView32控件,所以用ControlListView函数就可以对桌面图标进行基本的操作:
CODE: AutoIt ControlListView($handle,"",$ctrl,"SelectAll") ;选中桌面全部图标
ControlListView($handle,"",$ctrl,"SelectClear") ;取消选择
ControlListView($handle,"",$ctrl,"Select",4,7) ;选中第5到8个图标
MsgBox(0,"第一个图标名称",ControlListView($handle,"",$ctrl,"GetText",0,0))
MsgBox(0,"图标总数",ControlListView($handle,"",$ctrl,"GetItemCount"))
MsgBox(0,"选中图标数",ControlListView($handle,"",$ctrl,"GetSelectedCount"))
ControlListView($handle,"",$ctrl,"ViewChange","list") ;列表形式显示桌面图标(失败)
ControlListView($handle,"",$ctrl,"ViewChange","details") ;以"详细信息"的形式列出
ControlListView($handle,"",$ctrl,"ViewChange","smallicons") ;复原
ControlListView($handle,"",$ctrl,"ViewChange","largeicons") ;同上
ControlDisable ($handle,"",$ctrl) ;无法点击桌面图标
ControlEnable ($handle,"",$ctrl) ;恢复
ControlHide ($handle,"",$ctrl) ;隐藏桌面图标
ControlShow ($handle,"",$ctrl) ;恢复
;下面是列出全部选中的图标
$num = ControlListView($handle,"",$ctrl,"GetSelected",1)
$array= StringSplit($num,"|")
For $i = 1 To $array[0]
MsgBox(0,"选中的第"&$i&"个图标",ControlListView($handle,"",$ctrl,"GetText",$array[$i],0))
Next
1.移动桌面图标
移动图标主要靠发送LVM_SETITEMPOSITION消息来实现,下面是一个例子,其中$i_index、$i_x、$i_y分别代表要移动的图标(这里是第2个)、移动到的x坐标、y坐标:
CODE: AutoIt
#include <Misc.au3>
Global Const $LVM_FIRST = 0x1000
Global Const $LVM_SETITEMPOSITION = ($LVM_FIRST + 15)
$i_index = 1
$i_x = 500
$i_y = 600
_SendMessage(ControlGetHandle("Program Manager","",1), $LVM_SETITEMPOSITION, $i_index, BitOR(BitShift($i_y, -16), BitAND($i_x, 0xFFFF)))
2.设置桌面的对齐网格
由于对齐网格是listview控件的一种style,所以通过使用LVM_SETEXTENDEDLISTVIEWSTYLE消息我们就能对此进行设置:
CODE: AutoIt
#Include <misc.au3>
#include <GuiListView.au3>
$LVS_EX_SNAPTOGRID = 0x80000
;对齐到网格
_SendMessage(ControlGetHandle("Program Manager","",1), $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_SNAPTOGRID, $LVS_EX_SNAPTOGRID)
;不对齐
_SendMessage(ControlGetHandle("Program Manager","",1), $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_SNAPTOGRID, 0)
3.让窗口处于最下层
通过将某一窗口设为桌面的子窗口,我们就能实现窗口置底:
CODE: AutoIt
$gui = GuiCreate("MyGUI", 227, 119)
DllCall("user32.dll", "int", "SetParent", "hwnd", $gui, "hwnd",WinGetHandle("Program Manager"))
GuiSetState()
Do
;;;
Until GuiGetMsg() =-3
Exit
4.设置任务栏的状态
利用SHAppBarMessage函数我们能对任务栏的状态进行控制:
CODE: AutoIt
Global Const $ABM_SETSTATE = 0x0000000a
Global Const $ABS_ALWAYSONTOP = 0x2
Global Const $ABS_AUTOHIDE = 0x1
Global $pabd = DllStructCreate("dword;int;uint;uint;int;int;int;int;int")
DllStructSetData($pabd,1,DllStructGetSize($pabd)) ;cbSize
DllStructSetData($pabd,2,ControlGetHandle("Start","","Shell_TrayWnd")) ;hWnd
Func SHAppBarMessage($Message,ByRef $pabd)
$lResult = DllCall("shell32.dll","int","SHAppBarMessage","int",$Message,"ptr",DllStructGetPtr($pabd))
If Not @error Then
If $lResult[0] Then
Return $lResult[0]
EndIf
EndIf
SetError(1)
Return False
EndFunc
;要设置不同的状态,请取消相应语句的注释
;DllStructSetData($pabd,9,$ABS_AUTOHIDE) ;自动隐藏,且不位于窗口前
;DllStructSetData($pabd,9,$ABS_ALWAYSONTOP) ;不自动隐藏,且位于窗口前
DllStructSetData($pabd,9,BitOR($ABS_ALWAYSONTOP,$ABS_AUTOHIDE)) ;自动隐藏,且位于窗口前
SHAppBarMessage($ABM_SETSTATE,$pabd) ;发送ABM_SETSTATE消息应用修改
更多关于SHAppBarMessage函数的信息请看这里。
5.隐藏任务栏、开始按钮、任务栏图标等
说到这里我们已经转向了对任务栏的控制,同桌面一样,任务栏也是一个窗口,开始按钮、时钟等都是其下的控件,我们可以像控制窗口一样控制任务栏:
CODE: AutoIt
Opt("WinTitleMatchMode", 4)
$sTrayWindow = WinGetHandle("[CLASS:Shell_TrayWnd]") ;窗口句柄
$sTrayClock = ControlGetHandle($sTrayWindow,"","TrayClockWClass1") ;时钟句柄
$sTrayNotify = ControlGetHandle($sTrayWindow,"","TrayNotifyWnd1") ;任务栏图标句柄
$sStartButton = ControlGetHandle($sTrayWindow,"","Button1") ;开始按钮句柄
;隐藏任务栏
WinSetState($sTrayWindow,"",@SW_HIDE)
;隐藏任务栏时钟
WinSetState($sTrayClock,"",@SW_SHOW)
;隐藏任务栏图标(会马上还原)
WinSetState($sTrayNotify,"",@SW_HIDE)
;隐藏开始按钮
WinSetState($sStartButton,"",@SW_HIDE)
6.桌面图标的相关控制
细心的朋友会发现,我控制桌面图标时都是用的名为SendMessage的API函数,而autoit里全部以_GUICtrlListView打头的函数都是这样定义的,也就是说,利用autoit已有的这些函数我们就能对桌面图标进行控制,比如说:
CODE: AutoIt
#include <GuiListView.au3>
_GUICtrlListViewDeleteAllItems (ControlGetHandle("Program Manager","","SysListView321"))