Loading…

Button Widget

OBJ_BUTTON is a toggle by default - clicking it flips OBJPROP_STATE and the terminal leaves it pressed until clicked again. Most trading-panel buttons ("Buy", "Close All") are meant to be momentary: they fire once and spring back up. Code that forgets this ends up with buttons that stay visually pressed after doing their job, so nearly every home-grown panel resets OBJPROP_STATE to false on every click - and then breaks the one case where that reset is wrong, an actual toggle such as "Auto Trading On/Off", where resetting the state throws away the very thing the button exists to remember. This widget takes the toggle flag once, in Create(), and Clicked() does the right thing for either kind from then on: a momentary button resets itself immediately after reporting a click, a toggle button leaves the terminal's own state alone so IsPressed() reflects whatever the user last left it at. Disabled state is handled the same way. SetEnabled(false) greys the button out and Clicked() unconditionally returns false for it afterwards, forcing OBJPROP_STATE back to false so a disabled button cannot end up looking pressed while ignoring input - a caller does not have to remember to guard every click site with its own enabled check. The widget is a thin wrapper over one chart object; it does not draw anything beyond what OBJ_BUTTON already draws, and it has no dependency on the panel framework or any other library in this catalogue. Notes and limitations - Clicked() is designed to be called once per CHARTEVENT_OBJECT_CLICK, from the host program's OnChartEvent. It is not a polling function - calling it outside a real click event only checks the object's current OBJPROP_STATE against sparam, it does not detect a click on its own. - The demo cannot generate a real mouse click, since a script's OnStart runs once with no live event loop. It simulates the terminal's own sequence - set OBJPROP_STATE, then call Clicked() - which exercises the exact code path a genuine OnChartEvent-driven click would use. - SetEnabled(false) always greys to a fixed colour, not a computed dimmer version of whatever colour was passed to Create(). Call SetEnabled(true) to restore the original colour exactly. - One widget instance owns exactly one chart object. A row of buttons needs one CButtonWidget per button, or use PanelFramework in this catalogue if you want a container that also tracks a group of children as a unit.

Script · MT4 · v1.0 · Free

About Button Widget

Button Widget is a script for MetaTrader 4. It has 1 setting, such as Prefix. MQL source code included.

Files in the download: WidgetButton.mqh, WidgetButton.txt, WidgetButtonDemo.mq4.

Settings

SettingDefaultWhat it does
PrefixWidgetButtonDemo_Object-name prefix for this demo's buttons

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria