Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

CRect and CCanvas replacement (standard library - several bugs fixed) - Library for MetaTrader 5

author EAcpu | 5 reads | 0 comments |

When you start working on a pixel basis using the standard library, you quickly discover that sometimes a pixel is missing here and there. This strange behavior is mainly caused by several bugs in the CRect class. The main problem here is that the original class fails when calculating the height and width of the rectangle, which affects almost all classes derived from CWnd, since CWnd is graphically based on CRect.

In the original CRect, the width is calculated as follows:

Width = x2 - x1

This is completely wrong. When you create a rectangular object ranging from 0,0 to 10,10, the width of the object is 11 pixels, not 10 pixels. Therefore, the correct answer is

Width = x2 - x1 +1

In some other classes of the standard library, calculations are sometimes correct, but not always (i.e. when creating and addressing resources in memory in CCanvas, but not when filling a rectangle). Regardless, this is an inconsistent behavior and should be corrected.

It is now possible to compare two rectangles using normal operators such as == != >= <= > <

instead of encoding

 if (rectangle1.left==rectangle2.left&&rectangle1.top==rectangle2.rectangletop&&rectangle1.right==rectangle2.rectangleright&&rectangle1.bottom==rectangle2.bottom)

Now you just need to enter

 if (rectangle1==rectangle2)

In addition, unary operators such as ++, --, +, and extending or inflating rectangles are supported.

suggestion:


Attachment download

📎rect.mqh (12.53 KB)

📎canvas.mqh (82.83 KB)

Source: MQL5 #13936

Verification code Refresh