Xojo Function and Populating Image Canvas

New to Xojo platform and studying tutorials and have a question please.

I have a form with a canvas and a push button and the following Method/Function that captures the screen. How would you go about populating the canvas control with the picture (screencap) from the Function on a button click event.

[code] If Width = 0 Or Height = 0 Then Return Nil
Dim screenCap As Picture

#If TargetWin32 Then
Declare Function GetDesktopWindow Lib “User32” () As Integer
Declare Function GetDC Lib “User32” (HWND As Integer) As Integer
Declare Function ReleaseDC Lib “User32” (HWND As Integer, DC As Integer) As Integer
Declare Function BitBlt Lib “GDI32” (DCdest As Integer, xDest As Integer, yDest As Integer, nWidth As Integer, _
nHeight As Integer, DCdource As Integer, xSource As Integer, ySource As Integer, rasterOp As Integer) As Boolean

Const SRCCOPY = &h00CC0020
Const CAPTUREBLT = &h40000000

screenCap = New Picture(Width, Height, 24)
Dim deskHWND As Integer = GetDesktopWindow()
Dim deskHDC As Integer = GetDC(deskHWND)
Call BitBlt(screenCap.Graphics.Handle(1), 0, 0, Width, Height, DeskHDC, X, Y, SRCCOPY Or CAPTUREBLT)
Call ReleaseDC(DeskHWND, deskHDC)


Return screenCap[/code]

Any help would be great.

I would create a custom subclass of Canvas with a picture property. Then in your pushbutton you can set that property to the screencap value and refresh the canvas. In the canvas subclass’ Paint event, you simply draw the picture property (if it’s not nil).

You could create a method within the canvas subclass that accepts a picture and sets the internal picture property if you’d rather not expose the property. That’s probably a better way to go. It would also handle refreshing the itself, so your push button wouldn’t have to do that.

Paint Event of Canvas

if screenCap<>nil then g.drawpicture screencap,0,0