IsMouseOnGuiElement

From Multi Theft Auto: Wiki
Jump to: navigation, search

This function allows you to check whether or not your mouse is over a specific gui element, this is especially useful if the gui element has a parent.

Syntax

element isMouseOnGuiElement (element guiElement, element guiElementParent)

Code

function isMouseOnGuiElement(guiElement,guiElementParent)
    if isCursorShowing() then
        local sx,sy = guiGetScreenSize()
        local x,y = getCursorPosition()
        local wx,wy = 0,0
        x,y = x*sx, y*sy
        if guiElementParent then
            wx,wy = guiGetPosition(guiElementParent,false)
        end
        local bx,by = guiGetPosition(guiElement,false)
        local ex,ey = guiGetSize(guiElement,false)
        if x >= wx+bx and x <= wx+bx+ex and y >= wy+by and y <= wy+by+ey then
            return true
        end
        return false
    end
end

Required Arguments

  • guiElement: The GUI element you want to check if the mouse is over it.
  • guiElementParent: The parent element of the element you want to check.

Returns

Returns true if the mouse is over the element, otherwise it returns false.

Example

local window = guiCreateWindow(500,500,200,200,"Test",false)
local image = guiCreateStaticImage(25,25,50,50,"image.png",false,window)

addEventHandler("onClientRender",root,function()
    if isMouseOnGuiElement(image,window) then
        guiStaticImageLoadImage(image,"image2.png")
    else
        guiStaticImageLoadImage(image,"image.png")
    end
end)

See Also

General functions

FROM VERSION 1.5.6 r16361 ONWARDS

Browsers

Buttons

Checkboxes

Comboboxes

Edit Boxes

Gridlists

FROM VERSION 1.5.6 r16204 ONWARDS

Memos

Progressbars

Radio Buttons

Scrollbars

Scrollpanes

Static Images

Tab Panels

Tabs

Text Labels

Windows

Input

GUI