Rounded Rectangle [HELP]

 

I'm a newbie here, the question is how to add a rounded rectangle, currently I can only add rectangles.

Files:
Untitled.png  2 kb
 
neng.diascute:

I'm a newbie here, the question is how to add a rounded rectangle, currently I can only add rectangles.

MT5 Object list does not contain a rounded rectangle, so you can not add rounds to corner. You can only use rectangular/square rectangle
 
neng.diascute:

I'm a newbie here, the question is how to add a rounded rectangle, currently I can only add rectangles.

i dont see the use for an on chart rounded rectangle , but , if you need a gui rounded rectangle 

load up the cavas library 

draw this : (2 rectangles and 4 disks)


 
Lorentzos Roussos #:

i dont see the use for an on chart rounded rectangle , but , if you need a gui rounded rectangle 

load up the cavas library 

draw this : (2 rectangles and 4 disks)


Wow creative idea.
 
Lorentzos Roussos #:

i dont see the use for an on chart rounded rectangle , but , if you need a gui rounded rectangle 

load up the cavas library 

draw this : (2 rectangles and 4 disks)



Hi  Lorentzos Roussos,


Great idea, can you share the basic code?

 
neng.diascute #:


Hi  Lorentzos Roussos,


Great idea, can you share the basic code?

enjoy

//call the canvas library
#include <Canvas\Canvas.mqh>
string system_objects="TEST_";
int OnInit()
  {
  ObjectsDeleteAll(0,system_objects);
  //a create a bitmap design
    if(designRoundedRectangleResource("FILL",300,300,20,20,20,20,clrGreen,255)){
      //create a bitmap label to display the resource 
        ObjectCreate(0,system_objects+"BMP",OBJ_BITMAP_LABEL,0,0,0);
        ObjectSetInteger(0,system_objects+"BMP",OBJPROP_XSIZE,300);
        ObjectSetInteger(0,system_objects+"BMP",OBJPROP_YSIZE,300);
        ObjectSetInteger(0,system_objects+"BMP",OBJPROP_XDISTANCE,30);
        ObjectSetInteger(0,system_objects+"BMP",OBJPROP_YDISTANCE,30);
        ObjectSetString(0,system_objects+"BMP",OBJPROP_BMPFILE,"::FILL");
        ChartRedraw(0);
      }
  return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
  ResourceFree("FILL");
  ObjectsDeleteAll(0,system_objects);
  }
void OnTick()
  {
  }

