Detect when mouse leaves program window
#17
(08-16-2023, 06:46 PM)TerryRitchie Wrote: The border width and height were always reporting a value of 1 for some reason throwing off the working area calculations within the window by a slight amount.

I traced this down to having used the wrong values for getting the border width and height. There are values for 3D and non-3D borders. Apparently Windows defaults to 3D. I made the changes below that now reflects the working area calculations perfectly. I also made the changes in the original code I posted on the previous page.


Code: (Select All)
TYPE POINTAPI
    x AS LONG
    y AS LONG
END TYPE
DIM apixy AS POINTAPI 'mouse x/y for the GetCursorPos function
DIM CaptionHeight AS INTEGER
DIM BorderHeight AS INTEGER
DIM BoderWidth AS INTEGER
DECLARE DYNAMIC LIBRARY "user32"
    'get current mouse x/y position
    'http://allapi.mentalis.org/apilist/GetCursorPos.shtml
    FUNCTION GetCursorPos% (lpPoint AS POINTAPI)
    'system window metrics in pixels
    'https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics
    FUNCTION GetSystemMetrics% (BYVAL nIndex AS INTEGER)
END DECLARE

SCREEN 12
DO
    CaptionHeight = GetSystemMetrics(4) ' caption (title bar) height in pixels
    BorderWidth = GetSystemMetrics(45) '   3D width of window border in pixels (5 for non-3D)
    BorderHeight = GetSystemMetrics(46) '  3D height of window border in pixels (6 for non-3D)
    'poll mouse x/y
    tmp = GetCursorPos(apixy)
    ax = apixy.x - BorderWidth
    ay = apixy.y - CaptionHeight - BorderHeight
    sx = _SCREENX
    sy = _SCREENY
    CLS: PRINT "MOUSE IN"
    IF ax - sx < 0 THEN CLS: PRINT "MOUSE OUT"
    IF ax - sx > _WIDTH THEN CLS: PRINT "MOUSE OUT"
    IF ay - sy < 0 THEN CLS: PRINT "MOUSE OUT"
    IF ay - sy > _HEIGHT THEN CLS: PRINT "MOUSE OUT"
    LOCATE 2, 2: PRINT "CaptionHeight:"; CaptionHeight
    LOCATE 3, 2: PRINT "BorderWidth  :"; BorderWidth
    LOCATE 4, 2: PRINT "BorderHeight :"; BorderHeight
    _DISPLAY
LOOP

(08-17-2023, 05:34 PM)SpriggsySpriggs Wrote: Hmmm.... GetClientRect might take it into account. It reports the coordinates of each corner, if I'm not mistaken.

I'll look into that, thank you.
Software and cathedrals are much the same — first we build them, then we pray.
QB64 Tutorial
Reply


Messages In This Thread
RE: Detect when mouse leaves program window - by TerryRitchie - 08-17-2023, 07:08 PM



Users browsing this thread: 5 Guest(s)