;监视窗体的事例代码 ;sunwind收集整理 ;用Loop监视窗体,而cpu占用不高的技巧: ;其实Loop的频率,每次切换窗口,才会进行一次的。因为有WinWaitNotActive的作用。 ; WinWaitNotActive,ahk_id %__ProcessId% #SingleInstance a := 0 Gui, Add, Text, w200 h37 Border Center gWinMove, 点击此处并拖动 ;~ Gui,Add,Text,w200 h80 Gui, -Caption +ToolWindow +0x400000 Gui, Show, AutoSize WinSet, AlwaysOnTop, toggle,ahk_class AutoHotkeyGUI loop { WinGetActiveTitle, _ActiveTitle ;获取当前激活的窗口名 WinGet,__ProcessId,id,%_ActiveTitle% ;通过窗口名获取进程ID(像记事本之类的,可能更改窗口名) WinGet,_ProcessName,ProcessName,%_ActiveTitle% ;通过窗口名获取运行程序名 MouseGetPos, , , act_id, ClassNN If _ActiveTitle ;如果窗口名是可获取的。 { ; Tooltip % _ActiveTitle " -- " _ProcessName guicontrol,text,Static1,窗口名:%_ActiveTitle% 进程名:%_ProcessName% ;显示窗口名和运行程序名 if (_ActiveTitle="运行") TrayTip,,检测到运行窗口!,10 ;~ MsgBox 运行窗口! if (_ProcessName="notepad.exe") TrayTip,,检测到记事本窗口!,10 if (_ProcessName="mspaint.exe") TrayTip,,检测到画图窗口!,10 if (_ProcessName="iexplore.exe") { ;~ getUrl() ;~ TrayTip,,检测到iexplore!,10 } } Else guicontrol,text,Static1,控件%ClassNN% ;不可见的窗口,不显示内容 WinWaitNotActive,ahk_id %__ProcessId% ;判断窗口是否被更换。一个窗口激活,代表另一个窗口取消激活 _ActiveTitle := ;初始化变量。放WinGetActiveTitle前也行 __ProcessId := _ProcessName := if a break ;退出循环判断 } #1:: a := 1 return ;~ 就这一段,其实Loop的频率,每次切换窗口,才会进行一次的。因为有WinWaitNotActive的作用。 ;其实Loop的频率,每次切换窗口,才会进行一次的。因为有WinWaitNotActive的作用。 ;~ 检测窗口 #2:: WinGetActiveTitle,A_TITLE MsgBox %A_TITLE% return ;~ #r:: ;运行窗口 ;run,%windir%system32rundll32.exe shell32.dll `,#61 ;~ h := DllCall("GetModuleHandleA", "Str", "shell32") ;~ fun := DllCall("GetProcAddress", "UInt", h, "UInt", 61) ;~ DllCall(fun,"Uint", 0) ;~ DllCall(fun,"Uint", 0, "Uint", 0) ;~ Return WinMove: PostMessage, 0xA1, 2,,, A Return GuiEscape: Exitapp Ctrl & LButton:: getUrl() return getUrl() { global ; 处理 COM 错误. WinGetTitle,biaoti,A ;~ ie:=GetTab(name) ;~ InputVar:=ie.LocationURL wangzhi:=getDizhilan() ;~ 把文件名或 URL 分解成它的名称, 目录, 扩展名和驱动器. ;~ SplitPath, InputVar [, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive] SplitPath,wangzhi,OutFileName,OutDir,OutExtension,OutNameNoExt, OutDrive Array := ["OutFileName","OutDir","OutExtension","utNameNoExt", "OutDrive"] ;~ ; 枚举数组内容: ;~ For index, value in array ;~ MsgBox % "Item " index " is '" %value% "'" yuming:=OutDrive ;~ biaoti:=ie.LocationName fangwenshijian:=GetChineseDate() IniRead,shangcishijian,网页记录.ini,%yuming%,%wangzhi%,"" StringSplit,shangcishijian_,shangcishijian,"|" ;~ MsgBox %shangcishijian_2% fangwenshijian:=shangcishijian_2 . "`," . fangwenshijian IniWrite,%biaoti%|%fangwenshijian%,网页记录.ini,%yuming%,%wangzhi%%A_Space% TrayTip,收藏了!,%wangzhi%`n%biaoti% } getDizhilan() { ControlGetText, url, Edit1, % "ahk_id" WinExist("A") return url } GetTab(Name="") { for window in ComObjCreate("Shell.Application").Windows { IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame Name := ( Name="新建选项卡 - Windows Internet Explorer" ) ? "about:Tabs" : RegExReplace( Name, " - (Windows|Microsoft) Internet Explorer" ) If (InStr(window.FullName,"iexplore.exe") and InStr(window.locationName,Name)) { ;网页地址子字符串 ie:=window Break } else ie:= ComObjCreate("InternetExplorer.Application") ; 创建Excel 对象 } Return ie } GetChineseDate(_date="") { if _date= _date:=A_Now FormatTime,_date_str,%_date%, yyyy-MM-dd HH:mm:ss ;~ FormatTime, Year, %_date%, yyyy ;~ FormatTime, Month , %_date%,M ;~ FormatTime, Day, %_date%, d ;~ return number(Year)"年"number(Month) "月"number(Day)"日" return _date_str }