8月82022
[推荐]控件的各事件的绑定
控件事件
AutoIt 的 Win32 控件事件处理程序
特征
便于使用。
高性能。
语法如 Javascript。
许多事件,如鼠标、键盘、拖放文件、滚动等。
事件处理程序
注册事件处理程序以通过其句柄进行控制。
$event = CtrlEvent_Reg($handle)
; IsDllSctruct($event) == 1 ; -> succeed, 0 is failed
; $event == 0 ; -> failed
取消注册事件处理程序以通过其句柄进行控制。
$result = CtrlEvent_UnReg($handle)
; -> 1 : succeed
; -> 0 : failed
鼠标事件
当控件与鼠标交互时调用。
$event.onMouse = '__onMouse'
func __onMouse($e)
$e.x ; -> mouse x pos on control.
$e.y ; -> mouse y pos on control.
$e.state ; -> state of mouse
; +> 0 : left.
; +> 1 : just hovered.
; +> 2 : pressed.
; +> 3 : clicked and just release.
$e.isOver ; -> is mouse over on control.
endfunc
键盘事件
键盘打开时调用。
$event.onKey = '__onKeyEvent'
func __onKeyEvent($e)
$e.type ; -> state of key
; +> 0 : key is down.
; +> 1 : key is up.
$e.which ; -> virtual key code.
$e.key ; -> char of key.
$e.isHotkey ; -> key pressed is a hotkey ($e.type be down).
$e.ctrlKey ; -> ctrl key is pressed.
$e.altKey ; -> alt key is pressed.
$e.shiftKey ; -> shift key is pressed.
endfunc
Drog 文件事件
当文件被控制时调用。
控件必须具有$WS_EX_ACCEPTFILES( 0x00000010 ) 扩展样式才能使用此事件处理程序。
$event.onDrop = '__onDrop'
func __onDrop($e)
$e.count ; -> number of files.
$e.files ; -> list of file name, splitted by semicolon for multiple files
; e.g:
; count = 3
; files = 'file_1.txt;file_2.exe;file_3.img'
endfunc
焦点事件
当控制处于焦点或丢失时调用。
$event.onFucus = '__onFocus'
func __onFocus($e)
$e.type ; -> type of focus
; +> 0 : control was lost focus.
; +> 1 : control being focus on.
$e.handle ; -> handle (hwnd) of last control was lost/being focus.
endfunc
滚动事件
当控件滚动时调用。
$event.onScroll = '__onScroll'
func __onScroll($e)
$e.min ; -> minimium position.
$e.max ; -> maximium position.
$e.page ; -> range page of scrolling.
$e.pos ; -> position of scrolling.
$e.type ; -> type of scrollbar
; +> 0 : horizontal scrollbar.
; +> 1 : vertical scrollbar.
$e.action ; -> user's action on scrollbar
; +> 0 : scrolls left/top by one unit.
; +> 1 : scrolls right/down by one unit.
; +> 2 : scrolls left/top by the width/height of the window.
; +> 3 : scrolls right/down by the width/height of the window.
; +> 4 : the user has dragged the scroll box (thumb) and released the mouse button
; +> 5 : the user is dragging the scroll box;
; this message is sent repeatedly until the user releases the mouse button.
; +> 6 : scrolls to the upper left/top.
; +> 7 : scrolls to the upper right/bottom.
; +> 8 : ends scroll.
endfunc
移动事件
当控制位置刚刚移动时调用。
$event.onMove = '__onMove'
func __onMove($e)
$e.x ; -> x offset (coord on parent client).
$e.y ; -> y offset (coord on parent client).
endfunc
大小事件
当控件大小刚刚改变时调用。
$event.onSize = '__onSize'
func __onSize($e)
$e.type ; -> type of resize
; +> 0 : the window has been restored.
; +> 1 : the window has been minimized.
; +> 2 : the window has been maximized.
; +> 3 : message is sent to all pop-up windows when
; some other window has been restored to its former size.
; +> 4 : the window has been maximized.
$e.width ; -> width.
$e.height ; -> height.
endfunc
例子
#include 'CtrlEvent.au3'
GUICreate('Test')
local $Btn = GUICtrlCreateButton('text', 20, 20, 100, 35, -1, 0x00000010)
local $hBtn = GUICtrlGetHandle($Btn)
local $Btn_event = CtrlEvent_Reg($hBtn)
$Btn_event.onKey = 'Btn_onKey'
$Btn_event.onMouse = 'Btn_onMouse'
$Btn_event.onDrop = 'Btn_onDrop'
GUISetState()
while 1
switch GUIGetMsg()
case -3
CtrlEvent_UnReg($hBtn)
exit
case $Btn ; cannot use this
endswitch
wend
func Btn_onKey($e)
ConsoleWrite('type: ' & $e.type & ', which: ' & $e.which & ', key: ' & $e.key & _
', ctrl: ' & $e.ctrlKey & ', alt: ' & $e.altKey & ', shift: ' & $e.shiftKey & @crlf)
endfunc
func Btn_onMouse($e)
ConsoleWrite('mouse state: ' & $e.state & ', isOver: ' & $e.isOver & ', position: ' & $e.x & ' - '& $e.y & @crlf)
endfunc
func Btn_onDrop($e)
ConsoleWrite('file counted: ' & $e.count & ', name: ' & $e.files & @crlf)
endfunc
项目来源地址:https://github.com/small-autoit/mb
扫描二维码,在手机上阅读
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!