TRegion has a large number of operators.
These operators can be used to query and modify the values of a region. They are not necessarily restricted to working with other regions; many of them let you add and subtract rectangles and other units to and from the region.
TRegion provides two Boolean test operators, == and !=. These operators work to compare two regions. If two regions are equivalent, the == operator returns true, and the != operator returns false. If two regions are not equivalent, the == operator returns false, and the != operator returns true. You can use these operators much as you do their equivalents for ints, chars, and so on. For example, suppose you want to test whether two regions are identical, and, if they are not, perform an operation on them. The code would look something like this:
TRegion also provides a number of assignment operators that you can use to change the region:
- The = operator lets you assign one region to another. For example, the statement rgn1 = rgn2 sets the contents of rgn1 to the contents of rgn2, regardless of the contexts of rgn1 prior to the assignment.
- The += operator lets you move a region by an offset contained in a TSize object. This operation is analogous to numerical addition: just add the offset to each point in the region. The region retains all its properties, except that the coordinates defining the region are shifted by the values contained in the cx and cy members of the TSize object:
- If cx is positive, the region is shifted cx pixels to the right.
- If cx is negative, the region is shifted cx pixels to the left.
- If cy is positive, the region is shifted cy pixels down.
- If cy is negative, the region is shifted cy pixels up.
For example, suppose you want to move a region to the right 50 pixels and up 20 pixels. The code would look something like this:
TSize size(50, -20);
.
.
.
- The -= operator, when used with a TSize object, does essentially the opposite of the += operator; that is, it subtracts the offset from each point in the region. For example, suppose you have the same code as in the previous example, except that instead of using the += operator, it uses the -= operator. This code would offset the region in exactly the opposite way from the += operator, 50 pixels to the left and down 20 pixels.
- The -= operator, when used with a TRegion object, behaves differently from when it is used with a TSize object. To demonstrate how the -= operator works when used with TRegion, consider the following code: After execution of this code, rgn1 contains all the area it contained originally, minus any parts of that area shared by rgn2. Thus any point that is contained in rgn2 is not contained in rgn1 after this code has executed. This is analogous to subtraction: subtract the area defined by rgn2 from rgn1.
- The &= operator can be used with both TRegion objects and TRect objects (before any operations are performed, the TRect is converted to a TRegion using the constructor TRegion::TRegion(TRect &)). The following code demonstrates how the &= operator works: After execution of this code, rgn1 contains all the area it originally shared with rgn2; that is, areas that were common to both regions before the execution of the &= statement. This is a logical AND operation: only the areas that are part of both rgn1 AND rgn2 become part of the new region.
- The |= operator can be used with both TRegion objects and TRect objects (before any operations are performed, the TRect is converted to a TRegion using the constructor TRegion::TRegion(TRect &)). The following code demonstrates how the |= operator works: After execution of this code, rgn1 contains all the area it originally contained, plus all the area contained in rgn2; that is, it contains all of both regions. This is a logical OR operation: Areas that are part of either rgn1 OR rgn2 become part of the new region.
- The ^= operator can be used with both TRegion objects and TRect objects (before any operations are performed, the TRect is converted to a TRegion using the constructor TRegion::TRegion(TRect &)). The following code demonstrates how the ^= operator works: After execution of this code, rgn1 contains only that area it originally contained but did not share with rgn2, plus all the area originally contained in rgn2 that was not shared with rgn1. This operator combines both areas and removes the overlapping sections. This is a logical XOR (exclusive OR) operation: Areas that are part of either rgn1 OR rgn2 but not of both become part of the new region.
See Also