bool designRoundedRectangleResource(string resource_name,
                                       int width,
                                       int height,
                                       int top_left_radius,
                                       int top_right_radius,
                                       int bottom_left_radius,
                                       int bottom_right_radius,
                                       color rectangle_color,
                                       uchar rectangle_opacity){

/*
we will design a resource .
but wtf is a resource ? it is a bitmap held in memory which can be used
on bitmap label objects in their OBJPROP_BMPFILE property (without even beeing a file)
If you delete the object and the program is still running you can arbitrarily 
create the object again with the resource without needing to design it again.
The rounded corners have limitations , no radius can be greater than half the width
or the height of the rectangle
*/
int max_radius=width/2;
if((height/2)<max_radius){max_radius=height/2;}
//we create a canvas
CCanvas DESIGNER;
        DESIGNER.Destroy();
//and we size it
  DESIGNER.Create("DESIGNER",width,height,COLOR_FORMAT_ARGB_NORMALIZE);
//the above is invisible you can draw whatever you want and the user wont notice until you 
//-------------------------------------------------------------------------------decide
  /*
  now imagine you sliced the rectangle in 4 parts , the top left top right bottom left and bottom right
  define the center :
  */
  int center_x=width/2;
  int center_y=height/2;
  /*
  if we want to draw a rounded corner we must calculate where the center of that circle
  is going to be .
  So , lets define these :
       wid     => width of rectangle 
       hei     => height of rectangle  
       cR      => corner radius
       ciX     => circle center X
       ciY     => circle center Y
       
       CTL     => Corner Top Left
       CTR     => Corner Top Right
       CBL     => Corner Bottom Left
       CBR     => Corner Bottom Right
  Then the formulas for finding the center of the drawn disk for each corner are
       for CTL : ciX=cR 
                 ciY=cR
       for CTR : ciX=wid-cR
                 ciY=cR
       for CBL : ciX=cR
                 ciY=hei-cR      
       for CBR : ciX=wid-cR
                 ciY=hei-cR
        
  */
  //mix our color 
    uint color_to_paint=ColorToARGB(rectangle_color,rectangle_opacity);
  //TOP LEFT CORNER 
    //check if we breach max radius 
      if(top_left_radius>max_radius){top_left_radius=max_radius;}
      else if(top_left_radius<0){top_left_radius=0;}
    //calculate the disk center
      int ciX=top_left_radius;
      int ciY=top_left_radius;
    //draw an antialized circle first
      DESIGNER.CircleAA(ciX,ciY,top_left_radius,color_to_paint);
    //draw the disk
      DESIGNER.FillCircle(ciX,ciY,top_left_radius,color_to_paint);
    /*
    now we must connect the disk to the rectangle structure otherwise the 
    design is going to look like a stovetop
    we need 2 rectangles:
       1::from the (ciX,0) till  (center X,center Y)
       2::from the (0,ciY) till  (center X,center Y) 
    */
      DESIGNER.FillRectangle(ciX,0,center_x,center_y,color_to_paint);
      DESIGNER.FillRectangle(0,ciY,center_x,center_y,color_to_paint);
  //TOP RIGHT CORNER 
    //check if we breach max radius 
      if(top_right_radius>max_radius){top_right_radius=max_radius;}
      else if(top_right_radius<0){top_right_radius=0;}
    //calculate the disk center
      ciX=width-top_right_radius;
      ciY=top_right_radius;
    //draw an antialized circle first
      DESIGNER.CircleAA(ciX,ciY,top_right_radius,color_to_paint);
    //draw the disk
      DESIGNER.FillCircle(ciX,ciY,top_right_radius,color_to_paint);
    /*
    now we must connect the disk to the rectangle structure otherwise the 
    design is going to look like a stovetop
    we need 2 rectangles:
       1::from the (ciX,0) till  (center X,center Y)
       2::from the (width,ciY) till  (center X,center Y) 
    */
      DESIGNER.FillRectangle(ciX,0,center_x,center_y,color_to_paint);
      DESIGNER.FillRectangle(width-1,ciY,center_x,center_y,color_to_paint);    
      // width - 1 because if you have 100 pixels the last one is the 99th
  //BOTTOM LEFT CORNER 
    //check if we breach max radius 
      if(bottom_left_radius>max_radius){bottom_left_radius=max_radius;}
      else if(bottom_left_radius<0){bottom_left_radius=0;}
    //calculate the disk center
      ciX=bottom_left_radius;
      ciY=height-bottom_left_radius;
    //draw an antialized circle first
      DESIGNER.CircleAA(ciX,ciY,bottom_left_radius,color_to_paint);
    //draw the disk
      DESIGNER.FillCircle(ciX,ciY,bottom_left_radius,color_to_paint);
    /*
    now we must connect the disk to the rectangle structure otherwise the 
    design is going to look like a stovetop
    we need 2 rectangles:
       1::from the (ciX,height) till  (center X,center Y)
       2::from the (0,ciY) till  (center X,center Y) 
    */
      DESIGNER.FillRectangle(ciX,height-1,center_x,center_y,color_to_paint);
      DESIGNER.FillRectangle(0,ciY,center_x,center_y,color_to_paint);    
      // height - 1 because if you have 100 pixels the last one is the 99th     
  //BOTTOM RIGHT CORNER 
    //check if we breach max radius 
      if(bottom_right_radius>max_radius){bottom_right_radius=max_radius;}
      else if(bottom_right_radius<0){bottom_right_radius=0;}
    //calculate the disk center
      ciX=width-bottom_right_radius;
      ciY=height-bottom_right_radius;
    //draw an antialized circle first
      DESIGNER.CircleAA(ciX,ciY,bottom_right_radius,color_to_paint);
    //draw the disk
      DESIGNER.FillCircle(ciX,ciY,bottom_right_radius,color_to_paint);
    /*
    now we must connect the disk to the rectangle structure otherwise the 
    design is going to look like a stovetop
    we need 2 rectangles:
       1::from the (ciX,height) till  (center X,center Y)
       2::from the (width,ciY) till  (center X,center Y) 
    */
      DESIGNER.FillRectangle(ciX,height-1,center_x,center_y,color_to_paint);
      DESIGNER.FillRectangle(width-1,ciY,center_x,center_y,color_to_paint);    
      // height - 1 because if you have 100 pixels the last one is the 99th   
DESIGNER.Update(true);
/*
the canvas has a resource of its own but we wanna kinda copy it 
and give it a new name that will be easier to recall
*/
uint copied_pixels[],copied_width=0,copied_height=0;
//so we copy the resource of the canvas 
  ResetLastError();
  ResourceFree(resource_name);
  if(ResourceReadImage(DESIGNER.ResourceName(),copied_pixels,copied_width,copied_height)){
    Print("Resource copied");
    DESIGNER.Destroy();
    if(ResourceCreate(resource_name,copied_pixels,copied_width,copied_height,0,0,copied_width,COLOR_FORMAT_ARGB_NORMALIZE)){
      return(true);
      }else{
      Print("cannot create resource "+resource_name);
      }
    }else{
    Print("Cannot copy resource "+IntegerToString(GetLastError()));
    }
return(false);
}
 
Lorentzos Roussos #:

enjoy

Hi Lorenzo Roussos,


Thank you, I will check it and I will inform you if I have any further questions.

 
What a kind man.