//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- declare the parameters of the graphical resource
string rc_name="Resource";
uint rc_width=100;
uint rc_height=100;
uint rc_data[];
uint rc_size=rc_width*rc_height;
ResetLastError();
//--- set the size of the pixel array
if(ArrayResize(rc_data,rc_size)!=rc_size)
{
Print("ArrayResize() failed. Error code: ",GetLastError());
return;
}
//--- fill the pixel array with a transparent color and create a graphical resource based on it
ArrayInitialize(rc_data,0x00FFFFFF);
if(!ResourceCreate(rc_name,rc_data,rc_width,rc_height,0,0,0,COLOR_FORMAT_ARGB_NORMALIZE))
{
Print("ResourceCreate() failed. Error code: ",GetLastError());
return;
}
Print("Size of created recource array: ",rc_data.Size());
//--- check the created graphical resource.
//--- get the time and price data of the current bar
MqlTick tick={};
if(!SymbolInfoTick(Symbol(),tick))
{
Print("SymbolInfoTick() failed. Error code: ",GetLastError());
return;
}
//--- create the Bitmap object using the coordinates of the last tick price and time
string obj_name="Bitmap";
if(!ObjectCreate(0,obj_name,OBJ_BITMAP,0,tick.time,tick.bid))
{
Print("ObjectCreate() failed. Error code: ",GetLastError());
return;
}
//--- set the width and height of the created bitmap object equal to the width and height of the graphical resource.
//--- set the object anchor point to its center.
ObjectSetInteger(0,obj_name,OBJPROP_XSIZE,rc_width);
ObjectSetInteger(0,obj_name,OBJPROP_YSIZE,rc_height);
ObjectSetInteger(0,obj_name,OBJPROP_ANCHOR,ANCHOR_CENTER);
//--- specify the previously created graphical resource for the bitmap object as an image file
//--- In this case, in order to indicate the name of the graphical resource used, we need to add "::" before its name
ObjectSetString(0,obj_name,OBJPROP_BMPFILE,"::"+rc_name);
//--- set the DodgerBlue color with the transparency of 200
uint clr=ColorToARGB(clrDodgerBlue,200);
//--- fill the entire array of pixels of the graphical resource with the set color
ArrayInitialize(rc_data,clr);
//--- update the graphical resource data
Update(rc_name,rc_data,rc_width,rc_height,true);
//--- wait 3 seconds before reading the graphical resource data
Print("Wait 3 seconds before ResourceReadImage()");
Sleep(3000);
//--- read the image from the resource into a new array of pixels
uint rc_data_copy[];
uint w=0,h=0;
ResetLastError();
if(!ResourceReadImage("::"+rc_name,rc_data_copy,w,h))
{
Print("ResourceReadImage() failed. Error code: ",GetLastError());
return;
}
//--- set the OrangeRed color with the transparency of 200
clr=ColorToARGB(clrOrangeRed,200);
//--- fill the entire array of pixels of the graphical resource with the set color and create a new graphical resource based on it
ArrayInitialize(rc_data_copy,clr);
if(!ResourceCreate(rc_name+"Copy",rc_data_copy,rc_width,rc_height,0,0,0,COLOR_FORMAT_ARGB_NORMALIZE))
{
Print("New ResourceCreate() failed. Error code: ",GetLastError());
return;
}
Print("Size of created new recource array: ",rc_data_copy.Size());
//--- create the "Graphical label" object using the coordinates of the last tick price and time
string obj_name2="BitmapLabel";
if(!ObjectCreate(0,obj_name2,OBJ_BITMAP_LABEL,0,0,0))
{
Print("ObjectCreate() failed. Error code: ",GetLastError());
return;
}
//--- get screen coordinates using the previously received price and time
int x=0,y=0;
if(!ChartTimePriceToXY(0,0,tick.time,tick.bid,x,y))
{
Print("New ChartTimePriceToXY() failed. Error code: ",GetLastError());
return;
}
//--- set the width and height of the created graphical label object equal to the width and height of the graphical resource.
//--- set the object anchor point to its center.
ObjectSetInteger(0,obj_name2,OBJPROP_XSIZE,rc_width);
ObjectSetInteger(0,obj_name2,OBJPROP_YSIZE,rc_height);
ObjectSetInteger(0,obj_name2,OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
ObjectSetInteger(0,obj_name2,OBJPROP_XDISTANCE,x);
ObjectSetInteger(0,obj_name2,OBJPROP_YDISTANCE,y);
//--- set the copied graphical resource as an image file for the graphical label object
//--- in this case, in order to indicate the name of the graphical resource used, we need to add "::" before its name
ObjectSetString(0,obj_name2,OBJPROP_BMPFILE,"::"+rc_name+"Copy");
//--- change the color of the new graphical label object
Print("Wait 3 seconds before changing color to GreenYellow");
Sleep(3000);
//--- set the GreenYellow color with the transparency of 200
clr=ColorToARGB(clrGreenYellow,200);
//--- fill the entire array of pixels of the new graphical resource with the set color
ArrayInitialize(rc_data_copy,clr);
//--- update the graphical resource data
Update(rc_name+"Copy",rc_data_copy,rc_width,rc_height,true);
//--- wait three seconds and delete resources and both objects
Print("Wait 3 seconds before deleting both objects");
Sleep(3000);
Print("Deleting Resource and all Bitmap objects");
ResourceFree("::"+rc_name);
ResourceFree("::"+rc_name+"Copy");
ObjectDelete(0,obj_name);
ObjectDelete(0,obj_name2);
}
//+------------------------------------------------------------------+
//| Update graphical resource data |
//+------------------------------------------------------------------+
void Update(const string res_name,const uint &pixel_data[],const uint width,const uint height,const bool redraw)
{
//--- leave if zero dimensions are passed
if(width==0 || height==0)
return;
//--- update resource data and redraw the chart
if(ResourceCreate(res_name,pixel_data,width,height,0,0,0,COLOR_FORMAT_ARGB_NORMALIZE) && redraw)
ChartRedraw();
}
|