Page 1 of 1

How to make all gadgets detect MouseEnter and MouseLeave

Posted: Tue Jun 05, 2012 1:29 pm
by hmasr
Why only Canvas can detect these events?

Re: How to make all gadgets detect MouseEnter and MouseLeave

Posted: Tue Jun 05, 2012 3:43 pm
by Bisonte
Windows only :

Code: Select all

Procedure IsMouseOverGadget(Gadget) ; return #True if its right
  
  Protected GadgetRect.RECT
  Protected mouse.Point
  
  If IsGadget(Gadget)

    GetWindowRect_(GadgetID(Gadget),GadgetRect.RECT) 
    GetCursorPos_(mouse.POINT) 
    
    If mouse\x>=GadgetRect\Left And mouse\x<=GadgetRect\right And mouse\y>=GadgetRect\Top And mouse\y<=GadgetRect\bottom 
      ProcedureReturn #True 
    Else 
      ProcedureReturn #False 
    EndIf 
  Else
    ProcedureReturn #False
  EndIf

EndProcedure

Re: How to make all gadgets detect MouseEnter and MouseLeave

Posted: Tue Jun 05, 2012 4:02 pm
by ts-soft
And Crossplattform:

Code: Select all

Procedure IsMouseOverGadget(window, gadget)
  If (WindowMouseX(window) >= GadgetX(gadget)) And (WindowMouseX(window) <= (GadgetX(gadget)+GadgetWidth(gadget)))
    If (WindowMouseY(window) >= GadgetY(gadget)) And (WindowMouseY(window) <= (GadgetY(gadget)+GadgetHeight(gadget)))
      ProcedureReturn #True
    EndIf
  EndIf
EndProcedure

Re: How to make all gadgets detect MouseEnter and MouseLeave

Posted: Tue Jun 05, 2012 5:23 pm
by netmaestro
A windows-only solution which sends the enter and leave messages to your event loop:

Code: Select all

;===================================================== 
; Program:          Hover.pbi 
; Author:           netmaestro, srod 
; Date:             August 19, 2006 (Version 1.0)
;                   January 30, 2011 (Version 2.0)
; Target OS:        Windows all 
; Target Compiler:  PureBasic 4.0 and above 
;===================================================== 
; 
Import ""
  PB_Gadget_SendGadgetCommand(hwnd, EventType)
EndImport

#HoverEvent_EnterGadget = #WM_USER + 100 
#HoverEvent_LeaveGadget = #WM_USER + 101

Procedure CallWndProc(nCode, wParam, lParam)
  Shared hook
  Static gadget_is_hot
  
  If nCode < 0
    result = CallNextHookEx_(hook, nCode, wParam, lParam)
  EndIf
  
  *mhs.MOUSEHOOKSTRUCT = lparam
  If GetProp_(*mhs\hwnd, "track_hover_events") And (*mhs\wHitTestCode=#HTCLIENT Or *mhs\wHitTestCode=#HTVSCROLL Or *mhs\wHitTestCode=#HTHSCROLL)
    If gadget_is_hot <> *mhs\hwnd
      If gadget_is_hot
        PB_Gadget_SendGadgetCommand(gadget_is_hot, #HoverEvent_LeaveGadget)
      EndIf
      gadget_is_hot = *mhs\hwnd
      PB_Gadget_SendGadgetCommand(*mhs\hwnd, #HoverEvent_EnterGadget)
    EndIf
  Else
    If gadget_is_hot
      PB_Gadget_SendGadgetCommand(gadget_is_hot, #HoverEvent_LeaveGadget)
      gadget_is_hot = 0
    EndIf
  EndIf
  
  If ncode<0
    ProcedureReturn result
  Else
    ProcedureReturn CallNextHookEx_(hook, nCode, wParam, lParam)
  EndIf
  
EndProcedure

ProcedureDLL InitHoverEvents(hwnd)
  Shared hook
  hook = SetWindowsHookEx_(#WH_MOUSE, @CallWndProc(), #Null, GetCurrentThreadId_())
  If hook
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

ProcedureDLL AddHoverGadget(hwnd)
  If SetProp_(hwnd, "track_hover_events", #True)
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

ProcedureDLL RemoveHoverGadget(hwnd)
  If RemoveProp_(hwnd, "track_hover_events")
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

Procedure EnumControls(hwnd, lParam)
  If GetProp_(hwnd, "track_hover_events")
    RemoveProp_(hwnd, "track_hover_events")
  EndIf
  ProcedureReturn 1
EndProcedure

ProcedureDLL EndHoverEvents(hwnd)
  Shared hook
  EnumChildWindows_(hwnd, @EnumControls(), 0)
  If UnhookWindowsHookEx_(hook)
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

;========================================================
;                  End of Library Code
;========================================================
Usage: Call InitHoverEvents() once before you use AddHoverGadget(), RemoveHoverGadget() and EndHoverEvents() are optional. Look for #HoverEvent_EnterGadget and #HoverEvent_LeaveGadget in your event loop with the appropriate EventGadget().

Re: How to make all gadgets detect MouseEnter and MouseLeave

Posted: Thu Jun 07, 2012 8:45 am
by hmasr
Very helpful. Thanks for all the replies

Re: How to make all gadgets detect MouseEnter and MouseLeave

Posted: Thu Jun 07, 2012 9:04 am
by Kwai chang caine
Strange..i have this error with Netmaestro code with v4.51 et 4.60 :(
POLINK: error: Unresolved external symbol '_PB_Gadget_SendGadgetCommand'.
POLINK: fatal error: 1 unresolved external(s).

Re: How to make all gadgets detect MouseEnter and MouseLeave

Posted: Thu Jun 07, 2012 9:07 am
by ts-soft
Kwaï chang caïne wrote:Strange..i have this error with Netmaestro code with v4.51 et 4.60 :(
You have to create a window, or the code will not work and is useless :wink: