How to make all gadgets detect MouseEnter and MouseLeave

Just starting out? Need help? Post your questions and find answers here.
hmasr
User
User
Posts: 15
Joined: Wed May 16, 2012 8:08 pm

How to make all gadgets detect MouseEnter and MouseLeave

Post by hmasr »

Why only Canvas can detect these events?
User avatar
Bisonte
Addict
Addict
Posts: 1306
Joined: Tue Oct 09, 2007 2:15 am

Re: How to make all gadgets detect MouseEnter and MouseLeave

Post 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
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: How to make all gadgets detect MouseEnter and MouseLeave

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How to make all gadgets detect MouseEnter and MouseLeave

Post 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().
BERESHEIT
hmasr
User
User
Posts: 15
Joined: Wed May 16, 2012 8:08 pm

Re: How to make all gadgets detect MouseEnter and MouseLeave

Post by hmasr »

Very helpful. Thanks for all the replies
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: How to make all gadgets detect MouseEnter and MouseLeave

Post 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).
ImageThe happiness is a road...
Not a destination
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: How to make all gadgets detect MouseEnter and MouseLeave

Post 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:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Post Reply