The clipping region of the Delphi GDI object

Source: Internet
Author: User

Original link: http://www.cnblogs.com/pchmonster/archive/2012/07/05/2577627.html

Shear Area (Clipping regions)

Regions is part of the screen that can be used to control the part of the drawing canvas. The Tcanvas class has a Cliprect property, but this property is read-only, and to change the clipping zone, you must use the Windows API, which is still illustrated in the previous example, and modified to show how the cropping area works, and here's the code:

1 2 3 4 5 6 7 8 9 Ten One A var  Bitmap: TBitmap;  Rgn: HRGN;   { long型}begin  ClearCanvas; { 清除当前画布的内容}  Bitmap := TBitmap.Create;  Bitmap.LoadFromFile(‘handshak.bmp‘);  Rgn := CreateRectRgn(50, 50, 250, 250);  SelectClipRgn(Canvas.Handle, Rgn);  Canvas.Draw(0, 0, Bitmap);  Bitmap.Free;end;

When you run the program, you will see that only a subset of the bitmaps are displayed, such as:

The SELECTCLIPRGN function sets the clipping area of the canvas to a rectangle determined by coordinates (50, 50, 250, 250). The bitmap is still drawn in its previous position, but now only a subset of the bitmaps (defined by the clipping area) are visible, and the portions outside the clipping area are omitted.

The clipping area is not necessarily a rule, still taking the previous example as an example and making it more scarce, delete the CreateRectRgn function that creates the rule area, replacing it with the following code:

1 2 3 4 5 6 7 8 9 Ten One A var  Bitmap: TBitmap;  Rgn: HRGN;   { long型}begin  ClearCanvas; { 清除当前画布的内容}  Bitmap := TBitmap.Create;  Bitmap.LoadFromFile(‘handshak.bmp‘);  Rgn := CreateEllipticRgn(30, 30, 250, 250); { 圆形区域}  SelectClipRgn(Canvas.Handle, Rgn);  Canvas.Draw(0, 0, Bitmap);  Bitmap.Free;end;

Re-execute the program, at which point the bitmap is confined to a circular area, such as:

Let's try another type of crop area. The code is as follows:

1 2 3 4 5 6 7 8 9 Ten One A - - the const  Points: array[0..3] of TPoint =    ((X:160; Y:0), (X:0; Y:160), (X:160; Y:320), (X:320; Y:160));var  Bitmap: TBitmap;  Rgn: HRGN;   { long型}begin  ClearCanvas; { 清除当前画布的内容}  Bitmap := TBitmap.Create;  Bitmap.LoadFromFile(‘handshak.bmp‘);  Rgn := CreatePolygonRgn(Points, 4, ALTERNATE); { 菱形区域}  SelectClipRgn(Canvas.Handle, Rgn);  Canvas.Draw(0, 0, Bitmap);  Bitmap.Free;end;

The diamond area used this time. The points array in the program defines the point where the diamond region is created, and the CREATEPOLYGONRGN function creates an area based on the points defined by the points array.

You can use the desired number of points, you do not have to specify the closure point, because the zone automatically connects the start point to the end point, rerun run to see what the graph is. Defined as follows:

1 2 3 4 5 6 7 8 9 Ten One A - - the - const  Points: array[0..11] of TPoint =    ((X:0; Y:0), (X:120; Y:0), (X:120; Y:60), (X:180; Y:60), (X:180; Y:120), (X:120; Y:120),     (X:120; Y:180), (X:0; Y:180), (X:0; Y:120), (X:60; Y:120), (X:60; Y:60), (X:0; Y:60));var  Bitmap: TBitmap;  Rgn: HRGN;   { long型}begin  ClearCanvas; { 清除当前画布的内容}  Bitmap := TBitmap.Create;  Bitmap.LoadFromFile(‘handshak.bmp‘);  Rgn := CreatePolygonRgn(Points, 12, ALTERNATE); { 菱形区域}  SelectClipRgn(Canvas.Handle, Rgn);  Canvas.Draw(0, 0, Bitmap);  Bitmap.Free;end;

Note

This program also shows how to initialize a set of const records. Its program code is as follows:

1 2 3 const  Points: array[0..3] of TPoint =    ((X:160; Y:0), (X:0; Y:160), (X:160; Y:320), (X:320; Y:160));

Tpoint has two fields: X and Y. Note that the field names are listed, followed by a colon and the value assigned to the field (for example: x:80), and also note that the X and Y fields are assigned and enclosed in parentheses, where there are four groups because the points array has 4 elements. This is the only way to define and initialize a set of const records.

Cropping areas can be useful when working with some drawing operations, and the user may not need to use the cropping area frequently, but the cropping area is valuable when it is needed.

The above code is tested in Delphi7 and the sample code is downloaded: GDI bitmap and color palette. rar

The clipping region of the Delphi GDI object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.