Page 2 of 2

Re: Select a rectangle on the screen

Posted: Wed Oct 05, 2022 4:44 pm
by AZJIO
Made global. After the first call, hBmp always contains a handle and will be deleted before the call.

Code: Select all

Global hBmp, PDC

Procedure HBitmapFromScreen(X, Y, W, H)
	If hBmp
		SelectObject_(PDC, hBmp)
		DeleteObject_(hBmp)
		DeleteDC_(PDC)
	EndIf
	If Not HDC
		HDC = GetDC_(0)
	EndIf
	hBmp = CreateCompatibleBitmap_(HDC, W, H)
	PDC = CreateCompatibleDC_(HDC)
	SelectObject_(PDC, hBmp)
	BitBlt_(PDC, 0, 0, W, H, HDC, X, Y, #SRCCOPY)
EndProcedure

Re: Select a rectangle on the screen

Posted: Wed Oct 05, 2022 5:10 pm
by Mijikai
Example:

Code: Select all

Procedure.i BitmapFromDC(Handle.i,X.i,Y.i,W.i,H.i)
  Protected.i hdc,hbm,hold
  hdc = CreateCompatibleDC_(Handle)
  If hdc
    hbm = CreateCompatibleBitmap_(Handle,W,H)
    If hbm
      hold = SelectObject_(hdc,hbm)
      If hold
        If BitBlt_(hdc,#Null,#Null,W,H,Handle,X,Y,#SRCCOPY)
          SelectObject_(hdc,hold)
          DeleteDC_(hdc)
          ProcedureReturn hbm
        EndIf
        SelectObject_(hdc,hold)
      EndIf
      DeleteObject_(hbm)
    EndIf
    DeleteDC_(hdc)
  EndIf
  ProcedureReturn #Null
EndProcedure

Re: Select a rectangle on the screen

Posted: Wed Oct 05, 2022 5:22 pm
by AZJIO
I can't delete Bitmap since it is used to save to a file.

Re: Select a rectangle on the screen

Posted: Wed Oct 05, 2022 5:37 pm
by Mijikai
I showed how to deselect the bitmap so that it doesnt get leaked once you lose control over the DC.

Re: Select a rectangle on the screen

Posted: Wed Oct 05, 2022 7:34 pm
by AZJIO

Code: Select all

hold = SelectObject_(hdc,hbm)
is it necessary inside one program? Will this cause a problem in other programs?

I read in the help that it is always necessary to return the original object
An application should always replace a new object with the original, default object after it has finished drawing with the new object.

Re: Select a rectangle on the screen

Posted: Wed Oct 05, 2022 7:46 pm
by Mijikai

Re: Select a rectangle on the screen

Posted: Wed Oct 05, 2022 8:24 pm
by AZJIO
He says that if you control your DC, then you can not delete it right away, but delete it later. This is the strategy I chose. This can only be a problem if someone is using this code and specifically using my DC. I hope the user himself will decide when to delete and he will not mix one with the other.