Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i undo the effect of IntersectClipRect?

Given the following code snippet:

procedure TPicture.PaintLine(_Canvas: TCanvas; _Left, _Top, _Right, _Bottom: Integer);
begin
  IntersectClipRect(_Canvas.Handle, _Left, _Top, _Right, _Bottom);
  try
    _Canvas.MoveTo(_Left - 10, _Top - 10);
    _Canvas.LineTo(_Right + 10, _Bottom + 10);
    // (This is an example only, the actual drawing is much more complex.)
  finally
    SelectClipRgn(_Canvas.Handle, 0); // This does too much
  end;
end;

I want to undo the clipping effected by the call to IntersectClipRect so the previously active clipping becomes active again. In the above code, this is done by SelectClipRgn(...,0) which turns off clipping altogether. This works, kind of, but afterwards there is no clipping active so any drawing that is executed after the above will paint to areas that should not be painted to.

So, what is the correct way to undo only the effect of IntersectClipRect?

EDIT: Removed the unnecessary CreateRectRgn and DeleteObject code after I understood the comment from Sertac, to make the question more readable for others that might stumble upon it later.

like image 502
dummzeuch Avatar asked Dec 26 '22 21:12

dummzeuch


1 Answers

You can save and restore the state of the DC:

var
  //  RGN: HRGN;
  SavedDC: Integer;
begin
//  RGN := CreateRectRgn(_Left, _Top, _Right, _Bottom);
  SavedDC := SaveDC(_Canvas.Handle);
  try
    IntersectClipRect(_Canvas.Handle, _Left, _Top, _Right, _Bottom);
    _Canvas.MoveTo(_Left - 10, _Top - 10);
    _Canvas.LineTo(_Right + 10, _Bottom + 10);
    // (This is an example only, the actual drawing is much more complex.)
  finally
    RestoreDC(_Canvas.Handle, SavedDC);
  end;
  ...
like image 164
Sertac Akyuz Avatar answered Jan 04 '23 03:01

Sertac Akyuz