If you have multiple screens attached, it probably is best to capture the screen the application is running on.
That could be done like this.
Code: Select all
; PB 5.40+
EnableExplicit
Define CGImage, NSImage, MainScreen, MainScreenID, Rect.NSRect, ZeroSize.NSSize
; Get id for main screen
MainScreen = CocoaMessage(0, 0, "NSScreen mainScreen")
MainScreenID = CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, MainScreen, "deviceDescription"),
"objectForKey:$", @"NSScreenNumber"), "intValue")
; Create NSImage from main screen
CGImage = CGDisplayCreateImage_(MainScreenID)
NSImage = CocoaMessage(0, CocoaMessage(0, 0, "NSImage alloc"), "initWithCGImage:", CGImage, "size:@", @ZeroSize)
CGImageRelease_(CGImage)
; Set rectangle (0, 0, width, height) for main screen
CocoaMessage(@Rect, MainScreen, "frame")
Rect\origin\x = 0
Rect\origin\y = 0
; Create a PureBasic image
CreateImage(0, Rect\size\width, Rect\size\height)
StartDrawing(ImageOutput(0))
CocoaMessage(0, NSImage, "drawInRect:@", @Rect)
StopDrawing()
; Release the NSImage
CocoaMessage(0, NSImage, "release")
If OpenWindow(0, 0, 0, 660, 500, "Image from display", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ImageGadget(0, 10, 10, 640, 480, ImageID(0))
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
If you only want to show the captured screen, you don't need to convert to a PB image.
In that case you can display a retina screen capture as sharp as the original.
Code: Select all
; PB 5.40+
EnableExplicit
Define CGImage, NSImage, MainScreen, MainScreenID, Scale.CGFloat, Size.NSSize
; Get id for main screen
MainScreen = CocoaMessage(0, 0, "NSScreen mainScreen")
MainScreenID = CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, MainScreen, "deviceDescription"),
"objectForKey:$", @"NSScreenNumber"), "intValue")
; Create NSImage from main screen
CGImage = CGDisplayCreateImage_(MainScreenID)
Size\width = CGImageGetWidth_(CGImage)
Size\height = CGImageGetHeight_(CGImage)
NSImage = CocoaMessage(0, CocoaMessage(0, 0, "NSImage alloc"), "initWithCGImage:", CGImage, "size:@", @Size)
CGImageRelease_(CGImage)
; Correct NSImage size for backingScaleFactor
If OSVersion() >= #PB_OS_MacOSX_10_7
CocoaMessage(@Scale, MainScreen, "backingScaleFactor")
Size\width / Scale
Size\height / Scale
CocoaMessage(0, NSImage, "setSize:@", @Size)
EndIf
; Show NSImage
If OpenWindow(0, 0, 0, 660, 500, "Image from display", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ImageGadget(0, 10, 10, 640, 480, NSImage)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf