风行者 发表于 2012-1-6 17:06:45

鼠标停留、移出窗口事件

没事看到这两个事件,原来还要wm_mousemove事件的

#include <Winapi.au3>
Global Const $WM_MOUSEMOVE = 0x0200 ;鼠标移动
Global Const $WM_MOUSEHOVER = 0x02A1 ;鼠标停留
Global Const $WM_MOUSELEAVE = 0x02A3 ;鼠标离开
Global Const $TME_HOVER = 0x00000001
Global Const $TME_LEAVE = 0x00000002

$Form1 = GUICreate("测试", 261, 254, 192, 124)
GUISetState()
GUIRegisterMsg($WM_MOUSEMOVE, "WM_MOUSEMOVE")
GUIRegisterMsg($WM_MOUSEHOVER, "WM_MOUSEHOVER")
GUIRegisterMsg($WM_MOUSELEAVE, "WM_MOUSELEAVE")

While GUIGetMsg() <> -3
WEnd

Func WM_MOUSEMOVE($hWnd, $Msg, $wParam, $lParam)
        $tagTRACKMOUSEEVENT = DllStructCreate("DWORD cbSize;DWORD dwFlags;HWNDhwndTrack;DWORD dwHoverTime")
        DllStructSetData($tagTRACKMOUSEEVENT, "cbSize", DllStructGetSize($tagTRACKMOUSEEVENT)) ;结构体缓冲区大小
        DllStructSetData($tagTRACKMOUSEEVENT, "dwFlags", BitOR($TME_HOVER,$TME_LEAVE)) ;注册WM_MOUSEHOVER和$WM_MOUSELEAVE消息
        DllStructSetData($tagTRACKMOUSEEVENT, "hwndTrack", $hWnd) ;当前窗口句柄
        DllStructSetData($tagTRACKMOUSEEVENT, "dwHoverTime", 300) ;触发的时间300ms
        $TRACKMOUSEEVENT = DllStructGetPtr($tagTRACKMOUSEEVENT)
        DllCall("user32.dll", "bool", "TrackMouseEvent","ptr",$TRACKMOUSEEVENT)
EndFunc   ;==>WM_MOUSEMOVE

Func WM_MOUSEHOVER($hWnd, $Msg, $wParam, $lParam)
        $X = _WinAPI_LoWord($lParam)
        $Y = _WinAPI_HiWord($lParam)
        TrayTip("鼠标在窗口坐标", "X:" & $X & " Y:" & $Y, 1, 1)
EndFunc   ;==>WM_MOUSEHOVER

Func WM_MOUSELEAVE($hWnd, $Msg, $wParam, $lParam)
        TrayTip("提示","鼠标移出窗口了!",1,1)
EndFunc

xx44t10 发表于 2012-1-6 17:27:05

本帖最后由 xx44t10 于 2012-1-6 19:13 编辑

谢谢你的代码.学习先 你这个不能对无边框窗体 使用它啊

xiehuahere 发表于 2012-1-9 23:36:09

回复 2# xx44t10


    无边框窗体当然也可以。#include <Winapi.au3>
Global Const $WM_MOUSEMOVE = 0x0200 ;鼠标移动
Global Const $WM_MOUSEHOVER = 0x02A1 ;鼠标停留
Global Const $WM_MOUSELEAVE = 0x02A3 ;鼠标离开
Global Const $TME_HOVER = 0x00000001
Global Const $TME_LEAVE = 0x00000002
Global Const $WS_SYSMENU = 0x00080000
Global Const $WS_POPUP = 0x80000000

;$Form1 = GUICreate("测试", 261, 254, 192, 124)
$Form1 = GUICreate('无边框', 261, 254, 192, 124, BitOR($WS_SYSMENU, $WS_POPUP))
GUISetState()
GUIRegisterMsg($WM_MOUSEMOVE, "WM_MOUSEMOVE")
GUIRegisterMsg($WM_MOUSEHOVER, "WM_MOUSEHOVER")
GUIRegisterMsg($WM_MOUSELEAVE, "WM_MOUSELEAVE")

While GUIGetMsg() <> -3
WEnd

Func WM_MOUSEMOVE($hWnd, $Msg, $wParam, $lParam)
      $tagTRACKMOUSEEVENT = DllStructCreate("DWORD cbSize;DWORD dwFlags;HWNDhwndTrack;DWORD dwHoverTime")
      DllStructSetData($tagTRACKMOUSEEVENT, "cbSize", DllStructGetSize($tagTRACKMOUSEEVENT)) ;结构体缓冲区大小
      DllStructSetData($tagTRACKMOUSEEVENT, "dwFlags", BitOR($TME_HOVER,$TME_LEAVE)) ;注册WM_MOUSEHOVER和$WM_MOUSELEAVE消息
      DllStructSetData($tagTRACKMOUSEEVENT, "hwndTrack", $hWnd) ;当前窗口句柄
      DllStructSetData($tagTRACKMOUSEEVENT, "dwHoverTime", 300) ;触发的时间300ms
      $TRACKMOUSEEVENT = DllStructGetPtr($tagTRACKMOUSEEVENT)
      DllCall("user32.dll", "bool", "TrackMouseEvent","ptr",$TRACKMOUSEEVENT)
EndFunc   ;==>WM_MOUSEMOVE

Func WM_MOUSEHOVER($hWnd, $Msg, $wParam, $lParam)
      $X = _WinAPI_LoWord($lParam)
      $Y = _WinAPI_HiWord($lParam)
      TrayTip("鼠标在窗口坐标", "X:" & $X & " Y:" & $Y, 1, 1)
EndFunc   ;==>WM_MOUSEHOVER

Func WM_MOUSELEAVE($hWnd, $Msg, $wParam, $lParam)
      TrayTip("提示","鼠标移出窗口了!",1,1)
EndFunc

xiehuahere 发表于 2012-1-9 23:41:45

为了获取高两字节和低两字节就include一个大文件,体积可能会不必要地增大吧,直接 BitAnd 做一下位运算好了。

xx44t10 发表于 2012-1-11 21:42:56

回复 3# xiehuahere


谢谢.原来我没把Global Const $WS_SYSMENU = 0x00080000

Global Const $WS_POPUP = 0x80000000它们全局定义 所以出错了

461767057 发表于 2012-2-20 15:05:04

哎呀,是咱的风哥呀.支持支持啊!

wher 发表于 2012-2-21 21:27:29

谢谢楼主分享

即即 发表于 2015-1-20 16:27:41

支持,支持。

zmdzhxj 发表于 2017-3-23 19:23:23

很实用的一个小东西,学习了!

jingzhinvr 发表于 2017-4-18 11:53:20

这个挺好的

venus1982518 发表于 2017-8-9 10:56:29

好东西呀,支持。

w60711 发表于 2019-5-25 18:50:43

這個用於判斷還不錯啊
將之套用在判斷鼠標不再窗口中
就將ToolTip取消掉,'ˋ只是尚未能完美...XD
页: [1]
查看完整版本: 鼠标停留、移出窗口事件