Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2015.04.10 15:43

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

| English Русский 中文 Español Deutsch 日本語 Português

votes: 19
3dButtons.mq5 (4.04 KB)view
Class.mqh (24.14 KB)view (139.78 KB)

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.


//|                                                    3dButtons.mq5 |
//|                                           Copyright 2015, fyords |
//|                  |
#property copyright "Copyright 2015, fyords"
#property link      ""
#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
//| 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();

   int n=0;

   for(int y=0;y<3;y++)
      for(int x=0;x<3;x++)
         btn[n]=new CBtn;
            case UpperLeft:
            case UpperRight:
            case LowerLeft:
            case LowerRight:


//| Deinitialization function                                        |
void OnDeinit(const int reason)
   for(int i=0;i<num_buttons;i++) delete btn[i];
//| 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 Software Corp.
Original code:

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.