Главная страница | назад





Article #19286: Resizing a Canvas Object's cliprect

 Question and Answer Database
FAQ4286C.txt :Resizing a Canvas Object's cliprect
Category :VCL
Platform :All Windows
Product :C++Builder1.0, C++Builder3.x, C++Builder4.x,
Question:
How do I resize a Canvas Object's cliprect?
Answer:
To do we need to combine Win32 API calls with VCL code. The example below uses an Image component
and a Button. Place the code in the OnClick event handler of the button.
void __fastcall TForm1::Button3Click(TObject *Sender)
{
HRGN MyRgn;
MyRgn = ::CreateRectRgn(100,100,200,200);
::SelectClipRgn(Image1->Canvas->Handle,MyRgn);
Ellipse(Image1->Canvas->Handle,90,90,210,210);
Image1->Invalidate();
::SelectClipRgn(Image1->Canvas->Handle,NULL);
::DeleteObject(MyRgn);
MyRgn = CreateRectRgn(200,200,300,300);
::SelectClipRgn(Image1->Canvas->Handle,MyRgn);
::Ellipse(Image1->Canvas->Handle,190,190,310,310);
Image1->Invalidate();
::SelectClipRgn(Image1->Canvas->Handle,NULL);
::DeleteObject(MyRgn);
MyRgn = ::CreateRectRgn(300,300,400,400);
::SelectClipRgn(Image1->Canvas->Handle,MyRgn);
::Ellipse(Image1->Canvas->Handle,290,290,410,410);
Image8->Invalidate();
::SelectClipRgn(Image1->Canvas->Handle,NULL);// reset to default state
::DeleteObject(MyRgn);
}
In this function we are changing the Clipping region from the VCL default rect
that is the whole canvas to one of our own definition. Because the ClipRect
property is read only, we can?t set it directly. We must do some behind the
scenes work and then let the VCL do its work. First, what we need to do is
define a region and we do this with the API call CreateRectRgn and pass it
the region we wish to create. In the fist case we are creating a 100 x 100
pixel square region anchored at 100,100 on the canvas. Next we need to select
this region as the clipping Rect and we do this with SelectClipRgn. This function
takes two parameters, a DC which we use Image8->Canvas->Handle and the region we
wish to select. Next we draw our ellipse and immediately follow itfollowed by a
call to the Invalidate method. This issues a WM_PAINT message and the VCL reads
the ClipRect, which is now the region that we defined and does the drawing
accordingly. A second call to SelectClipRgn passing NULL as the second parameter
reselects the original clipping region we had prior to us redefining it and lastly
we need to do our own cleanup, so we have to make the call to DeleteObject to
release the region. So there you have a good example of how to mix VCL code with
straight API Windows code.
Lastly we can see that the ClipRect has indeed been reset to the default value.
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Image8->Canvas->FillRect(Image8->Canvas->ClipRect);
}
This function call repaints the whole canvas, so our ClipRect is back to normal.
1/21/1999 2:53:32 PM

Last Modified: 01-SEP-99