How to create multiple Bitmaps using single CCanvas object?

 

How to create multiple Bitmap using CCanvas?

I have coded a sample code below.

When you attach it, you will see only 1 Bitmap instead of 2. Notice in the CreateBitmap function, both has a different name.

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com/en/users/abiotamalo"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#include <Canvas\Canvas.mqh>
 #include <Canvas\Charts\HistogramChart.mqh>
CCanvas canvas;
CHistogramChart chart;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnInit()
  {

  }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {  
   return rates_total;
  }
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{   
   if(id==CHARTEVENT_CHART_CHANGE){
//--- create canvas
   int x=0;
   int y=0;
   ChartTimePriceToXY(0,0,iTime(Symbol(),PERIOD_CURRENT,114),iHigh(Symbol(),PERIOD_CURRENT,114),x,y);
   int x1=0;
   int y1=0;
   ChartTimePriceToXY(0,0,iTime(Symbol(),PERIOD_CURRENT,97),iLow(Symbol(),PERIOD_CURRENT,97),x1,y1); 
   double priceWidth=x1-x;
   double priceHeight=y1-y;
   uchar alpha=80;
   bool objCreated=canvas.CreateBitmap("11111",iTime(Symbol(),PERIOD_CURRENT,114),iHigh(Symbol(),PERIOD_CURRENT,114),x1-x,y1-y,COLOR_FORMAT_ARGB_NORMALIZE);
   canvas.Erase(ColorToARGB(clrNONE, 0));
   canvas.Update();   
   double barHeight=priceHeight/200;    
   canvas.FillRectangle(0,0,200,200,ColorToARGB(clrYellow,alpha));
   //canvas.TransparentLevelSet(alpha);
   canvas.Update();   
   
   ChartTimePriceToXY(0,0,iTime(Symbol(),PERIOD_CURRENT,50),iHigh(Symbol(),PERIOD_CURRENT,114),x,y);
   x1=0;
   y1=0;
   ChartTimePriceToXY(0,0,iTime(Symbol(),PERIOD_CURRENT,5),iLow(Symbol(),PERIOD_CURRENT,97),x1,y1); 
   priceWidth=x1-x;
   priceHeight=y1-y;
   alpha=80;
   objCreated=canvas.CreateBitmap("2222",iTime(Symbol(),PERIOD_CURRENT,50),iHigh(Symbol(),PERIOD_CURRENT,50),x1-x,y1-y,COLOR_FORMAT_ARGB_NORMALIZE);
   canvas.Erase(ColorToARGB(clrNONE, 0));
   canvas.Update();   
   barHeight=priceHeight/200;    
   canvas.FillRectangle(0,0,200,200,ColorToARGB(clrRed,alpha));
   //canvas.TransparentLevelSet(alpha);
   canvas.Update();          
   }
}
 
You only have one instance of CCanvas object, you need to use another one. Each CCanvas tracks one bitmap, and if you call create again, the previous bitmap would be deleted (and track the most recent)
 

That's stupid lol.

What's the point of giving a name when creating a bitmap?
I thought it's used to be an identifier.

I'm planning to make several objects as much as possible and attach it to multiple infinite swings. How could I do that with Canvas?

Thanks! :)

 

Problem solved

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com/en/users/abiotamalo"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#include <Canvas\Canvas.mqh>
 #include <Canvas\Charts\HistogramChart.mqh>
CHistogramChart chart;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnInit()
  {

  }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {  
   return rates_total;
  }
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{   
   if(id==CHARTEVENT_CHART_CHANGE){
//--- create canvas
   int x=0;
   int y=0;
   ChartTimePriceToXY(0,0,iTime(Symbol(),PERIOD_CURRENT,114),iHigh(Symbol(),PERIOD_CURRENT,114),x,y);
   int x1=0;
   int y1=0;
   ChartTimePriceToXY(0,0,iTime(Symbol(),PERIOD_CURRENT,97),iLow(Symbol(),PERIOD_CURRENT,97),x1,y1); 
   double priceWidth=x1-x;
   double priceHeight=y1-y;
   uchar alpha=80;
   bool objCreated=false;
   double barHeight=priceHeight/200;  
   if(true){
   CCanvas canvas;
   bool objCreated=canvas.CreateBitmap("11111",iTime(Symbol(),PERIOD_CURRENT,114),iHigh(Symbol(),PERIOD_CURRENT,114),x1-x,y1-y,COLOR_FORMAT_ARGB_NORMALIZE);
   canvas.Erase(ColorToARGB(clrNONE, 0));
   canvas.Update();        
   canvas.FillRectangle(0,0,200,200,ColorToARGB(clrYellow,alpha));
   //canvas.TransparentLevelSet(alpha);
   canvas.Update();   
   }
   ChartTimePriceToXY(0,0,iTime(Symbol(),PERIOD_CURRENT,50),iHigh(Symbol(),PERIOD_CURRENT,114),x,y);
   x1=0;
   y1=0;
   ChartTimePriceToXY(0,0,iTime(Symbol(),PERIOD_CURRENT,5),iLow(Symbol(),PERIOD_CURRENT,97),x1,y1); 
   priceWidth=x1-x;
   priceHeight=y1-y;
   alpha=80;
   objCreated=false;
   if(true){
   CCanvas canvas;
   objCreated=canvas.CreateBitmap("2222",iTime(Symbol(),PERIOD_CURRENT,50),iHigh(Symbol(),PERIOD_CURRENT,50),x1-x,y1-y,COLOR_FORMAT_ARGB_NORMALIZE);
   canvas.Erase(ColorToARGB(clrNONE, 0));
   canvas.Update();   
   barHeight=priceHeight/200;    
   canvas.FillRectangle(0,0,200,200,ColorToARGB(clrYellow,alpha));
   //canvas.TransparentLevelSet(alpha);
   canvas.Update();          
   }
   }
}
 
Agustinus Biotamalo Lumbantoruan #:

That's stupid lol.

What's the point of giving a name when creating a bitmap?
I thought it's used to be an identifier.

I'm planning to make several objects as much as possible and attach it to multiple infinite swings. How could I do that with Canvas?

Thanks! :)

Just create a one canvas object covering the complete screen and then fill the pixels i.e. draw as much objects you want. Why need another canvas object?