1

I know how to set the colour of a rectangle object programmatically, as seen below. I want to set a default colour that every manually created rectangle is set to in such a way that I can change the colour after creating it. For example, every rectangle manually created is yellow but I can change it to say, blue. How can this be done?

ObjectSetInteger(0,name,OBJPROP_COLOR, clrAliceBlue);
TenOutOfTen
  • 467
  • 6
  • 14

1 Answers1

0

Q : How can this be done?

AFAIK, there is no way since the introduction of the MetaTrader4 Terminal to define and keep an infinitely valid <default-object-color>, as the MT4-GUI-ecosystem retains an LRU-based policy for the <object-color> of the next manual creation of a GUI-object.

The rest is easy.

One can run a script process ( or a utility function, best just in the non-critical section of the EA OnTick(){}-handler ) that will repaint any "manual" GUI-object as per user's set of rules.

Each object must get a "DO-NOT-RE-COLOUR-FLAG" flag prepended to an otherwise unique <Name>-field, during the next manual re-colouring, as you wished to in the behaviour-specification above, so as not having been again re-painted back automatically to a clrIntendedObjectCOLOR-repaint colour ( yellow ), as was intended to take place after a manual creation with { unknown | any colour } setting, in accord with such policy and to stay as fast and as efficient per each re-run as possible.

for ( int anObjectORDINAL  = 0;
          anObjectORDINAL <  ObjectsTotal( MAIN_WINDOW, SUB_WINDOW_OF_INTEREST );
          anObjectORDINAL++
          ){
      string            ObjNAME = ObjectName( anObjectORDINAL );
      if (  ObjectType( ObjNAME ) != OBJTYPE_RECTANGLE
         || ObjectGet(  ObjNAME, OBJPROP_COLOR ) == clrIntendedObjectCOLOR
         || StringFind( ObjNAME, "DO-NOT-RE-COLOUR-FLAG", 0 ) > EMPTY
            ) continue;
      ObjectSet(        ObjNAME, OBJPROP_COLOR,     clrIntendedObjectCOLOR );
}
WindowRedraw(); // GUI-ops deferred WindowRedraw()

Both an Error-detection and Error-handling strategies go way beyond the scope of this Question.

A way more tricky approach would be to create a DMA-alike array of objects ( a maintained repository of GUI-objects, that may avoid top-down blind scans of ObjectsTotal() ), yet that would require a helper function, that will assist to draw a rectangle semi-manually, so as to get it under your code's control since it's first appearance, but will yield in a fast DMA-alike GUI-reprocessing

user3666197
  • 1
  • 6
  • 50
  • 92