找回密码
 加入
搜索
查看: 5117|回复: 10

[AU3基础] 【已解决】同一快捷键既能调用也能结束某一自定义函数,如何实现?

[复制链接]
发表于 2015-1-19 11:42:16 | 显示全部楼层 |阅读模式
本帖最后由 三八线 于 2015-1-19 16:35 编辑

例如如下代码,摆弄半天,想实现Au3Info的CTRL+ALT+F切换捕获与暂停捕获效果,无法实现,请高手帮忙!
#include <Misc.au3>
#include <WinAPI.au3>

HotKeySet("^!f", "_WindowFromPoint_KG") ; 

Global $g_tStruct = DllStructCreate($tagPOINT) ; 创建定义检查点的结构.
Local $tKG = 1

While 1
WEnd

Func _WindowFromPoint_KG() 
        If $tKG = 1 Then 
                $tKG = 0
                _WindowFromPoint($tKG) 
        EndIf
EndFunc   ;==>_WindowFromPoint

Func _WindowFromPoint(ByRef $tKG)          ;检索鼠标位置的窗口句柄.
        While 1
                ToolTip("")
                Position() ; 更新鼠标 X 和 Y 坐标和结构的 X 和 Y 的元素.
                Local $hWnd = _WinAPI_WindowFromPoint($g_tStruct) ; 检索窗口句柄.
                ToolTip($hWnd, Default, Default, '鼠标下的窗口句柄', 1 ,1)        ; 设置工具提示.
                Sleep(500)
                ;If _IsPressed("11") And _IsPressed("12") And _IsPressed("46") Then ExitLoop       ;ctrl+alt+F组合键退出while循环
                If $tKG = 0 Then 
                        ToolTip("")                                       ; 清除工具提示
                        $tKG = 1
                        ExitLoop
                EndIf
        WEnd
EndFunc   ;==>_WindowFromPoint

Func Position()
        DllStructSetData($g_tStruct, "x", MouseGetPos(0))          ;MouseGetPos(0) = 当前鼠标的 X 坐标
        DllStructSetData($g_tStruct, "y", MouseGetPos(1))          ;MouseGetPos(1) = 当前鼠标的 Y 坐标
EndFunc   ;==>Position
 楼主| 发表于 2015-1-19 11:49:44 | 显示全部楼层
自己顶一下,希望大家帮忙,出出主意。
 楼主| 发表于 2015-1-19 12:19:50 | 显示全部楼层
用两个不同快捷键分别执行调用、结束自定义函数,很容易实现,见如何代码:

#include <Misc.au3>
#include <WinAPI.au3>

HotKeySet("^!f", "_WindowFromPoint") ; 设置 CTRL+ALT+T调用自定义函数.

Global $g_tStruct = DllStructCreate($tagPOINT) ; 创建定义检查点的结构.

;_WindowFromPoint()
While 1
        Sleep(100)
WEnd


Func _WindowFromPoint()          ;检索鼠标位置的窗口句柄.
        While 1
                ToolTip("")
                Position() ; 更新鼠标 X 和 Y 坐标和结构的 X 和 Y 的元素.
                Local $hWnd = _WinAPI_WindowFromPoint($g_tStruct) ; 检索窗口句柄.
                ToolTip($hWnd, Default, Default, '鼠标下的窗口句柄', 1 ,1)        ; 设置工具提示.
                Sleep(100)
                If _IsPressed("11") And _IsPressed("12") And _IsPressed("54")  Then    ;CTRL+ALT+T退出While循环
                        ToolTip("")                                       ; 清除工具提示
                        ExitLoop       ;ctrl+ PAUSE组合键退出while循环
                EndIf
        WEnd
EndFunc   ;==>_WindowFromPoint

Func Position()
        DllStructSetData($g_tStruct, "x", MouseGetPos(0))          ;MouseGetPos(0) = 当前鼠标的 X 坐标
        DllStructSetData($g_tStruct, "y", MouseGetPos(1))          ;MouseGetPos(1) = 当前鼠标的 Y 坐标
EndFunc   ;==>Position
 楼主| 发表于 2015-1-19 13:05:11 | 显示全部楼层
再丰富一下窗口信息。
#include <WinAPIProc.au3>
#include <Misc.au3>
#include <WinAPI.au3>

HotKeySet("^!f", "_WindowFromPoint") ; 设置 CTRL+ALT+T调用检索鼠标位置的窗口句柄函数, CTRL+ALT+T结束该函数.
Global $g_tStruct = DllStructCreate($tagPOINT) ; 检索鼠标位置的窗口句柄函数用。创建定义检查点的结构.该变量声明必须在主 While 循环之前。

