It seems that the official CButton class in Controls\Button.mqh doesn't adapt BASE CORNER?

To add comments, please log in or register
Amy Liu
948
Amy Liu  
The CButton class is derived from CWndObj, and CWndObj is derived from CWnd. All the classes don't have an interface to set BASE CORNER. If you set BASE CORNER by ObjectSetInteger OBJPROP_CORNER, then the coordinates is weird. This will make event handler to response false due to position miss. Does anyone encountered this issue when using official CButton? Do you have solutions to use it easily? Thanks.
Marco vd Heijden
Moderator
9225
Marco vd Heijden  

Hello the corner is:

x1

[in]  X coordinate of the upper left corner.

y1

[in]  Y coordinate of the upper left corner.

x2

[in]  X coordinate of the lower right corner.

y2

[in]  Y coordinate of the lower right corner.


There are some limitations to using these libraries.

You can always use the standard functions.

// Functions:
//- ButtonCreate()
//- ButtonMove()
//- ButtonChangeSize()
//- ButtonChangeCorner()
//- ButtonTextChange()
//- ButtonDelete()

//+------------------------------------------------------------------+ 
//| Create the button                                                | 
//+------------------------------------------------------------------+ 
bool ButtonCreate(const long              chart_ID=0,               // chart's ID 
                  const string            name="Button",            // button name 
                  const int               sub_window=0,             // subwindow index 
                  const int               x=0,                      // X coordinate 
                  const int               y=0,                      // Y coordinate 
                  const int               width=50,                 // button width 
                  const int               height=18,                // button height 
                  const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring 
                  const string            text="Button",            // text 
                  const string            font="Arial",             // font 
                  const int               font_size=10,             // font size 
                  const color             clr=clrBlack,             // text color 
                  const color             back_clr=C'236,233,216',  // background color 
                  const color             border_clr=clrNONE,       // border color 
                  const bool              state=false,              // pressed/released 
                  const bool              back=false,               // in the background 
                  const bool              selection=false,          // highlight to move 
                  const bool              hidden=true,              // hidden in the object list 
                  const long              z_order=0)                // priority for mouse click 
  { 
//--- reset the error value 
   ResetLastError(); 
//--- create the button 
   if(!ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0)) 
     { 
      Print(__FUNCTION__, 
            ": failed to create the button! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- set button coordinates 
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x); 
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y); 
//--- set button size 
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width); 
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height); 
//--- set the chart's corner, relative to which point coordinates are defined 
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner); 
//--- set the text 
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text); 
//--- set text font 
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font); 
//--- set font size 
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size); 
//--- set text color 
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 
//--- set background color 
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr); 
//--- set border color 
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr); 
//--- display in the foreground (false) or background (true) 
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); 
//--- set button state 
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state); 
//--- enable (true) or disable (false) the mode of moving the button by mouse 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); 
//--- hide (true) or display (false) graphical object name in the object list 
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); 
//--- set the priority for receiving the event of a mouse click in the chart 
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); 
//--- successful execution 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| Move the button                                                  | 
//+------------------------------------------------------------------+ 
bool ButtonMove(const long   chart_ID=0,    // chart's ID 
                const string name="Button", // button name 
                const int    x=0,           // X coordinate 
                const int    y=0)           // Y coordinate 
  { 
//--- reset the error value 
   ResetLastError(); 
//--- move the button 
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x)) 
     { 
      Print(__FUNCTION__, 
            ": failed to move X coordinate of the button! Error code = ",GetLastError()); 
      return(false); 
     } 
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y)) 
     { 
      Print(__FUNCTION__, 
            ": failed to move Y coordinate of the button! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- successful execution 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| Change button size                                               | 
//+------------------------------------------------------------------+ 
bool ButtonChangeSize(const long   chart_ID=0,    // chart's ID 
                      const string name="Button", // button name 
                      const int    width=50,      // button width 
                      const int    height=18)     // button height 
  { 
//--- reset the error value 
   ResetLastError(); 
//--- change the button size 
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width)) 
     { 
      Print(__FUNCTION__, 
            ": failed to change the button width! Error code = ",GetLastError()); 
      return(false); 
     } 
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height)) 
     { 
      Print(__FUNCTION__, 
            ": failed to change the button height! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- successful execution 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| Change corner of the chart for binding the button                | 
//+------------------------------------------------------------------+ 
bool ButtonChangeCorner(const long             chart_ID=0,               // chart's ID 
                        const string           name="Button",            // button name 
                        const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER) // chart corner for anchoring 
  { 
//--- reset the error value 
   ResetLastError(); 
//--- change anchor corner 
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner)) 
     { 
      Print(__FUNCTION__, 
            ": failed to change the anchor corner! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- successful execution 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| Change button text                                               | 
//+------------------------------------------------------------------+ 
bool ButtonTextChange(const long   chart_ID=0,    // chart's ID 
                      const string name="Button", // button name 
                      const string text="Text")   // text 
  { 
//--- reset the error value 
   ResetLastError(); 
//--- change object text 
   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text)) 
     { 
      Print(__FUNCTION__, 
            ": failed to change the text! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- successful execution 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| Delete the button                                                | 
//+------------------------------------------------------------------+ 
bool ButtonDelete(const long   chart_ID=0,    // chart's ID 
                  const string name="Button") // button name 
  { 
//--- reset the error value 
   ResetLastError(); 
//--- delete the button 
   if(!ObjectDelete(chart_ID,name)) 
     { 
      Print(__FUNCTION__, 
            ": failed to delete the button! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- successful execution 
   return(true); 
  } 
//+------------------------------------------------------------------+ 




nicholi shen
2388
nicholi shen  
Amy Liu:
The CButton class is derived from CWndObj, and CWndObj is derived from CWnd. All the classes don't have an interface to set BASE CORNER. If you set BASE CORNER by ObjectSetInteger OBJPROP_CORNER, then the coordinates is weird. This will make event handler to response false due to position miss. Does anyone encountered this issue when using official CButton? Do you have solutions to use it easily? Thanks.
That's because CButton is only intended to be used to add buttons to a CAppDialog subclass. If you're trying to add this button to a chart then you are using the wrong class. See https://www.mql5.com/en/docs/standardlibrary/chart_object_classes/obj_controls/cchartobjectbutton
Documentation on MQL5: Standard Library / Graphic Objects / Control Objects / CChartObjectButton
Documentation on MQL5: Standard Library / Graphic Objects / Control Objects / CChartObjectButton
  • www.mql5.com
Standard Library / Graphic Objects / Control Objects / CChartObjectButton - Reference on algorithmic/automated trading language for MetaTrader 5
Amy Liu
948
Amy Liu  
Marco vd Heijden:

Hello the corner is:

x1

[in]  X coordinate of the upper left corner.

y1

[in]  Y coordinate of the upper left corner.

x2

[in]  X coordinate of the lower right corner.

y2

[in]  Y coordinate of the lower right corner.


There are some limitations to using these libraries.

You can always use the standard functions.




Thank you. I have used the standard objects functions to deal with buttons. 

Amy Liu
948
Amy Liu  
nicholi shen:
That's because CButton is only intended to be used to add buttons to a CAppDialog subclass. If you're trying to add this button to a chart then you are using the wrong class. See https://www.mql5.com/en/docs/standardlibrary/chart_object_classes/obj_controls/cchartobjectbutton

Thank you very much. This is really what I have expected. ;)

To add comments, please log in or register