GUI Panel Framework
MQL4 has no native window system - every on-chart panel is really a handful of chart objects a program creates, repositions and deletes by hand. Two parts of that are fiddly enough that almost every home-grown panel gets them wrong: dragging, and telling child controls apart in the event handler. Dragging has no mid-motion hook. The terminal moves a selectable object on its own; the program only learns about it afterwards, through CHARTEVENT_OBJECT_DRAG in OnChartEvent, carrying just the object's name. This class makes its background rectangle selectable, and HandleDrag() compares the rectangle's new XDISTANCE/YDISTANCE against the position it last recorded, then moves every child control by that same delta in one pass. Call it once per CHARTEVENT_OBJECT_DRAG and the whole panel tracks the mouse as a unit - there is no polling and no per-frame work. Child tracking is the second trap. A common pattern is comparing the sparam object name against a list of hard-coded strings inside OnChartEvent, which turns into an unreadable if/else ladder the moment a panel has more than three controls, and breaks quietly if a name is typo'd on one side. AddButton and AddLabel here take a caller-chosen integer id instead - HandleClick resolves the clicked object straight back to that id, so the host program's event handler is a small switch on stable, self-documenting constants, never a string comparison. The class only ever deletes objects it created itself, tracked in its own arrays, so Destroy() cannot reach out and remove something else on the chart the way a prefix-substring delete occasionally does. Self-contained by the catalogue's own rule, this header does not include ChartObjects.mqh even though the two solve related problems - the small number of object calls a panel needs are duplicated here rather than pulling in a second library from this collection. Notes and limitations - The demo is a script, and a script's OnStart runs once with no live OnChartEvent loop, so it cannot show a real mouse drag. It reproduces what the terminal does internally - moves the background object's distance properties directly - then calls HandleDrag() exactly as a host EA's OnChartEvent would, which exercises the same code path a real drag uses. - Positioning is upper-left-corner only (CORNER_LEFT_UPPER). Other corners would need the drag delta sign-flipped per corner for no real benefit to a panel meant to sit in a fixed screen position. - Buttons reset their own pressed state on click, so they behave as momentary push buttons. A toggle button or checkbox needs different state handling - see the WidgetButton and WidgetChoice libraries in this catalogue. - This framework provides the panel shell and generic button/label children. It does not include sliders, edit fields or checkboxes - those are separate, self-contained libraries in this catalogue by design, so a program that only needs a plain draggable panel is not forced to pull in widgets it does not use. - Not thread-safe in any special way beyond what the platform already guarantees - chart objects are only ever touched from the UI thread, as with any MQL4 or MQL5 program.
Script · MT5 · v1.0 · Free
About GUI Panel Framework
GUI Panel Framework is a script for MetaTrader 5. It has 1 setting, such as Prefix. MQL source code included.
Files in the download: PanelFramework.mq5.mqh, PanelFramework.txt, PanelFrameworkDemo.ex5, PanelFrameworkDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Prefix | PanelDemo_ | Object-name prefix for this panel instance |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code