Search code examples
delphiwinapi

Clipping region coordinates are relative to control parent and not to control itself?


I am working on a graph chart control, and I want to define a clipping region to draw in it. I pass SelectClipRgn() the coordinates to my clipping region relative to the Left and Top of the control. But it seems that it doesn't select the correct region. In fact, the region became correct only if I add the Left and Top position of the control within its Parent (a form or a panel). What is happening? Is this behavior normal?

 ClipRect:= Rect(MarginLeft, MarginTop, MarginLeft + GraphWidth, MarginTop + GraphHeight);
 NewClipRgn:= CreateRectRgn(Left+ClipRect.Left, Top+ClipRect.Top, Left+ClipRect.Right, Top+ClipRect.Bottom);
 SelectClipRgn(Canvas.Handle, NewClipRgn);

Solution

  • I don't know what caused this problem, but I found a way to make my control work. I read the coordinates of the current clipping region that the system gives me when I try to Paint, and then I adjust it relative to my control. In this way, I always have the correct coordinates.

     OldClipRgn:= CreateRectRgn(0, 0, 0, 0);
     GetClipRgn(Canvas.Handle, OldClipRgn);
     GetRgnBox(OldClipRgn, RgnBox);
     RgnBox.Left:=   RgnBox.Left + MarginLeft;
     RgnBox.Right:=  RgnBox.Left + GraphWidth;
     RgnBox.Top:=    RgnBox.Top + MarginTop;
     RgnBox.Bottom:= RgnBox.Top + GraphHeight;
     NewClipRgn:= CreateRectRgn(RgnBox.Left, RgnBox.Top, RgnBox.Right, RgnBox.Bottom);
     SelectClipRgn(Canvas.Handle, NewClipRgn);
    
     // do the drawing...
    
     SelectClipRgn(Canvas.Handle, OldClipRgn);
     DeleteObject(OldClipRgn); DeleteObject(NewClipRgn);