;_WindowFromPoint() 
While 1
        Sleep(100)
WEnd


Func _WindowFromPoint()          ;检索鼠标位置的窗口句柄.
        While 1
                ToolTip("")
                Position() ; 更新鼠标 X 和 Y 坐标和结构的 X 和 Y 的元素.
                Local $hWnd = _WinAPI_WindowFromPoint($g_tStruct)  ;依据光标位置检索窗口句柄.
                Local $sTitle = WinGetTitle($hWnd, "")         ;依据句柄获窗口标题
                Local $hWnd1 = WinGetHandle($hWnd, "")         ;依据句柄获窗口句柄
                Local $sPid = WinGetProcess($hWnd, '')         ;依据句柄获取窗口关联的进程 ID(PID).
                Local $Dir = _WinAPI_GetProcessFileName($sPid) ;检索指定进程可执行文件的完全有效路径(含文件名).
                $sTempText = '标题=' & $sTitle & @CRLF & '句柄 =' & $hWnd & @CRLF & '句柄1=' & $hWnd1 & @CRLF & '进程=' & $sPid & @CRLF & '路径=' & $Dir
                ToolTip($sTempText, Default, Default, '鼠标下的窗口句柄', 1 ,1)        ; 设置工具提示.
                Sleep(100)
                If _IsPressed("11") And _IsPressed("12") And _IsPressed("54")  Then    ;CTRL+ALT+T退出While循环
                        ToolTip("")                                       ; 清除工具提示
                        ExitLoop       ;ctrl+ PAUSE组合键退出while循环
                EndIf
        WEnd
EndFunc   ;==>_WindowFromPoint

Func Position()
        DllStructSetData($g_tStruct, "x", MouseGetPos(0))          ;MouseGetPos(0) = 当前鼠标的 X 坐标
        DllStructSetData($g_tStruct, "y", MouseGetPos(1))          ;MouseGetPos(1) = 当前鼠标的 Y 坐标
EndFunc   ;==>Position
 楼主| 发表于 2015-1-19 13:27:00 | 显示全部楼层
再丰富一个ID信息,请大家走过路过,帮我出出主意,用一个快捷键实现调用和结束自定义函数的功能。
#include <WinAPIProc.au3>
#include <Misc.au3>
#include <WinAPI.au3>

HotKeySet("^!f", "_WindowFromPoint") ; 设置 CTRL+ALT+T调用检索鼠标位置的窗口句柄函数, CTRL+ALT+T结束该函数.
Global $g_tStruct = DllStructCreate($tagPOINT) ; 检索鼠标位置的窗口句柄函数用。创建定义检查点的结构.该变量声明必须在主 While 循环之前。

;_WindowFromPoint() 
While 1
        Sleep(100)
WEnd


Func _WindowFromPoint()          ;检索鼠标位置的窗口句柄.
        While 1
                ToolTip("")
                Position() ; 更新鼠标 X 和 Y 坐标和结构的 X 和 Y 的元素.
                Local $hWnd = _WinAPI_WindowFromPoint($g_tStruct)  ;依据光标位置检索窗口句柄.
                Local $iID = _WinAPI_GetDlgCtrlID($hWnd)       ;依据句柄获 ID
                Local $sTitle = WinGetTitle($hWnd, "")         ;依据句柄获窗口标题
                Local $hWnd1 = WinGetHandle($hWnd, "")         ;依据句柄获窗口句柄
                Local $sPid = WinGetProcess($hWnd, '')         ;依据句柄获取窗口关联的进程 ID(PID).
                Local $Dir = _WinAPI_GetProcessFileName($sPid) ;检索指定进程可执行文件的完全有效路径(含文件名).
                $sTempText = '标题=' & $sTitle & @CRLF & '句柄 =' & $hWnd & @CRLF & '句柄1=' & $hWnd1 &  @CRLF & 'ID=' & $iID & @CRLF & '进程=' & $sPid & @CRLF & '路径=' & $Dir
                ToolTip($sTempText, Default, Default, '鼠标下的窗口句柄', 1 ,1)        ; 设置工具提示.
                Sleep(100)
                If _IsPressed("11") And _IsPressed("12") And _IsPressed("54")  Then    ;CTRL+ALT+T退出While循环
                        ToolTip("")                                       ; 清除工具提示
                        ExitLoop       ;ctrl+ PAUSE组合键退出while循环
                EndIf
        WEnd
