Delphi SelectClipRGN hides bitmap
I am drawing my own background (derived from TGraphicControl) with border. After drawing the border in the DrawBorderRect function, I return the client area and want to constrain my future painting to this new region. Everything works if I don't use ClipRgn.
Working code:
var
R : TRect;
begin;
R := GetClientRect; //(R -> 0, 0, 300, 200)
R := DrawBorderRect(R); //(R -> 20, 20, 280, 180)
Canvas.StretchDraw(R, FBitmap); //FBitmap is a 3 pixel x 3 pixel square
end;
The above code gives me the following:
But I want to use ClipRgn and I tried the following. This time the template is not displayed (but if I click on the area, the pattern appears. So it works like a working, but then gets overwritten for some reason?).
Problem code:
var
R : TRect;
ClientRegion: HRGN;
begin;
R := GetClientRect; //(R -> 0, 0, 300, 200)
R := DrawBorderRect(R); //(R -> 20, 20, 280, 180)
ClientRegion := CreateRectRgn(R.Left, R.top, R.Right, R.Bottom);
SelectClipRgn(Canvas.Handle, ClientRegion);
try
Canvas.StretchDraw(R, FBitmap); //FBitmap is a 3 pixel x 3 pixel square
finally
SelectClipRgn(Canvas.Handle, HRGN(nil));
DeleteObject(ClientRegion);
end;
end;
and I get this (unless I left click, in which case I see above)
Any ideas as to what is going on and what I am missing?
source to share
SelectClipRgn
takes the coordinates of the device.
TGraphicControl
Descendants have the device context retrieved for their parent window. The origin of the viewport is moved to be able to set the origin of the client (0, 0), but they are logical coordinates.
In short, you need to offset your region:
... ClientRegion := CreateRectRgn(R.Left, R.top, R.Right, R.Bottom); OffsetRgn(ClientRegion, Left, Top); // <-- SelectClipRgn(Canvas.Handle, ClientRegion); ...
source to share