Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

A class for working with free form buttons - library for MetaTrader 5

Views:
4992
Rating:
(26)
Published:
2015.04.10 15:43
Updated:
2016.11.22 07:32
3dButtons.mq5 (4.04 KB) view
Class.mqh (24.14 KB) view
img.zip (139.78 KB)
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

This class is designed for creating interactive buttons with various states on a price chart.

It has been developed for a competition arranged by generous TheXpert. Thank you.


A demonstration of the CBtn class


Class methods

Create(long chart_id,int sub_wnd,string name,int x,int y,int dx,int dy) - create a button with parameters:

  1. Window identifier
  2. Subwindow number
  3. Button name
  4. X coordinate
  5. Y coordinate
  6. Horizontal size
  7. Vertical size

Resources(string img_up,string img_up_active="",string img_dn="",string img_dn_active="",string img_up_disable="",string img_dn_disable="",string img_mask="") - define the images for different button states:

  1. Normal unpressed button
  2. Button hovered by cursor
  3. Pressed button
  4. Pressed button hovered by cursor
  5. Disabled button
  6. Button outline mask

SetUseMask(ENUM_USEMASK mask,int x=0,int y=0) - set the mask by the color of the specified pixel; the mask is formed from the standard unpressed button image.

  1. Used mask type
  2. X coordinate
  3. Y coordinate

SetUseMask(ENUM_USEMASK mask,uint acolor) - set the mask by color; the mask is formed from the normal unpressed button image.

  1. Used mask type
  2. Color

SetCorner(ENUM_BASE_CORNER corner) - set the corner of the chart which the button is anchored to

SetAnchor(ENUM_ANCHOR_POINT anchor) - set the anchor type

SetX(int x) - set the X coordinate

SetY(int y) - set the Y coordinate

SetXY(int x,int y) - single method for setting the X and Y coordinates

On(bool state) - set the button status (true for pressed, false for unpressed)

Enable(bool state) - enable/disable the button

Paint(void) - draw the button

Event(int id,long lparam,double dparam,string sparam) - pass events to the button
all parameters are duplicated from the OnChartEvent function

GetX(void) - get the X coordinate

GetY(void) - get the Y coordinate

GetEnable(void) - get the enabled/disabled status

GetOn(void) - get the pressed/unpressed status

GetCorner(void) - get the corner of a chart which the button is anchored to

GetAnchor(void) - get the anchor type

AddText(int x,int y,string font_name,int font_size,color text_color,string text) - add text to the button

  1. X coordinate
  2. Y coordinate
  3. Font name
  4. Font size
  5. Text color
  6. Text

Text(string text) - update the button text (doesn't work without AddText(...) call) 

Creating a button

By default, a button has following parameters:

  • Unpressed
  • Enabled
  • Anchored to upper left corner of the chart
  • Anchored by upper left corner of the button

Setting the button mask

By default, the button is created from the normal unpressed button image. Transparent pixels serve as a mask.

  • UseMask(MASK_STANDALONE_RESOURCE) - the mask uses an image specified in Resources(). If the image is not set, the entire button area (rectangle) will be used as a working area.
  • UseMask(MASK_PIXEL,x,y) - the mask uses the color of the specified pixel. If the pixel color doesn't match the specified color, it becomes the mask. If the coordinate are not set, then use [0,0] coordinates.
  • UseMask(MASK_COLOR,color) - specified color is used for the mask. If the pixel color doesn't match the specified color, it becomes the mask. The color should be set in ARGB format.

Example

//+------------------------------------------------------------------+
//|                                                    3dButtons.mq5 |
//|                                           Copyright 2015, fyords |
//|                           https://login.mql5.com/ru/users/fyords |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, fyords"
#property link      "https://www.mql5.com/en/users/fyords"
#property version   "1.01"
//+------------------------------------------------------------------+
//| Insert resources                                                 |
//+------------------------------------------------------------------+
#resource "img\\200_1.bmp"
#resource "img\\200_2.bmp"
#resource "img\\200_3.bmp"
#resource "img\\200_4.bmp"
#resource "img\\200_5.bmp"
#resource "img\\200_6.bmp"
//+------------------------------------------------------------------+
#include "Class.mqh"
//+------------------------------------------------------------------+
enum Adjust
  {
   UpperLeft,
   UpperRight,
   LowerLeft,
   LowerRight
  };
//+------------------------------------------------------------------+
//| Input property                                                   |
//+------------------------------------------------------------------+
input Adjust adj_corner=UpperLeft;     //Corner
//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
CBtn *btn[];

int num_buttons;
//+------------------------------------------------------------------+
//| Initialization function                                          |
//+------------------------------------------------------------------+
int OnInit()
  {
   long wnd=ChartID();
   int sub_wnd=ChartWindowOnDropped();

   ArrayFree(btn);
   int n=0;
   num_buttons=0;

   for(int y=0;y<3;y++)
     {
      for(int x=0;x<3;x++)
        {
         ArrayResize(btn,n+1);
         btn[n]=new CBtn;
         btn[n].Create(wnd,sub_wnd,"3dButtons_"+(string)MathRand(),x*152+10,y*152+10,200,200);
         btn[n].Resources("img\\200_1.bmp","img\\200_2.bmp","img\\200_3.bmp","img\\200_4.bmp","img\\200_5.bmp","img\\200_6.bmp");
         btn[n].AddText(80,80,"Arial",25,clrWhite,"Button"+(string)(n+1));
         switch(adj_corner)
           {
            case UpperLeft:
               btn[n].SetAnchor(ANCHOR_LEFT_UPPER);
               btn[n].SetCorner(CORNER_LEFT_UPPER);
               break;
            case UpperRight:
               btn[n].SetAnchor(ANCHOR_RIGHT_UPPER);
               btn[n].SetCorner(CORNER_RIGHT_UPPER);
               break;
            case LowerLeft:
               btn[n].SetAnchor(ANCHOR_LEFT_LOWER);
               btn[n].SetCorner(CORNER_LEFT_LOWER);
               break;
            case LowerRight:
               btn[n].SetAnchor(ANCHOR_RIGHT_LOWER);
               btn[n].SetCorner(CORNER_RIGHT_LOWER);
               break;
           }
         btn[n].Paint();
         n++;
        }
     }
   ChartRedraw();

   num_buttons=ArraySize(btn);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Deinitialization function                                        |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   for(int i=0;i<num_buttons;i++) delete btn[i];
   ArrayFree(btn);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   for(int i=0;i<num_buttons;i++) btn[i].Event(id,lparam,dparam,sparam);
  }

Translated from Russian by MetaQuotes Ltd.
Original code: https://www.mql5.com/ru/code/12637

Ozymandias_HTF Ozymandias_HTF

The Ozymandias indicator with the timeframe selection option available in the input parameters.

AFL_Winner AFL_Winner

The ALF Winner indicator identifies a trend change by established turnover areas at -50/+50 levels.

Class СBmpButtonTransparent Class СBmpButtonTransparent

A class for a transparent button.

Risk Manager Risk Manager

The Expert Advisor controls and limits the overall loss for the account, and the loss for every deal. It includes Trailing Stop for the account.