Download MetaTrader 5

click button

To add comments, please log in or register
chinaski
977
chinaski  

Hello,

i want to provide a click button in my expert which behaviour is the same as any normal button.

So, when mouse is over button and left mouse button pressed, the button should change into pressed state.

Unfortunately, i get no such LBUTTON_DOWN event.


The next useful event for this is CHARTEVENT_OBJECT_CLICK.

But this is triggered, when left mouse button released.


Is there any chance (best without hack) to catch LBUTTON_DOWN event ?


Thank you for any advice.

Enrique Dangeroux
299
Enrique Dangeroux  

There is no Event for the pressed or depressed state, you have to handle state with your own logic

ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state); 
chinaski
977
chinaski  
Enrique Dangeroux:

There is no Event for the pressed or depressed state, you have to handle state with your own logic

Thank you for answer, but this was not my question. 

The question is: Can i catch the left mouse button down event.

I get only the event when mouse button released.

Petr Nosek
373
Petr Nosek  
chinaski:

Hello,

i want to provide a click button in my expert which behaviour is the same as any normal button.

So, when mouse is over button and left mouse button pressed, the button should change into pressed state.

Unfortunately, i get no such LBUTTON_DOWN event.


The next useful event for this is CHARTEVENT_OBJECT_CLICK.

But this is triggered, when left mouse button released.


Is there any chance (best without hack) to catch LBUTTON_DOWN event ?


Thank you for any advice.

You should try to solve this by win api functions. I dealt with a similar (not the same) problem in this thread: https://www.mql5.com/en/forum/225054
OBJ_EDIT focus
OBJ_EDIT focus
  • 2018.01.18
  • www.mql5.com
Hello, I have two similar questions related to OBJ_EDIT: 1) Can I find out if the object (OBJ_EDIT) has focus (the cursor blinks inside the box) in...
SanjayBalraj
48
SanjayBalraj  
Petr Nosek:
You should try to solve this by win api functions. I dealt with a similar (not the same) problem in this thread: https://www.mql5.com/en/forum/225054

There's no need for hacks my fren. Here is example EA button which capture left click events.

//+------------------------------------------------------------------+
//|                                                     ButtonEA.mq4 |
//|                                    Copyright 2018, Sanjay Balraj |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Sanjay Balraj"
#property version   "1.00"
#property strict
#include <ChartObjects\ChartObjectsTxtControls.mqh>

class EAbutton : public CChartObjectButton
{
protected:
   bool  m_ignore;
   bool  m_locked_state;
public:
   EAbutton()
   { 
      m_ignore = false;
      m_locked_state = State(); 
   }
   bool Create()
   {
      if(!CChartObjectButton::Create(0,"EAbutton",0,50,50,500,50))
         return false;
      Description("Click me!");
      return true;
   }
   void OnChartEvent(int id,long lp,double dp,string sp)
   {
      if(id==CHARTEVENT_OBJECT_CLICK && sp == Name())
      {
         State(!State());
         m_locked_state = State();
         m_ignore = false;
         Alert(StringFormat("Button(%s) state = %s",Name(),string(State())));   
         Description(State() ? "BUTTON_DOWN" : "BUTTON_UP");  
      }
      else
      if(id==CHARTEVENT_MOUSE_MOVE && sp == "1" && IsButtonZone((int)lp,(int)dp))
      {
         if(!m_ignore)
         {
            m_ignore = true;
            State(!State());
         }
      }
      else
      if(id==CHARTEVENT_MOUSE_MOVE && !IsButtonZone((int)lp,(int)dp))
      {
         if(State() != m_locked_state)
         {
            State(m_locked_state);
            m_ignore = false;
         }
      } 
   }
protected:
   bool  IsButtonZone(int x,int y)
   {
      return (x >= X_Distance() && x <= X_Distance()+X_Size()) && (y >= Y_Distance() && y <= Y_Distance() + Y_Size());
   }
};

EAbutton button;
//+------------------------------------------------------------------+
int OnInit()
{
   ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true);
   if(!button.Create())
      return INIT_FAILED;
   return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
void OnTick(){}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   button.OnChartEvent(id,lparam,dparam,sparam);
}
//+------------------------------------------------------------------+
Enrique Dangeroux
299
Enrique Dangeroux  
chinaski:

Thank you for answer, but this was not my question. 

The question is: Can i catch the left mouse button down event.

I get only the event when mouse button released.

There is none. Maybe you can abuse Drag event but probably only fires when mouse click down + mouse move. 

Why do you need to it anyway? 

SanjayBalraj
48
SanjayBalraj  
Enrique Dangeroux:

There is none. Maybe you can abuse Drag event but probably only fires when mouse click down + mouse move. 

Why do you need to it anyway? 

No need for abuse my fren... There is event for left click down. See my example above. 
Alain Verleyen
Moderator
35118
Alain Verleyen  
chinaski:

Thank you for answer, but this was not my question. 

The question is: Can i catch the left mouse button down event.

I get only the event when mouse button released.

Use MOUSE_MOVE event and check sparam value. (see documentation).

EDIT: just saw @SanjayBalraj which provides the code.
chinaski
977
chinaski  
SanjayBalraj:

There's no need for hacks my fren. Here is example EA button which capture left click events.

Thank you, this works fine. This is the solution what i am looking for.

chinaski
977
chinaski  
chinaski:

Thank you, this works fine. This is the solution what i am looking for.

Just a comment: This is a creative solution but a native left button down event would be better,

because you don't need to make those checks when mouse is moving.


Given you have 50 buttons, it could get a performance drawdown.

nicholishen
1537
nicholishen  
chinaski:

Just a comment: This is a creative solution but a native left button down event would be better,

because you don't need to make those checks when mouse is moving.


Given you have 50 buttons, it could get a performance drawdown.

Unfortunately that's the only way to capture left click down events in mql, and while a performance drain would be quantifiable on the microsecond scale, it would be completely imperceptible to the human brain.
To add comments, please log in or register