Capture window contents?

BlitzMax Forums/BlitzMax GUI Programming/Capture window contents?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I tried grabbing directly from the htmlview gadget, but i only got black & white? so this grabs from the screen (similar to the module mentioned earlier) but limits the grab to just the gadget.
It only does 24 and 32 bits, and is Win32 only.

Function GrabGadgetPixmap:TPixmap( gadget:TGadget)
?Win32
	Extern "Win32"
		Function GetDIBits:Int( hdc:Int,hbmp:Int,uStartScan:Int,cScanLines:Int,lpvBits:Byte Ptr,lpbmi:Byte Ptr,uUsage:Int)
		Function CreateDC:Int( driver$z, device$z, output$z, initdata:Byte Ptr) = "CreateDCA@16"
	EndExtern
	
	Function WindowClientToScreen:Int[]( hwnd:Int, screendc:Int)
		Local client:Int[4], pt1:Int[2], pt2:Int[4]
		GetClientRect( hwnd, client)
	
		pt1[0] = client[RECT_LEFT]
		pt1[1] = client[RECT_TOP]
		pt2[0] = client[RECT_RIGHT]
		pt2[1] = client[RECT_BOTTOM]
		
		ClientToScreen( hwnd, pt1)
		ClientToScreen( hwnd, pt2)
	
		Local nx:Int = pt1[0]
		Local ny:Int = pt1[1]
		Local nx2:Int = pt2[0]
		Local ny2:Int = pt2[1]
		
		Local scrx:Int = GetDeviceCaps( screendc, HORZRES)
		Local scry:Int = GetDeviceCaps( screendc, VERTRES)
	
		If nx < 0 Then nx = 0
		If ny < 0 Then ny = 0
		If nx2 > scrx Then nx2 = scrx
		If ny2 > scry Then ny2 = scry
	
		Return [nx,ny, nx2 - nx,ny2 - ny]
	EndFunction
	
	Local hwnd:Int = gadget.Query(QUERY_HWND_CLIENT)
	Local srcdc:Int = CreateDC( "DISPLAY", Null, Null, Null)
	Local memdc:Int = CreateCompatibleDC(srcdc)
	Local rect:Int[] = WindowClientToScreen( hwnd, srcdc)
	Local bmp:Int = CreateCompatibleBitmap( srcdc, rect[2],rect[3])
	Local oldbmp:Int = SelectObject( memdc, bmp)
	
	BitBlt( memdc, 0,0,rect[2],rect[3], srcdc,rect[0],rect[1], ROP_SRCCOPY)
	bmp = SelectObject( memdc, oldbmp)
	
	Local pixmap:TPixmap, bits:Int = GetDeviceCaps( srcdc, BITSPIXEL)
	Select bits
'		Case 8	pixmap = CreatePixmap( recr[2],rect[3], PF_I8)
'		Case 16	pixmap = CreatePixmap( recr[2],rect[3], PF_BGR888)
		Case 24	pixmap = CreatePixmap( rect[2],rect[3], PF_BGR888)
		Case 32	pixmap = CreatePixmap( rect[2],rect[3], PF_BGRA8888)
		Default
			Throw "GrabGadgetPixmap() supports only 24 and 32 bits"
	EndSelect

	Local bmphdr:BITMAPINFOHEADER = New BITMAPINFOHEADER
	bmphdr.biSIze = SizeOf(bmphdr)
	bmphdr.biWidth = pixmap.width
	bmphdr.biHeight = -pixmap.height
	bmphdr.biPlanes = 1
	bmphdr.biBitCount = bits
	bmphdr.biCompression = BI_RGB

	Local p:Byte Ptr = pixmap.pixels
	For Local y:Int = 0 Until pixmap.height
		GetDIBits( memdc,bmp, pixmap.height - y - 1,1, p, bmphdr, DIB_RGB_COLORS)
		p :+ pixmap.pitch
	Next
	
	DeleteObject(bmp)
	DeleteDC(srcdc)
	DeleteDC(memdc)
	
	Return pixmap
?
EndFunction