Use the srl inventory include.
Simba Code:
{*******************************************************************************
function CoordsToItem(X, Y: Integer): Integer;
By: Boreas
Description: Returns which Inventory slot the X and Y coords are in. If the
coords are between slots (or not in the inventory area at all) it will return 0.
*******************************************************************************}
Looks like what you want, although you may want to edit it to find the closest if it isn't in any of the boxes.