处理鼠标移出事件

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 40star 的 blog

 

windows提供的鼠标移出消息有时候很有用,但是vb6中没有把这个事件封装给我们。
但是我们仍然可以使用子类化技术实现他,下面的代码就是一个简单的例子来处理windows的
wm_mouseleave消息的,我演示的是鼠标移出一个button时的情形。

1.加入一个模块,专门用来处理子类函数:

option explicit

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'copyright 2002 40star, all rights reserved.
'
'e-mail      :40star@163.com
'distribution:你可以完全自由随便的使用这段代码,不管你用于任何目的
'              程序在于交流和学习
'              如有任何bug请和我联系
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
private declare function sendmessage lib "user32" alias "sendmessagea" _
    (byval hwnd as long, byval wmsg as long, _
    byval wparam as long, lparam as string) as long

private declare function getwindowlong lib "user32" alias _
    "getwindowlonga" (byval hwnd as long, _
    byval nindex as long) as long

private declare function setwindowlong lib "user32" alias _
        "setwindowlonga" (byval hwnd as long, byval nindex _
        as long, byval dwnewlong as long) as long
       
private declare function callwindowproc lib "user32" alias _
        "callwindowproca" (byval lpprevwndfunc as long, byval _
        hwnd as long, byval msg as long, byval wparam as _
        long, byval lparam as long) as long
 
const gwl_wndproc = (-4&)

dim prevwndproc&

private const wm_destroy = &h2


public declare function trackmouseevent lib "user32" (lpeventtrack as trackmouseeventtype) as long

public const tme_cancel = &h80000000
public const tme_hover = &h1&
public const tme_leave = &h2&
public const tme_nonclient = &h10&
public const tme_query = &h40000000

private const wm_mouseleave = &h2a3&

public type trackmouseeventtype
    cbsize as long
    dwflags as long
    hwndtrack as long
    dwhovertime as long
end type

public btracking as boolean
dim evttrack as trackmouseeventtype
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

private function subwndproc(byval hwnd as long, byval msg as long, _
                            byval wparam as long, byval lparam as long) _
                            as long

   if msg = wm_destroy then terminate (hwnd)

   '处理鼠标移出消息
   if msg = wm_mouseleave then
      btracking = false
      form1.print "the mouse left the form!"
   end if
   subwndproc = callwindowproc(prevwndproc, hwnd, msg, wparam, lparam)
end function

public sub init(hwnd as long)
  prevwndproc = setwindowlong(hwnd, gwl_wndproc, addressof subwndproc)
end sub

public sub terminate(hwnd as long)
  call setwindowlong(hwnd, gwl_wndproc, prevwndproc)
end sub

' -- 模块结束 -- '

2. 窗体中处理需要加入的代码:

option explicit

private sub command1_mousemove(button as integer, shift as integer, x as single, y as single)
if btracking = false then
   btracking = true
    dim et as trackmouseeventtype
    'initialize structure
    et.cbsize = len(et)
    et.hwndtrack = command1.hwnd
    et.dwflags = tme_leave
    'start the tracking
    trackmouseevent et
end if
end sub

private sub form_load()
call init(command1.hwnd)
end sub

private sub form_unload(cancel as integer)
call terminate(command1.hwnd)
end sub


此例程在win2000 + vb6中调试通过

本文关键:WM_MOUSELEAVE TrackMouseEvent SubClassing
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top