This is just the sort of thing I can’t resist… so here’s something.
define CGSize and CGPoint as follows:
CGPoint:
x as cgfloat
y as cgfloat
CGSize:
width as cgfloat
height as cgfloat
And this method:
[code]Function DPIForDisplayAtXY(x as integer, y as integer) As xojo.Core.Size
declare function CGGetDisplaysWithPoint lib “CoreGraphics” ( point as CGPoint, maxDisplays as uint32, byref displays as ptr , byref matchingDisplayCount as uint32 ) as integer
declare function CGDisplayScreenSize lib “CoreGraphics” ( display as ptr ) as CGSize
declare function CGDisplayPixelsWide lib “CoreGraphics” ( display as ptr ) as UInteger
declare function CGDisplayPixelsHigh lib “CoreGraphics” ( display as ptr ) as UInteger
dim p As CGPoint
p.x=x
p.y=y
dim cnt as uint32
dim d as ptr
call CGGetDisplaysWithPoint(p,1,d,cnt)
dim screenSize as CGSize
screenSize=CGDisplayScreenSize(d)
Return new xojo.Core.Size(CGDisplayPixelsWide(d)/screenSize.width25.4,CGDisplayPixelsHigh(d)/screenSize.height25.4)
End Function
[/code]
Will return the horizontal and vertical DPI of the screen at the X and Y provided.
Keep in mind I put this together in a few minutes, so some error checking and maybe a check that the display supports “Extended Display Identification Data” may be in order (if not then the documentation says 72dpi is assumed) …
My quick tests here return reasonable values for my cinema display at various resolutions and Macbook display.