EndFunc   ;==>_WindowFromPoint

Func Position()
        DllStructSetData($g_tStruct, "x", MouseGetPos(0))          ;MouseGetPos(0) = 当前鼠标的 X 坐标
        DllStructSetData($g_tStruct, "y", MouseGetPos(1))          ;MouseGetPos(1) = 当前鼠标的 Y 坐标
EndFunc   ;==>Position
发表于 2015-1-19 15:10:03 | 显示全部楼层
本帖最后由 afan 于 2015-1-19 16:41 编辑
#include <WinAPIEx.au3>

HotKeySet('{esc}', '_Exit')
HotKeySet('^!f', '_WindowFromPoint') ; 设置 CTRL+ALT+F调用检索鼠标位置的窗口句柄函数, CTRL+ALT+F结束该函数.
Global $g_tStruct = DllStructCreate($tagPOINT) ; 检索鼠标位置的窗口句柄函数用。创建定义检查点的结构.该变量声明必须在主 While 循环之前。
Global $SEFlag, $hWnd_Bak

While 1
        Sleep(100)
WEnd

Func _WindowFromPoint() ;检索鼠标位置的窗口句柄.
        $SEFlag = Not $SEFlag
        While $SEFlag
                Sleep(100)
                _Position() ; 更新鼠标 X 和 Y 坐标和结构的 X 和 Y 的元素.
                Local $hWnd = _WinAPI_WindowFromPoint($g_tStruct) ;依据光标位置检索窗口句柄.
                If $hWnd = $hWnd_Bak Then ContinueLoop
                $hWnd_Bak = $hWnd
                Local $iID = _WinAPI_GetDlgCtrlID($hWnd) ;依据句柄获 ID
                Local $sTitle = WinGetTitle($hWnd, '') ;依据句柄获窗口标题
                Local $hWnd1 = WinGetHandle($hWnd, '') ;依据句柄获窗口句柄
                Local $sPid = WinGetProcess($hWnd, '') ;依据句柄获取窗口关联的进程 ID(PID).
                Local $Dir = _WinAPI_GetProcessFileName($sPid) ;检索指定进程可执行文件的完全有效路径(含文件名).
                $sTempText = '标题=' & $sTitle & @CRLF & '句柄 =' & $hWnd & @CRLF & '句柄1=' & $hWnd1 & @CRLF & 'ID=' & $iID & @CRLF & '进程=' & $sPid & @CRLF & '路径=' & $Dir
                ToolTip($sTempText, Default, Default, '鼠标下的窗口句柄', 1, 1) ; 设置工具提示.
        WEnd
EndFunc   ;==>_WindowFromPoint

Func _Position()
        DllStructSetData($g_tStruct, 'x', MouseGetPos(0)) ;MouseGetPos(0) = 当前鼠标的 X 坐标
        DllStructSetData($g_tStruct, 'y', MouseGetPos(1)) ;MouseGetPos(1) = 当前鼠标的 Y 坐标
EndFunc   ;==>_Position

Func _Exit()
        Exit
EndFunc   ;==>_Exit
 楼主| 发表于 2015-1-19 16:33:05 | 显示全部楼层
本帖最后由 三八线 于 2015-1-19 16:34 编辑

回复 6# afan


    谢谢A大出手,高手出手就是不一样,不仅实现了同一快捷键既能调用也能结束自定义函数,还顺带解决了tooltip工具提示的闪烁问题。结下来就是理解A大代码的逻辑。再次谢谢A大!!,圆满结贴!
发表于 2015-1-19 16:43:53 | 显示全部楼层
回复 7# 三八线


    帖子修改了下,之前的 Sleep() 位置需要调整,放在循环的开始位置比较好,否则 CPU 占用会很高
 楼主| 发表于 2015-1-19 16:46:05 | 显示全部楼层
本帖最后由 三八线 于 2015-1-19 16:48 编辑

回复 6# afan


    禁不住请教A大,  $SEFlag = Not $SEFlag 该句该如何理解?,是咋实现的再次快捷键结束该函数?
发表于 2015-1-19 17:04:23 | 显示全部楼层
每调用一次 $SEFlag 的值就在 1 与 0 之间切换一次,之后是否继续下去的循环就是依据这个值 While $SEFlag
 楼主| 发表于 2015-1-19 18:37:12 | 显示全部楼层
回复 10# afan


    $SEFlag = Not $SEFlag  这样写竟有这样的作用,太神奇啦!谢谢A大!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 14:15 , Processed in 0.091962 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表